From 5cc9bae8d31e6bd5cf8dbfd5c42bb276f6c96cb4 Mon Sep 17 00:00:00 2001 From: Fy <1114550440@qq.com> Date: Tue, 31 Dec 2024 18:21:29 +0800 Subject: [PATCH] fix: transform nested webpack require with innerGraph enabled (#8908) --- .../src/parser_plugin/compatibility_plugin.rs | 16 +++++++++++++--- .../parsing/nested-webpack-exports/index.js | 1 + .../parsing/nested-webpack-exports/lib.js | 8 ++++++++ .../parsing/nested-webpack-exports/lib2.js | 1 + .../nested-webpack-exports/rspack.config.js | 7 +++++++ 5 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 packages/rspack-test-tools/tests/configCases/parsing/nested-webpack-exports/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/parsing/nested-webpack-exports/lib.js create mode 100644 packages/rspack-test-tools/tests/configCases/parsing/nested-webpack-exports/lib2.js create mode 100644 packages/rspack-test-tools/tests/configCases/parsing/nested-webpack-exports/rspack.config.js diff --git a/crates/rspack_plugin_javascript/src/parser_plugin/compatibility_plugin.rs b/crates/rspack_plugin_javascript/src/parser_plugin/compatibility_plugin.rs index ae0860baf757..6466f5b563dd 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/compatibility_plugin.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/compatibility_plugin.rs @@ -98,9 +98,9 @@ impl JavascriptParserPlugin for CompatibilityPlugin { decl: &swc_core::ecma::ast::VarDeclarator, _statement: &swc_core::ecma::ast::VarDecl, ) -> Option { - if let Some(ident) = decl.name.as_ident() - && ident.sym.as_str() == RuntimeGlobals::REQUIRE.name() - { + let ident = decl.name.as_ident()?; + + if ident.sym.as_str() == RuntimeGlobals::REQUIRE.name() { let start = ident.span().real_lo(); let end = ident.span().real_hi(); self.tag_nested_require_data( @@ -111,7 +111,17 @@ impl JavascriptParserPlugin for CompatibilityPlugin { end, ); return Some(true); + } else if ident.sym == RuntimeGlobals::EXPORTS.name() { + self.tag_nested_require_data( + parser, + ident.sym.to_string(), + "__nested_webpack_exports__".to_string(), + ident.span().real_lo(), + ident.span().real_hi(), + ); + return Some(true); } + None } diff --git a/packages/rspack-test-tools/tests/configCases/parsing/nested-webpack-exports/index.js b/packages/rspack-test-tools/tests/configCases/parsing/nested-webpack-exports/index.js new file mode 100644 index 000000000000..ee2cdbd3ef5f --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/parsing/nested-webpack-exports/index.js @@ -0,0 +1 @@ +require("./lib"); diff --git a/packages/rspack-test-tools/tests/configCases/parsing/nested-webpack-exports/lib.js b/packages/rspack-test-tools/tests/configCases/parsing/nested-webpack-exports/lib.js new file mode 100644 index 000000000000..16dc8bcb7a2c --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/parsing/nested-webpack-exports/lib.js @@ -0,0 +1,8 @@ +var __webpack_exports__ = {}; +__webpack_exports__.e = 42; + +it("rename top level (avoid override by inner graph top level symbol)", () => { + expect(__webpack_exports__.e).toBe(42); + const lib2 = require("./lib2"); + expect(lib2.a).toBe(1) +}); diff --git a/packages/rspack-test-tools/tests/configCases/parsing/nested-webpack-exports/lib2.js b/packages/rspack-test-tools/tests/configCases/parsing/nested-webpack-exports/lib2.js new file mode 100644 index 000000000000..cc798ff50da9 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/parsing/nested-webpack-exports/lib2.js @@ -0,0 +1 @@ +export const a = 1; diff --git a/packages/rspack-test-tools/tests/configCases/parsing/nested-webpack-exports/rspack.config.js b/packages/rspack-test-tools/tests/configCases/parsing/nested-webpack-exports/rspack.config.js new file mode 100644 index 000000000000..3c839f511900 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/parsing/nested-webpack-exports/rspack.config.js @@ -0,0 +1,7 @@ +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + mode: "production", + optimization: { + innerGraph: true, + } +}