remove module.id and loaded shortcuts and only include them when used

This commit is contained in:
Tobias Koppers 2019-12-04 14:12:16 +01:00
parent a8a8df5fc6
commit ab8a768cac
5 changed files with 54 additions and 39 deletions

View File

@ -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;"
])};`
]);

View File

@ -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
*/

View File

@ -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 => {

View File

@ -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(

View File

@ -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;"
]);