fix: handle nested `__webpack_require__` (#20171)

This commit is contained in:
Alexander Akait 2025-11-27 09:07:12 +03:00 committed by GitHub
parent 9abcf3fa8f
commit 579c0b2ac9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 4 deletions

View File

@ -147,6 +147,9 @@ class CompatibilityPlugin {
range: /** @type {Range} */ (pattern.range)
}
});
if (!parser.scope.topLevelScope) {
return true;
}
});
parser.hooks.pattern
.for(RuntimeGlobals.exports)

View File

@ -2,6 +2,7 @@ import { __webpack_require__ as namedUse } from './runtime-export-named'
import defaultUse from './runtime-export-default'
import { __webpack_require__ as namedDeclUse } from './runtime-export-decl'
import { __webpack_require__ as objectRequire, __webpack_exports__ as objectExport } from './runtime-single-require-and-export'
import defaultUseNested from './runtime-multiple-nested'
it("should compile and run", () => {
expect(namedUse()).toBe(42);
@ -9,13 +10,14 @@ it("should compile and run", () => {
expect(namedDeclUse()).toBe(42);
expect(objectRequire.foo).toBe(42);
expect(objectExport.foo).toBe(42);
expect(defaultUseNested().foo).toBe(42);
const path = __non_webpack_require__('path')
const fs = __non_webpack_require__('fs')
{
const content = fs.readFileSync(path.resolve(__dirname, './bundle0.js'), 'utf-8');
const NESTED_RE = /__nested_webpack_require_([^_]+)__/g;
expect(content.match(NESTED_RE).length).toBe(11);
expect(content.match(NESTED_RE).length).toBe(13);
}
{
@ -27,7 +29,7 @@ it("should compile and run", () => {
{
const content = fs.readFileSync(path.resolve(__dirname, './bundle1.js'), 'utf-8');
const NESTED_RE = /__nested_webpack_require_([^_]+)__/g;
expect(content.match(NESTED_RE).length).toBe(11);
expect(content.match(NESTED_RE).length).toBe(15);
}
{
@ -39,7 +41,7 @@ it("should compile and run", () => {
{
const content = fs.readFileSync(path.resolve(__dirname, './bundle2.js'), 'utf-8');
const NESTED_RE = /__nested_webpack_require_([^_]+)__/g;
expect(content.match(NESTED_RE).length).toBe(11);
expect(content.match(NESTED_RE).length).toBe(13);
}
{
@ -51,7 +53,7 @@ it("should compile and run", () => {
{
const content = fs.readFileSync(path.resolve(__dirname, './bundle3.js'), 'utf-8');
const NESTED_RE = /__nested_webpack_require_([^_]+)__/g;
expect(content.match(NESTED_RE).length).toBe(11);
expect(content.match(NESTED_RE).length).toBe(15);
}
{

View File

@ -0,0 +1,6 @@
function __webpack_require__() {
var __webpack_require__ = { foo: 42 };
return __webpack_require__;
}
export default __webpack_require__;