From 9cf27a94066291eeb85420c4ec16d6d0d1163fca Mon Sep 17 00:00:00 2001 From: hai-x Date: Wed, 19 Feb 2025 17:50:30 +0800 Subject: [PATCH 1/3] fix: commonjs static --- .../src/assign_library_plugin.rs | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/crates/rspack_plugin_library/src/assign_library_plugin.rs b/crates/rspack_plugin_library/src/assign_library_plugin.rs index c748e03f381d..91017ba1e10a 100644 --- a/crates/rspack_plugin_library/src/assign_library_plugin.rs +++ b/crates/rspack_plugin_library/src/assign_library_plugin.rs @@ -4,6 +4,7 @@ use std::sync::LazyLock; use regex::Regex; use rspack_collections::DatabaseItem; use rspack_core::rspack_sources::SourceExt; +use rspack_core::ExportInfoProvided; use rspack_core::{ get_entry_runtime, property_access, ApplyContext, BoxModule, ChunkUkey, CodeGenerationDataTopLevelDeclarations, CompilationAdditionalChunkRuntimeRequirements, @@ -242,7 +243,7 @@ fn render_startup( &self, compilation: &Compilation, chunk_ukey: &ChunkUkey, - _module: &ModuleIdentifier, + module: &ModuleIdentifier, render_source: &mut RenderSource, ) -> Result<()> { let Some(options) = self.get_options_for_chunk(compilation, chunk_ukey)? else { @@ -258,6 +259,24 @@ fn render_startup( .unwrap_or_default(); if matches!(self.options.unnamed, Unnamed::Static) { let export_target = access_with_init(&full_name_resolved, self.options.prefix.len(), true); + let module_graph = compilation.get_module_graph(); + let exports_info = module_graph.get_exports_info(module); + for export_info in exports_info.ordered_exports(&module_graph) { + if matches!( + export_info.provided(&module_graph), + Some(ExportInfoProvided::False) + ) { + continue; + } + let export_info_name = export_info + .name(&module_graph) + .expect("should have name") + .to_string(); + let name_access = property_access([export_info_name], 0); + source.add(RawStringSource::from(format!( + "{export_target}{name_access} = __webpack_exports__{export_access}{name_access};\n" + ))); + } source.add(RawStringSource::from(format!( "Object.defineProperty({export_target}, '__esModule', {{ value: true }});\n", ))); From b688c62e3c9f4a0c1df36954ae981479e02801f0 Mon Sep 17 00:00:00 2001 From: hai-x Date: Wed, 19 Feb 2025 22:55:01 +0800 Subject: [PATCH 2/3] test: commonjs static --- .../configCases/library/cjs-static/index.js | 10 ++++++++++ .../configCases/library/cjs-static/webpack.config.js | 7 +++++++ 2 files changed, 17 insertions(+) create mode 100644 tests/webpack-test/configCases/library/cjs-static/index.js create mode 100644 tests/webpack-test/configCases/library/cjs-static/webpack.config.js diff --git a/tests/webpack-test/configCases/library/cjs-static/index.js b/tests/webpack-test/configCases/library/cjs-static/index.js new file mode 100644 index 000000000000..a8241359e396 --- /dev/null +++ b/tests/webpack-test/configCases/library/cjs-static/index.js @@ -0,0 +1,10 @@ +const fs = require("fs") +export const foo1 = () => {} +export const foo2 = () => {} +const bar = "bar"; +export default bar + +it("should success compile and work",()=>{ + const output = fs.readFileSync(__filename).toString(); + expect(output.match(/exports(\[|\.)/g).length).toBe(3) +}) diff --git a/tests/webpack-test/configCases/library/cjs-static/webpack.config.js b/tests/webpack-test/configCases/library/cjs-static/webpack.config.js new file mode 100644 index 000000000000..68425c7fa2da --- /dev/null +++ b/tests/webpack-test/configCases/library/cjs-static/webpack.config.js @@ -0,0 +1,7 @@ +/** @type {import("../../../../").Configuration} */ +module.exports = { + target: "node", + output: { + library: { type: "commonjs-static" } + } +}; From fc554f55655a9723bba15363d472e52e77083938 Mon Sep 17 00:00:00 2001 From: hai-x Date: Mon, 24 Feb 2025 19:56:39 +0800 Subject: [PATCH 3/3] test: fix --- .../library/1-use-library/webpack.config.js | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/tests/webpack-test/configCases/library/1-use-library/webpack.config.js b/tests/webpack-test/configCases/library/1-use-library/webpack.config.js index 59a4a3338e68..5587516f7498 100644 --- a/tests/webpack-test/configCases/library/1-use-library/webpack.config.js +++ b/tests/webpack-test/configCases/library/1-use-library/webpack.config.js @@ -314,24 +314,23 @@ module.exports = (env, { testPath }) => [ }) ] }, - // TODO: amd esm import exports presence check - // { - // resolve: { - // alias: { - // library: path.resolve( - // testPath, - // "../0-create-library/commonjs-static-external.js" - // ), - // external: path.resolve(__dirname, "node_modules/external.js") - // } - // }, - // plugins: [ - // new webpack.DefinePlugin({ - // NAME: JSON.stringify("commonjs-static with external"), - // TEST_EXTERNAL: true - // }) - // ] - // }, + { + resolve: { + alias: { + library: path.resolve( + testPath, + "../0-create-library/commonjs-static-external.js" + ), + external: path.resolve(__dirname, "node_modules/external.js") + } + }, + plugins: [ + new webpack.DefinePlugin({ + NAME: JSON.stringify("commonjs-static with external"), + TEST_EXTERNAL: true + }) + ] + }, { resolve: { alias: {