From 4ea491e84c77d42917422844b0e2ca72a73bb265 Mon Sep 17 00:00:00 2001 From: Wei Date: Tue, 15 Oct 2024 21:51:01 +0800 Subject: [PATCH] fix(json): only add RuntimeGlobals::MODULE for concatenation (#8126) --- crates/rspack_plugin_json/src/lib.rs | 6 +++--- .../__snapshots__/StatsTestCases.basictest.js.snap | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/rspack_plugin_json/src/lib.rs b/crates/rspack_plugin_json/src/lib.rs index 24e218776d4..5f5f6181eab 100644 --- a/crates/rspack_plugin_json/src/lib.rs +++ b/crates/rspack_plugin_json/src/lib.rs @@ -145,9 +145,6 @@ impl ParserAndGenerator for JsonParserAndGenerator { let module_graph = compilation.get_module_graph(); match generate_context.requested_source_type { SourceType::JavaScript => { - generate_context - .runtime_requirements - .insert(RuntimeGlobals::MODULE); let module = module_graph .module_by_identifier(&module.identifier()) .expect("should have module identifier"); @@ -184,6 +181,9 @@ impl ParserAndGenerator for JsonParserAndGenerator { scope.register_namespace_export(NAMESPACE_OBJECT_EXPORT); format!("var {NAMESPACE_OBJECT_EXPORT} = {json_expr}") } else { + generate_context + .runtime_requirements + .insert(RuntimeGlobals::MODULE); format!(r#"module.exports = {}"#, json_expr) }; Ok(RawSource::from(content).boxed()) diff --git a/tests/webpack-test/__snapshots__/StatsTestCases.basictest.js.snap b/tests/webpack-test/__snapshots__/StatsTestCases.basictest.js.snap index 0bacc65eb95..17b58c89b9f 100644 --- a/tests/webpack-test/__snapshots__/StatsTestCases.basictest.js.snap +++ b/tests/webpack-test/__snapshots__/StatsTestCases.basictest.js.snap @@ -449,7 +449,7 @@ chunk (runtime: main) trees.js (trees) 215 bytes [rendered] `; exports[`StatsTestCases should print correct stats for ignore-warnings 1`] = ` -"asset main.js 1.83 KiB [emitted] (name: main) +"asset main.js 928 bytes [emitted] (name: main) orphan modules 794 bytes [orphan] 10 modules ./index.js + 9 modules 794 bytes [code generated] Rspack x.x.x compiled successfully in X.23"