Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add nwjs external preset #6907

Merged
merged 6 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Object {
"electronPreload": false,
"electronRenderer": false,
"node": false,
"nwjs": false,
"web": true,
},
"externalsType": "var",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ module.exports = {
+ "electronMain": true,
@@ ... @@
- "node": false,
- "web": true,
+ "node": true,
@@ ... @@
- "web": true,
+ "web": false,
@@ ... @@
- "document": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ module.exports = {

@@ ... @@
- "node": false,
- "web": true,
+ "node": true,
@@ ... @@
- "web": true,
+ "web": false,
@@ ... @@
- "document": true,
Expand Down
110 changes: 110 additions & 0 deletions packages/rspack-test-tools/tests/defaultsCases/target_/nwjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/** @type {import('../../..').TDefaultsCaseConfig} */
module.exports = {
description: "target nwjs",
options: () => ({ target: "nwjs" }),
diff: e =>
e.toMatchInlineSnapshot(`
- Expected
+ Received

@@ ... @@
- "node": false,
- "nwjs": false,
+ "node": true,
+ "nwjs": true,
@@ ... @@
- "document": true,
+ "document": false,
@@ ... @@
- "target": "web",
+ "target": "nwjs",
@@ ... @@
- "exportsOnly": false,
+ "exportsOnly": true,
@@ ... @@
- "exportsOnly": false,
+ "exportsOnly": true,
@@ ... @@
- "exportsOnly": false,
+ "exportsOnly": true,
@@ ... @@
- "__dirname": "warn-mock",
- "__filename": "warn-mock",
- "global": "warn",
+ "__dirname": "eval-only",
+ "__filename": "eval-only",
+ "global": false,
@@ ... @@
- "chunkFormat": "array-push",
- "chunkLoading": "jsonp",
+ "chunkFormat": "commonjs",
+ "chunkLoading": "async-node",
@@ ... @@
- "jsonp",
- "import-scripts",
+ "async-node",
@@ ... @@
- "fetch",
+ "async-node",
@@ ... @@
- "document": true,
+ "document": false,
@@ ... @@
- "globalObject": "self",
+ "globalObject": "global",
@@ ... @@
- "publicPath": "auto",
+ "publicPath": "",
@@ ... @@
- "wasmLoading": "fetch",
+ "wasmLoading": "async-node",
@@ ... @@
- "workerChunkLoading": "import-scripts",
+ "workerChunkLoading": "async-node",
@@ ... @@
- "workerWasmLoading": "fetch",
+ "workerWasmLoading": "async-node",
@@ ... @@
- "aliasFields": Array [
- "browser",
- ],
+ "aliasFields": Array [],
@@ ... @@
- "browser",
@@ ... @@
- "aliasFields": Array [
- "browser",
- ],
+ "aliasFields": Array [],
@@ ... @@
- "browser",
@@ ... @@
- "aliasFields": Array [
- "browser",
- ],
+ "aliasFields": Array [],
@@ ... @@
- "browser",
@@ ... @@
- "aliasFields": Array [
- "browser",
- ],
+ "aliasFields": Array [],
@@ ... @@
- "browser",
@@ ... @@
- "aliasFields": Array [
- "browser",
- ],
+ "aliasFields": Array [],
@@ ... @@
- "browser",
@@ ... @@
+ "node",
@@ ... @@
+ "nwjs",
@@ ... @@
- "target": "web",
+ "target": "nwjs",
`)
};
12 changes: 10 additions & 2 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3097,6 +3097,7 @@ const externalsPresets: z.ZodObject<{
electronMain: z.ZodOptional<z.ZodBoolean>;
electronPreload: z.ZodOptional<z.ZodBoolean>;
electronRenderer: z.ZodOptional<z.ZodBoolean>;
nwjs: z.ZodOptional<z.ZodBoolean>;
}, "strict", z.ZodTypeAny, {
node?: boolean | undefined;
web?: boolean | undefined;
Expand All @@ -3105,6 +3106,7 @@ const externalsPresets: z.ZodObject<{
electronMain?: boolean | undefined;
electronPreload?: boolean | undefined;
electronRenderer?: boolean | undefined;
nwjs?: boolean | undefined;
}, {
node?: boolean | undefined;
web?: boolean | undefined;
Expand All @@ -3113,6 +3115,7 @@ const externalsPresets: z.ZodObject<{
electronMain?: boolean | undefined;
electronPreload?: boolean | undefined;
electronRenderer?: boolean | undefined;
nwjs?: boolean | undefined;
}>;

// @public (undocumented)
Expand Down Expand Up @@ -9399,6 +9402,7 @@ export const rspackOptions: z.ZodObject<{
electronMain: z.ZodOptional<z.ZodBoolean>;
electronPreload: z.ZodOptional<z.ZodBoolean>;
electronRenderer: z.ZodOptional<z.ZodBoolean>;
nwjs: z.ZodOptional<z.ZodBoolean>;
}, "strict", z.ZodTypeAny, {
node?: boolean | undefined;
web?: boolean | undefined;
Expand All @@ -9407,6 +9411,7 @@ export const rspackOptions: z.ZodObject<{
electronMain?: boolean | undefined;
electronPreload?: boolean | undefined;
electronRenderer?: boolean | undefined;
nwjs?: boolean | undefined;
}, {
node?: boolean | undefined;
web?: boolean | undefined;
Expand All @@ -9415,6 +9420,7 @@ export const rspackOptions: z.ZodObject<{
electronMain?: boolean | undefined;
electronPreload?: boolean | undefined;
electronRenderer?: boolean | undefined;
nwjs?: boolean | undefined;
}>>;
infrastructureLogging: z.ZodOptional<z.ZodObject<{
appendOnly: z.ZodOptional<z.ZodBoolean>;
Expand Down Expand Up @@ -10974,7 +10980,7 @@ export const rspackOptions: z.ZodObject<{
templateLiteral?: boolean | undefined;
} | undefined;
} | undefined;
target?: false | "es3" | "es5" | "es2015" | "es2016" | "es2017" | "es2018" | "es2019" | "es2020" | "es2021" | "es2022" | "node" | "async-node" | "web" | "browserslist" | "webworker" | `node${number}` | `async-node${number}` | `node${number}.${number}` | `async-node${number}.${number}` | "electron-main" | `electron${number}-main` | `electron${number}.${number}-main` | "electron-renderer" | `electron${number}-renderer` | `electron${number}.${number}-renderer` | "electron-preload" | `electron${number}-preload` | `electron${number}.${number}-preload` | "nwjs" | `nwjs${number}` | `nwjs${number}.${number}` | "node-webkit" | `node-webkit${number}` | `node-webkit${number}.${number}` | ("es3" | "es5" | "es2015" | "es2016" | "es2017" | "es2018" | "es2019" | "es2020" | "es2021" | "es2022" | "node" | "async-node" | "web" | "browserslist" | "webworker" | `node${number}` | `async-node${number}` | `node${number}.${number}` | `async-node${number}.${number}` | "electron-main" | `electron${number}-main` | `electron${number}.${number}-main` | "electron-renderer" | `electron${number}-renderer` | `electron${number}.${number}-renderer` | "electron-preload" | `electron${number}-preload` | `electron${number}.${number}-preload` | "nwjs" | `nwjs${number}` | `nwjs${number}.${number}` | "node-webkit" | `node-webkit${number}` | `node-webkit${number}.${number}`)[] | undefined;
target?: false | "es3" | "es5" | "es2015" | "es2016" | "es2017" | "es2018" | "es2019" | "es2020" | "es2021" | "es2022" | "node" | "async-node" | "web" | "nwjs" | "browserslist" | "webworker" | `node${number}` | `async-node${number}` | `node${number}.${number}` | `async-node${number}.${number}` | "electron-main" | `electron${number}-main` | `electron${number}.${number}-main` | "electron-renderer" | `electron${number}-renderer` | `electron${number}.${number}-renderer` | "electron-preload" | `electron${number}-preload` | `electron${number}.${number}-preload` | `nwjs${number}` | `nwjs${number}.${number}` | "node-webkit" | `node-webkit${number}` | `node-webkit${number}.${number}` | ("es3" | "es5" | "es2015" | "es2016" | "es2017" | "es2018" | "es2019" | "es2020" | "es2021" | "es2022" | "node" | "async-node" | "web" | "nwjs" | "browserslist" | "webworker" | `node${number}` | `async-node${number}` | `node${number}.${number}` | `async-node${number}.${number}` | "electron-main" | `electron${number}-main` | `electron${number}.${number}-main` | "electron-renderer" | `electron${number}-renderer` | `electron${number}.${number}-renderer` | "electron-preload" | `electron${number}-preload` | `electron${number}.${number}-preload` | `nwjs${number}` | `nwjs${number}.${number}` | "node-webkit" | `node-webkit${number}` | `node-webkit${number}.${number}`)[] | undefined;
mode?: "none" | "development" | "production" | undefined;
experiments?: {
lazyCompilation?: boolean | {
Expand Down Expand Up @@ -11021,6 +11027,7 @@ export const rspackOptions: z.ZodObject<{
electronMain?: boolean | undefined;
electronPreload?: boolean | undefined;
electronRenderer?: boolean | undefined;
nwjs?: boolean | undefined;
} | undefined;
infrastructureLogging?: {
appendOnly?: boolean | undefined;
Expand Down Expand Up @@ -11464,7 +11471,7 @@ export const rspackOptions: z.ZodObject<{
templateLiteral?: boolean | undefined;
} | undefined;
} | undefined;
target?: false | "es3" | "es5" | "es2015" | "es2016" | "es2017" | "es2018" | "es2019" | "es2020" | "es2021" | "es2022" | "node" | "async-node" | "web" | "browserslist" | "webworker" | `node${number}` | `async-node${number}` | `node${number}.${number}` | `async-node${number}.${number}` | "electron-main" | `electron${number}-main` | `electron${number}.${number}-main` | "electron-renderer" | `electron${number}-renderer` | `electron${number}.${number}-renderer` | "electron-preload" | `electron${number}-preload` | `electron${number}.${number}-preload` | "nwjs" | `nwjs${number}` | `nwjs${number}.${number}` | "node-webkit" | `node-webkit${number}` | `node-webkit${number}.${number}` | ("es3" | "es5" | "es2015" | "es2016" | "es2017" | "es2018" | "es2019" | "es2020" | "es2021" | "es2022" | "node" | "async-node" | "web" | "browserslist" | "webworker" | `node${number}` | `async-node${number}` | `node${number}.${number}` | `async-node${number}.${number}` | "electron-main" | `electron${number}-main` | `electron${number}.${number}-main` | "electron-renderer" | `electron${number}-renderer` | `electron${number}.${number}-renderer` | "electron-preload" | `electron${number}-preload` | `electron${number}.${number}-preload` | "nwjs" | `nwjs${number}` | `nwjs${number}.${number}` | "node-webkit" | `node-webkit${number}` | `node-webkit${number}.${number}`)[] | undefined;
target?: false | "es3" | "es5" | "es2015" | "es2016" | "es2017" | "es2018" | "es2019" | "es2020" | "es2021" | "es2022" | "node" | "async-node" | "web" | "nwjs" | "browserslist" | "webworker" | `node${number}` | `async-node${number}` | `node${number}.${number}` | `async-node${number}.${number}` | "electron-main" | `electron${number}-main` | `electron${number}.${number}-main` | "electron-renderer" | `electron${number}-renderer` | `electron${number}.${number}-renderer` | "electron-preload" | `electron${number}-preload` | `electron${number}.${number}-preload` | `nwjs${number}` | `nwjs${number}.${number}` | "node-webkit" | `node-webkit${number}` | `node-webkit${number}.${number}` | ("es3" | "es5" | "es2015" | "es2016" | "es2017" | "es2018" | "es2019" | "es2020" | "es2021" | "es2022" | "node" | "async-node" | "web" | "nwjs" | "browserslist" | "webworker" | `node${number}` | `async-node${number}` | `node${number}.${number}` | `async-node${number}.${number}` | "electron-main" | `electron${number}-main` | `electron${number}.${number}-main` | "electron-renderer" | `electron${number}-renderer` | `electron${number}.${number}-renderer` | "electron-preload" | `electron${number}-preload` | `electron${number}.${number}-preload` | `nwjs${number}` | `nwjs${number}.${number}` | "node-webkit" | `node-webkit${number}` | `node-webkit${number}.${number}`)[] | undefined;
mode?: "none" | "development" | "production" | undefined;
experiments?: {
lazyCompilation?: boolean | {
Expand Down Expand Up @@ -11511,6 +11518,7 @@ export const rspackOptions: z.ZodObject<{
electronMain?: boolean | undefined;
electronPreload?: boolean | undefined;
electronRenderer?: boolean | undefined;
nwjs?: boolean | undefined;
} | undefined;
infrastructureLogging?: {
appendOnly?: boolean | undefined;
Expand Down
9 changes: 5 additions & 4 deletions packages/rspack/src/config/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import assert from "assert";
import fs from "fs";
import path from "path";

import { ASSET_MODULE_TYPE } from "../ModuleTypeConstants";
import { SwcCssMinimizerRspackPlugin } from "../builtin-plugin/SwcCssMinimizerPlugin";
import { SwcJsMinimizerRspackPlugin } from "../builtin-plugin/SwcJsMinimizerPlugin";
import { isNil } from "../util";
import { assertNotNill } from "../util/assertNotNil";
import { cleverMerge } from "../util/cleverMerge";
import {
EntryDescriptionNormalized,
Expand Down Expand Up @@ -43,10 +47,6 @@ import type {
SnapshotOptions
} from "./zod";
import Template = require("../Template");
import { ASSET_MODULE_TYPE } from "../ModuleTypeConstants";
import { SwcCssMinimizerRspackPlugin } from "../builtin-plugin/SwcCssMinimizerPlugin";
import { SwcJsMinimizerRspackPlugin } from "../builtin-plugin/SwcJsMinimizerPlugin";
import { assertNotNill } from "../util/assertNotNil";

export const applyRspackOptionsDefaults = (
options: RspackOptionsNormalized
Expand Down Expand Up @@ -852,6 +852,7 @@ const applyExternalsPresetsDefaults = (
targetProperties.electron &&
targetProperties.electronRenderer
);
D(externalsPresets, "nwjs", targetProperties && targetProperties.nwjs);
};

const applyLoaderDefaults = (
Expand Down
3 changes: 2 additions & 1 deletion packages/rspack/src/config/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,8 @@ const externalsPresets = z.strictObject({
electron: z.boolean().optional(),
electronMain: z.boolean().optional(),
electronPreload: z.boolean().optional(),
electronRenderer: z.boolean().optional()
electronRenderer: z.boolean().optional(),
nwjs: z.boolean().optional()
});
export type ExternalsPresets = z.infer<typeof externalsPresets>;
//#endregion
Expand Down
4 changes: 3 additions & 1 deletion packages/rspack/src/rspackOptionsApply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import {
HttpExternalsRspackPlugin,
InferAsyncModulesPlugin,
JavascriptModulesPlugin,
JsLoaderRspackPlugin,
JsonModulesPlugin,
LazyCompilationPlugin,
MangleExportsPlugin,
Expand Down Expand Up @@ -116,6 +115,9 @@ export class RspackOptionsApply {
) {
new ElectronTargetPlugin().apply(compiler);
}
if (options.externalsPresets.nwjs) {
new ExternalsPlugin("node-commonjs", "nw.gui").apply(compiler);
}
if (
options.externalsPresets.web ||
options.externalsPresets.webAsync ||
Expand Down
4 changes: 4 additions & 0 deletions website/docs/en/config/externals.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,7 @@ Treat electron built-in modules in the renderer context like `web-frame`, `ipc-r
### externalsPresets.electronPreload

Treat electron built-in modules in the preload context like `web-frame`, `ipc-renderer` or `shell` as external and load them via `require()` when used.

### externalsPresets.nwjs

Treat node-webkit built-in modules in the preload context like `nw.gui` as external and load them via `import` when used.
4 changes: 4 additions & 0 deletions website/docs/zh/config/externals.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,7 @@ jq('.my-element').animate(/* ... */);
### externalsPresets.electronPreload

将 Electron 预加载脚本中常见的 Electron 内置模块如 `web-frame`、`ipc-renderer` 或 `shell` 视为外部模块,并在使用时通过 `require()` 加载它们。

### externalsPresets.nwjs

将 node-webkit 预加载脚本中常见的 node-webkit 内置模块如 `nw.gui` 视为外部模块,并在使用时通过 `import` 加载它们。
Loading