From ed5b33bcbfca7acb660ece69dec4a6e339331848 Mon Sep 17 00:00:00 2001 From: hardfist Date: Thu, 15 Aug 2024 15:56:42 +0800 Subject: [PATCH] feat(core): don't inject bundlerInfo in library mode (#7567) --- .../output/library-truthy/index.js | 7 +++++ .../output/library-truthy/rspack.config.js | 8 ++++++ .../output/library-undefined/index.js | 7 +++++ .../output/library-undefined/rspack.config.js | 6 +++++ .../tests/defaultsCases/library/library.js | 3 +++ .../defaultsCases/library/name-placeholder.js | 3 +++ .../library/name-root-placeholder.js | 3 +++ .../defaultsCases/library/placeholder.js | 3 +++ .../defaultsCases/library/root-placeholder.js | 3 +++ packages/rspack/src/config/defaults.ts | 26 ++++++++++++++----- 10 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 packages/rspack-test-tools/tests/configCases/output/library-truthy/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/output/library-truthy/rspack.config.js create mode 100644 packages/rspack-test-tools/tests/configCases/output/library-undefined/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/output/library-undefined/rspack.config.js diff --git a/packages/rspack-test-tools/tests/configCases/output/library-truthy/index.js b/packages/rspack-test-tools/tests/configCases/output/library-truthy/index.js new file mode 100644 index 00000000000..c39bb0d24e6 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/output/library-truthy/index.js @@ -0,0 +1,7 @@ +it('should not inject bundlerInfo when library is truthy', () => { + const content = require('fs').readFileSync( + require('path').resolve(__dirname, "bundle0.js"), + "utf-8" + ); + expect(content).not.toContain('__webpack_require__' + '.rv') +}) \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/output/library-truthy/rspack.config.js b/packages/rspack-test-tools/tests/configCases/output/library-truthy/rspack.config.js new file mode 100644 index 00000000000..8c5696e760e --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/output/library-truthy/rspack.config.js @@ -0,0 +1,8 @@ +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + output: { + library: { + type: 'modern-module' + } + } +} \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/output/library-undefined/index.js b/packages/rspack-test-tools/tests/configCases/output/library-undefined/index.js new file mode 100644 index 00000000000..6eb91d85e74 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/output/library-undefined/index.js @@ -0,0 +1,7 @@ +it('should inject bundlerInfo when library is falsy', () => { + const content = require('fs').readFileSync( + require('path').resolve(__dirname, "bundle0.js"), + "utf-8" + ); + expect(content).toContain('__webpack_require__.rv') +}) \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/output/library-undefined/rspack.config.js b/packages/rspack-test-tools/tests/configCases/output/library-undefined/rspack.config.js new file mode 100644 index 00000000000..9040e5bcd0d --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/output/library-undefined/rspack.config.js @@ -0,0 +1,6 @@ +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + output: { + + } +} \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/defaultsCases/library/library.js b/packages/rspack-test-tools/tests/defaultsCases/library/library.js index 270873a35f0..c8053c3fc6b 100644 --- a/packages/rspack-test-tools/tests/defaultsCases/library/library.js +++ b/packages/rspack-test-tools/tests/defaultsCases/library/library.js @@ -7,6 +7,9 @@ module.exports = { - Expected + Received + @@ ... @@ + - "force": true, + + "force": false, @@ ... @@ - "chunkLoadingGlobal": "webpackChunk_rspack_test_tools", + "chunkLoadingGlobal": "webpackChunkmyLib_awesome", diff --git a/packages/rspack-test-tools/tests/defaultsCases/library/name-placeholder.js b/packages/rspack-test-tools/tests/defaultsCases/library/name-placeholder.js index d620651a67e..8aeac253b43 100644 --- a/packages/rspack-test-tools/tests/defaultsCases/library/name-placeholder.js +++ b/packages/rspack-test-tools/tests/defaultsCases/library/name-placeholder.js @@ -14,6 +14,9 @@ module.exports = { - Expected + Received + @@ ... @@ + - "force": true, + + "force": false, @@ ... @@ - "chunkLoadingGlobal": "webpackChunk_rspack_test_tools", + "chunkLoadingGlobal": "webpackChunkmyLib_lib", diff --git a/packages/rspack-test-tools/tests/defaultsCases/library/name-root-placeholder.js b/packages/rspack-test-tools/tests/defaultsCases/library/name-root-placeholder.js index a8106373b32..56b3d922268 100644 --- a/packages/rspack-test-tools/tests/defaultsCases/library/name-root-placeholder.js +++ b/packages/rspack-test-tools/tests/defaultsCases/library/name-root-placeholder.js @@ -16,6 +16,9 @@ module.exports = { - Expected + Received + @@ ... @@ + - "force": true, + + "force": false, @@ ... @@ - "chunkLoadingGlobal": "webpackChunk_rspack_test_tools", + "chunkLoadingGlobal": "webpackChunk_name_my_name_Lib_name_", diff --git a/packages/rspack-test-tools/tests/defaultsCases/library/placeholder.js b/packages/rspack-test-tools/tests/defaultsCases/library/placeholder.js index 791758d7764..08bbab78daa 100644 --- a/packages/rspack-test-tools/tests/defaultsCases/library/placeholder.js +++ b/packages/rspack-test-tools/tests/defaultsCases/library/placeholder.js @@ -11,6 +11,9 @@ module.exports = { - Expected + Received + @@ ... @@ + - "force": true, + + "force": false, @@ ... @@ - "chunkLoadingGlobal": "webpackChunk_rspack_test_tools", + "chunkLoadingGlobal": "webpackChunkmyLib", diff --git a/packages/rspack-test-tools/tests/defaultsCases/library/root-placeholder.js b/packages/rspack-test-tools/tests/defaultsCases/library/root-placeholder.js index c158f478638..9ac4b77197d 100644 --- a/packages/rspack-test-tools/tests/defaultsCases/library/root-placeholder.js +++ b/packages/rspack-test-tools/tests/defaultsCases/library/root-placeholder.js @@ -16,6 +16,9 @@ module.exports = { - Expected + Received + @@ ... @@ + - "force": true, + + "force": false, @@ ... @@ - "chunkLoadingGlobal": "webpackChunk_rspack_test_tools", + "chunkLoadingGlobal": "webpackChunkmyLib", diff --git a/packages/rspack/src/config/defaults.ts b/packages/rspack/src/config/defaults.ts index 3de849980a3..f87163ee8a0 100644 --- a/packages/rspack/src/config/defaults.ts +++ b/packages/rspack/src/config/defaults.ts @@ -46,6 +46,7 @@ import type { Optimization, Performance, ResolveOptions, + RspackFutureOptions, RuleSetRules, SnapshotOptions } from "./zod"; @@ -110,6 +111,11 @@ export const applyRspackOptionsDefaults = ( entry: options.entry, futureDefaults: options.experiments.futureDefaults! }); + // bundlerInfo is affected by outputDefaults so must be executed after outputDefaults + applybundlerInfoDefaults( + options.experiments.rspackFuture, + options.output.library + ); applyExternalsPresetsDefaults(options.externalsPresets, { targetProperties @@ -197,16 +203,24 @@ const applyExperimentsDefaults = (experiments: ExperimentsNormalized) => { // IGNORE(experiments.rspackFuture): Rspack specific configuration D(experiments, "rspackFuture", {}); - if (typeof experiments.rspackFuture === "object") { - D(experiments.rspackFuture, "bundlerInfo", {}); - if (typeof experiments.rspackFuture.bundlerInfo === "object") { + // rspackFuture.bundlerInfo default value is applied after applyDefaults +}; + +const applybundlerInfoDefaults = ( + rspackFuture?: RspackFutureOptions, + library?: Library +) => { + if (typeof rspackFuture === "object") { + D(rspackFuture, "bundlerInfo", {}); + if (typeof rspackFuture.bundlerInfo === "object") { D( - experiments.rspackFuture.bundlerInfo, + rspackFuture.bundlerInfo, "version", require("../../package.json").version ); - D(experiments.rspackFuture.bundlerInfo, "bundler", "rspack"); - D(experiments.rspackFuture.bundlerInfo, "force", true); + D(rspackFuture.bundlerInfo, "bundler", "rspack"); + // don't inject for library mode + D(rspackFuture.bundlerInfo, "force", !library); } } };