diff --git a/lib/CommonJsStuffPlugin.js b/lib/CommonJsStuffPlugin.js index b1601cc79..f0aa9e075 100644 --- a/lib/CommonJsStuffPlugin.js +++ b/lib/CommonJsStuffPlugin.js @@ -96,22 +96,24 @@ class CommonJsStuffPlugin { .tap("CommonJsStuffPlugin", expr => { parser.state.module.buildMeta.moduleConcatenationBailout = "module.loaded"; - return toConstantDependency( - parser, - `${RuntimeGlobals.module}.l`, - [RuntimeGlobals.module] - )(expr); + const dep = new RuntimeRequirementsDependency([ + RuntimeGlobals.moduleLoaded + ]); + dep.loc = expr.loc; + parser.state.module.addPresentationalDependency(dep); + return true; }); parser.hooks.expression .for("module.id") .tap("CommonJsStuffPlugin", expr => { parser.state.module.buildMeta.moduleConcatenationBailout = "module.id"; - return toConstantDependency( - parser, - `${RuntimeGlobals.module}.i`, - [RuntimeGlobals.module] - )(expr); + const dep = new RuntimeRequirementsDependency([ + RuntimeGlobals.moduleId + ]); + dep.loc = expr.loc; + parser.state.module.addPresentationalDependency(dep); + return true; }); parser.hooks.expression .for("module.exports") @@ -197,18 +199,6 @@ class HarmonyModuleDecoratorRuntimeModule extends RuntimeModule { } = ${runtimeTemplate.basicFunction("module", [ "module = Object.create(module);", "if (!module.children) module.children = [];", - "Object.defineProperty(module, 'loaded', {", - Template.indent([ - "enumerable: true,", - `get: ${runtimeTemplate.returningFunction("module.l")}` - ]), - "});", - "Object.defineProperty(module, 'id', {", - Template.indent([ - "enumerable: true,", - `get: ${runtimeTemplate.returningFunction("module.i")}` - ]), - "});", "Object.defineProperty(module, 'exports', {", Template.indent([ "enumerable: true,", @@ -239,18 +229,6 @@ class NodeModuleDecoratorRuntimeModule extends RuntimeModule { } = ${runtimeTemplate.basicFunction("module", [ "module.paths = [];", "if (!module.children) module.children = [];", - "Object.defineProperty(module, 'loaded', {", - Template.indent([ - "enumerable: true,", - `get: ${runtimeTemplate.returningFunction("module.l")}` - ]), - "});", - "Object.defineProperty(module, 'id', {", - Template.indent([ - "enumerable: true,", - `get: ${runtimeTemplate.returningFunction("module.i")}` - ]), - "});", "return module;" ])};` ]); diff --git a/lib/RuntimeGlobals.js b/lib/RuntimeGlobals.js index d5783ca6a..3efdb8009 100644 --- a/lib/RuntimeGlobals.js +++ b/lib/RuntimeGlobals.js @@ -35,6 +35,16 @@ exports.returnExportsFromRuntime = "return-exports-from-runtime"; */ exports.module = "module"; +/** + * the internal module object + */ +exports.moduleId = "module.id"; + +/** + * the internal module object + */ +exports.moduleLoaded = "module.loaded"; + /** * the bundle public path */ diff --git a/lib/RuntimePlugin.js b/lib/RuntimePlugin.js index ce9243d31..5417b3a2b 100644 --- a/lib/RuntimePlugin.js +++ b/lib/RuntimePlugin.js @@ -45,6 +45,11 @@ const GLOBALS_ON_REQUIRE = [ RuntimeGlobals.instantiateWasm ]; +const MODULE_DEPENDENCIES = { + [RuntimeGlobals.moduleLoaded]: [RuntimeGlobals.module], + [RuntimeGlobals.moduleId]: [RuntimeGlobals.module] +}; + const TREE_DEPENDENCIES = { [RuntimeGlobals.definePropertyGetters]: [RuntimeGlobals.hasOwnProperty], [RuntimeGlobals.compatGetDefaultExport]: [ @@ -88,6 +93,14 @@ class RuntimePlugin { for (const dep of deps) set.add(dep); }); } + for (const req of Object.keys(MODULE_DEPENDENCIES)) { + const deps = MODULE_DEPENDENCIES[req]; + compilation.hooks.runtimeRequirementInModule + .for(req) + .tap("RuntimePlugin", (chunk, set) => { + for (const dep of deps) set.add(dep); + }); + } compilation.hooks.runtimeRequirementInTree .for(RuntimeGlobals.definePropertyGetters) .tap("RuntimePlugin", chunk => { diff --git a/lib/dependencies/ModuleDecoratorDependency.js b/lib/dependencies/ModuleDecoratorDependency.js index 66733ee8e..fc1cc4e85 100644 --- a/lib/dependencies/ModuleDecoratorDependency.js +++ b/lib/dependencies/ModuleDecoratorDependency.js @@ -76,6 +76,8 @@ ModuleDecoratorDependency.Template = class ModuleDecoratorDependencyTemplate ext { module, chunkGraph, initFragments, runtimeRequirements } ) { const dep = /** @type {ModuleDecoratorDependency} */ (dependency); + runtimeRequirements.add(RuntimeGlobals.moduleLoaded); + runtimeRequirements.add(RuntimeGlobals.moduleId); runtimeRequirements.add(RuntimeGlobals.module); runtimeRequirements.add(dep.decorator); initFragments.push( diff --git a/lib/javascript/JavascriptModulesPlugin.js b/lib/javascript/JavascriptModulesPlugin.js index a1896ace8..d7a2b2559 100644 --- a/lib/javascript/JavascriptModulesPlugin.js +++ b/lib/javascript/JavascriptModulesPlugin.js @@ -867,6 +867,10 @@ class JavascriptModulesPlugin { : Template.asString([ "__webpack_modules__[moduleId](module, module.exports, __webpack_require__);" ]); + const needModuleId = runtimeRequirements.has(RuntimeGlobals.moduleId); + const needModuleLoaded = runtimeRequirements.has( + RuntimeGlobals.moduleLoaded + ); const content = Template.asString([ "// Check if module is in cache", "if(__webpack_module_cache__[moduleId]) {", @@ -874,7 +878,11 @@ class JavascriptModulesPlugin { "}", "// Create a new module (and put it into the cache)", "var module = __webpack_module_cache__[moduleId] = {", - Template.indent(["i: moduleId,", "l: false,", "exports: {}"]), + Template.indent([ + needModuleId ? "id: moduleId," : "// no module.id needed", + needModuleLoaded ? "loaded: false," : "// no module.loaded needed", + "exports: {}" + ]), "};", "", outputOptions.strictModuleExceptionHandling @@ -893,10 +901,14 @@ class JavascriptModulesPlugin { "// Execute the module function", moduleExecution ]), - "", - "// Flag the module as loaded", - "module.l = true;", - "", + needModuleLoaded + ? Template.asString([ + "", + "// Flag the module as loaded", + "module.loaded = true;", + "" + ]) + : "", "// Return the exports of the module", "return module.exports;" ]);