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); +});