diff --git a/lib/CompatibilityPlugin.js b/lib/CompatibilityPlugin.js index 05f39249d..1a92e400d 100644 --- a/lib/CompatibilityPlugin.js +++ b/lib/CompatibilityPlugin.js @@ -147,6 +147,9 @@ class CompatibilityPlugin { range: /** @type {Range} */ (pattern.range) } }); + if (!parser.scope.topLevelScope) { + return true; + } }); parser.hooks.pattern .for(RuntimeGlobals.exports) diff --git a/test/configCases/module/consume-webpack-runtime/index.js b/test/configCases/module/consume-webpack-runtime/index.js index 73566214f..5be46d309 100644 --- a/test/configCases/module/consume-webpack-runtime/index.js +++ b/test/configCases/module/consume-webpack-runtime/index.js @@ -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); } { diff --git a/test/configCases/module/consume-webpack-runtime/runtime-multiple-nested.js b/test/configCases/module/consume-webpack-runtime/runtime-multiple-nested.js new file mode 100644 index 000000000..a0397c4ce --- /dev/null +++ b/test/configCases/module/consume-webpack-runtime/runtime-multiple-nested.js @@ -0,0 +1,6 @@ +function __webpack_require__() { + var __webpack_require__ = { foo: 42 }; + return __webpack_require__; +} + +export default __webpack_require__;