diff --git a/packages/rspack-test-tools/tests/__snapshots__/Defaults.test.js.snap b/packages/rspack-test-tools/tests/__snapshots__/Defaults.test.js.snap index 7716f2e3759..d8bb3b5c78e 100644 --- a/packages/rspack-test-tools/tests/__snapshots__/Defaults.test.js.snap +++ b/packages/rspack-test-tools/tests/__snapshots__/Defaults.test.js.snap @@ -35,6 +35,7 @@ Object { "electronPreload": false, "electronRenderer": false, "node": false, + "nwjs": false, "web": true, }, "externalsType": "var", diff --git a/packages/rspack-test-tools/tests/defaultsCases/target_/electron-main.js b/packages/rspack-test-tools/tests/defaultsCases/target_/electron-main.js index c6d6c933abd..afa613ea4ea 100644 --- a/packages/rspack-test-tools/tests/defaultsCases/target_/electron-main.js +++ b/packages/rspack-test-tools/tests/defaultsCases/target_/electron-main.js @@ -14,8 +14,9 @@ module.exports = { + "electronMain": true, @@ ... @@ - "node": false, - - "web": true, + "node": true, + @@ ... @@ + - "web": true, + "web": false, @@ ... @@ - "document": true, diff --git a/packages/rspack-test-tools/tests/defaultsCases/target_/node.js b/packages/rspack-test-tools/tests/defaultsCases/target_/node.js index 23d0195bde0..4565cb24114 100644 --- a/packages/rspack-test-tools/tests/defaultsCases/target_/node.js +++ b/packages/rspack-test-tools/tests/defaultsCases/target_/node.js @@ -9,8 +9,9 @@ module.exports = { @@ ... @@ - "node": false, - - "web": true, + "node": true, + @@ ... @@ + - "web": true, + "web": false, @@ ... @@ - "document": true, diff --git a/packages/rspack-test-tools/tests/defaultsCases/target_/nwjs.js b/packages/rspack-test-tools/tests/defaultsCases/target_/nwjs.js new file mode 100644 index 00000000000..c4156f7ffe0 --- /dev/null +++ b/packages/rspack-test-tools/tests/defaultsCases/target_/nwjs.js @@ -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", + `) +}; diff --git a/packages/rspack/etc/api.md b/packages/rspack/etc/api.md index 52b5164c9a2..6c3f31fbac8 100644 --- a/packages/rspack/etc/api.md +++ b/packages/rspack/etc/api.md @@ -3097,6 +3097,7 @@ const externalsPresets: z.ZodObject<{ electronMain: z.ZodOptional; electronPreload: z.ZodOptional; electronRenderer: z.ZodOptional; + nwjs: z.ZodOptional; }, "strict", z.ZodTypeAny, { node?: boolean | undefined; web?: boolean | undefined; @@ -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; @@ -3113,6 +3115,7 @@ const externalsPresets: z.ZodObject<{ electronMain?: boolean | undefined; electronPreload?: boolean | undefined; electronRenderer?: boolean | undefined; + nwjs?: boolean | undefined; }>; // @public (undocumented) @@ -9399,6 +9402,7 @@ export const rspackOptions: z.ZodObject<{ electronMain: z.ZodOptional; electronPreload: z.ZodOptional; electronRenderer: z.ZodOptional; + nwjs: z.ZodOptional; }, "strict", z.ZodTypeAny, { node?: boolean | undefined; web?: boolean | undefined; @@ -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; @@ -9415,6 +9420,7 @@ export const rspackOptions: z.ZodObject<{ electronMain?: boolean | undefined; electronPreload?: boolean | undefined; electronRenderer?: boolean | undefined; + nwjs?: boolean | undefined; }>>; infrastructureLogging: z.ZodOptional; @@ -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 | { @@ -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; @@ -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 | { @@ -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; diff --git a/packages/rspack/src/config/defaults.ts b/packages/rspack/src/config/defaults.ts index 8135eb8be43..114167f8d46 100644 --- a/packages/rspack/src/config/defaults.ts +++ b/packages/rspack/src/config/defaults.ts @@ -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, @@ -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 @@ -852,6 +852,7 @@ const applyExternalsPresetsDefaults = ( targetProperties.electron && targetProperties.electronRenderer ); + D(externalsPresets, "nwjs", targetProperties && targetProperties.nwjs); }; const applyLoaderDefaults = ( diff --git a/packages/rspack/src/config/zod.ts b/packages/rspack/src/config/zod.ts index 6c688a84224..bc0923c5a6f 100644 --- a/packages/rspack/src/config/zod.ts +++ b/packages/rspack/src/config/zod.ts @@ -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; //#endregion diff --git a/packages/rspack/src/rspackOptionsApply.ts b/packages/rspack/src/rspackOptionsApply.ts index 33979261f0c..1a2cb96ba71 100644 --- a/packages/rspack/src/rspackOptionsApply.ts +++ b/packages/rspack/src/rspackOptionsApply.ts @@ -44,7 +44,6 @@ import { HttpExternalsRspackPlugin, InferAsyncModulesPlugin, JavascriptModulesPlugin, - JsLoaderRspackPlugin, JsonModulesPlugin, LazyCompilationPlugin, MangleExportsPlugin, @@ -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 || diff --git a/website/docs/en/config/externals.mdx b/website/docs/en/config/externals.mdx index 4ebe10ca06a..989d59fd915 100644 --- a/website/docs/en/config/externals.mdx +++ b/website/docs/en/config/externals.mdx @@ -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. diff --git a/website/docs/zh/config/externals.mdx b/website/docs/zh/config/externals.mdx index e3adf4d345c..e27a81266c1 100644 --- a/website/docs/zh/config/externals.mdx +++ b/website/docs/zh/config/externals.mdx @@ -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` 加载它们。