From 81b26f849cd13eedc44e7dfbb496716cfe6687c8 Mon Sep 17 00:00:00 2001 From: CPunisher <1343316114@qq.com> Date: Tue, 9 Jul 2024 13:47:55 +0800 Subject: [PATCH] fix: Add missing code for compatibility plugin (#7079) * Add missing code for compatibility plugin * Add testcase --- .../src/parser_plugin/compatibility_plugin.rs | 17 +++++++++++++++++ .../plugins/compatibility-plugin/a.js | 6 ++++++ .../plugins/compatibility-plugin/b.js | 1 + .../plugins/compatibility-plugin/index.js | 4 ++++ 4 files changed, 28 insertions(+) create mode 100644 packages/rspack-test-tools/tests/configCases/plugins/compatibility-plugin/a.js create mode 100644 packages/rspack-test-tools/tests/configCases/plugins/compatibility-plugin/b.js create mode 100644 packages/rspack-test-tools/tests/configCases/plugins/compatibility-plugin/index.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 cd66f7562dec..9197f6b78b8c 100644 --- a/crates/rspack_plugin_javascript/src/parser_plugin/compatibility_plugin.rs +++ b/crates/rspack_plugin_javascript/src/parser_plugin/compatibility_plugin.rs @@ -95,6 +95,23 @@ impl JavascriptParserPlugin for CompatibilityPlugin { }), ); return Some(true); + } else if for_name == RuntimeGlobals::REQUIRE.name() { + let low = ident.span().lo().0; + let hi = ident.span().hi().0; + parser.tag_variable( + ident.sym.to_string(), + NESTED_WEBPACK_IDENTIFIER_TAG, + Some(NestedRequireData { + name: format!("__nested_webpack_require_{low}_{hi}__"), + update: false, + loc: DependencyLocation::new( + ident.span().real_lo(), + ident.span().real_hi(), + Some(parser.source_map.clone()), + ), + }), + ); + return Some(true); } None } diff --git a/packages/rspack-test-tools/tests/configCases/plugins/compatibility-plugin/a.js b/packages/rspack-test-tools/tests/configCases/plugins/compatibility-plugin/a.js new file mode 100644 index 000000000000..0dceb3b1fdea --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/plugins/compatibility-plugin/a.js @@ -0,0 +1,6 @@ +function f(__webpack_require__) { + require('./b.js'); + return __webpack_require__ + 1 +} + +module.exports = f; \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/plugins/compatibility-plugin/b.js b/packages/rspack-test-tools/tests/configCases/plugins/compatibility-plugin/b.js new file mode 100644 index 000000000000..111a1500cb2c --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/plugins/compatibility-plugin/b.js @@ -0,0 +1 @@ +module.exports = "b" \ No newline at end of file diff --git a/packages/rspack-test-tools/tests/configCases/plugins/compatibility-plugin/index.js b/packages/rspack-test-tools/tests/configCases/plugins/compatibility-plugin/index.js new file mode 100644 index 000000000000..161c8c6d5478 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/plugins/compatibility-plugin/index.js @@ -0,0 +1,4 @@ +it("compatibility plugin", async () => { + const f = require("./a.js"); + expect(f(1)).toBe(2); +});