move Module.id into ChunkGraph

remove disconnect and unseal from Module
This commit is contained in:
Tobias Koppers 2018-08-28 11:56:48 +02:00
parent 02b15a1b46
commit bad9d8a271
28 changed files with 126 additions and 98 deletions

View File

@ -43,7 +43,10 @@ class AmdMainTemplatePlugin {
);
const externalsArguments = externals
.map(
m => `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(`${m.id}`)}__`
m =>
`__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(
`${chunkGraph.getModuleId(m)}`
)}__`
)
.join(", ");

View File

@ -79,6 +79,8 @@ class ChunkGraphModule {
this.hash = undefined;
/** @type {string} */
this.renderedHash = undefined;
/** @type {string | number} */
this.id = null;
}
}
@ -390,8 +392,9 @@ class ChunkGraph {
array = [];
chunkModuleIdMap[asyncChunk.id] = array;
}
array.push(module.id);
chunkModuleHashMap[module.id] = this.getRenderedModuleHash(module);
const moduleId = this.getModuleId(module);
array.push(moduleId);
chunkModuleHashMap[moduleId] = this.getRenderedModuleHash(module);
}
}
if (array !== undefined) {
@ -711,6 +714,25 @@ class ChunkGraph {
chunkGroup._blocks.clear();
}
/**
* @param {Module} module the module
* @returns {string | number} the id of the module
*/
getModuleId(module) {
const cgm = this._getChunkGraphModule(module);
return cgm.id;
}
/**
* @param {Module} module the module
* @param {string | number} id the id of the module
* @returns {void}
*/
setModuleId(module, id) {
const cgm = this._getChunkGraphModule(module);
cgm.id = id;
}
/**
* @param {Module} module the module
* @returns {string} hash

View File

@ -484,7 +484,6 @@ class Compilation {
}
if (!rebuild) {
cacheModule.disconnect();
this._modules.set(identifier, cacheModule);
this.modules.push(cacheModule);
for (const err of cacheModule.errors) {
@ -1048,9 +1047,6 @@ class Compilation {
this.namedChunkGroups.clear();
this.additionalChunkAssets.length = 0;
this.assets = {};
for (const module of this.modules) {
module.unseal();
}
this.moduleGraph.removeAllModuleAttributes();
}
@ -1906,8 +1902,9 @@ class Compilation {
const modules1 = this.modules;
for (let indexModule1 = 0; indexModule1 < modules1.length; indexModule1++) {
const module1 = modules1[indexModule1];
if (module1.id !== null) {
usedIds.add(module1.id);
const moduleId = chunkGraph.getModuleId(module1);
if (moduleId !== null) {
usedIds.add(moduleId);
}
}
@ -1935,11 +1932,11 @@ class Compilation {
const module2 = modules2[indexModule2];
// Module that are not in any chunk don't need ids
if (chunkGraph.getNumberOfModuleChunks(module2) === 0) continue;
if (module2.id === null) {
if (chunkGraph.getModuleId(module2) === null) {
if (unusedIds.length > 0) {
module2.id = unusedIds.pop();
chunkGraph.setModuleId(module2, unusedIds.pop());
} else {
module2.id = nextFreeModuleId++;
chunkGraph.setModuleId(module2, nextFreeModuleId++);
}
}
}
@ -2333,7 +2330,7 @@ class Compilation {
const modules = this.modules;
for (let indexModule = 0; indexModule < modules.length; indexModule++) {
const moduleId = modules[indexModule].id;
const moduleId = chunkGraph.getModuleId(modules[indexModule]);
if (moduleId === null) continue;
if (usedIds.has(moduleId)) {
throw new Error(`checkConstraints: duplicate module id ${moduleId}`);

View File

@ -345,7 +345,8 @@ class ContextModule extends Module {
return a.userRequest < b.userRequest ? -1 : 1;
})
.reduce((map, dep) => {
map[dep.userRequest] = moduleGraph.getModule(dep).id;
const module = moduleGraph.getModule(dep);
map[dep.userRequest] = chunkGraph.getModuleId(module);
return map;
}, Object.create(null));
}
@ -376,7 +377,7 @@ class ContextModule extends Module {
.sort((a, b) => comparator(a.module, b.module))
.reduce((map, { dependency: dep, module }) => {
const exportsType = module.buildMeta && module.buildMeta.exportsType;
const id = module.id;
const id = chunkGraph.getModuleId(module);
if (!exportsType) {
map[id] = this.options.namespaceObject === "strict" ? 1 : 7;
hasNonHarmony = true;
@ -669,9 +670,11 @@ module.exports = webpackAsyncContext;`;
if (chunks.length !== 1) {
hasMultipleOrNoChunks = true;
}
const arrayStart = [moduleGraph.getModule(item.dependency).id];
const module = moduleGraph.getModule(item.dependency);
const moduleId = chunkGraph.getModuleId(module);
const arrayStart = [moduleId];
if (typeof fakeMap === "object") {
arrayStart.push(fakeMap[moduleGraph.getModule(item.dependency).id]);
arrayStart.push(fakeMap[moduleId]);
}
map[item.userRequest] = arrayStart.concat(
chunks.map(chunk => chunk.id)
@ -745,7 +748,7 @@ webpackEmptyAsyncContext.id = ${JSON.stringify(id)};`;
* @returns {string} the source code
*/
getSourceString(asyncMode, { runtimeTemplate, chunkGraph }) {
const id = this.id;
const id = chunkGraph.getModuleId(this);
if (asyncMode === "lazy") {
if (this.blocks && this.blocks.length > 0) {
return this.getLazySource(this.blocks, id, chunkGraph);

View File

@ -63,14 +63,6 @@ class DependenciesBlock {
for (const block of this.blocks) block.updateHash(hash, chunkGraph);
}
disconnect() {
for (const block of this.blocks) block.disconnect();
}
unseal() {
for (const block of this.blocks) block.unseal();
}
/**
* @param {DependencyFilterFunction} filter filter function for dependencies, gets passed all dependency ties from current instance
* @returns {boolean} returns boolean for filter

View File

@ -116,7 +116,7 @@ class Dependency {
updateHash(hash, chunkGraph) {
const module = chunkGraph.moduleGraph.getModule(this);
if (module) {
hash.update(module.id + "");
hash.update(chunkGraph.getModuleId(module) + "");
}
}

View File

@ -88,7 +88,7 @@ class EvalSourceMapDevToolModuleTemplatePlugin {
);
sourceMap.sources = moduleFilenames;
sourceMap.sourceRoot = options.sourceRoot || "";
const moduleId = module.id;
const moduleId = chunkGraph.getModuleId(module);
sourceMap.file = `${moduleId}.js`;
const footer =

View File

@ -191,7 +191,7 @@ class ExternalModule extends Module {
case "umd":
case "umd2":
return getSourceForAmdOrUmdExternal(
this.id,
chunkGraph.getModuleId(this),
this.isOptional(moduleGraph),
request,
runtimeTemplate

View File

@ -32,8 +32,9 @@ class HashedModuleIdsPlugin {
compilation.hooks.beforeModuleIds.tap(
"HashedModuleIdsPlugin",
modules => {
const chunkGraph = compilation.chunkGraph;
for (const module of modules) {
if (module.id === null) {
if (chunkGraph.getModuleId(module) === null) {
const id = module.libIdent({
context: this.options.context || compiler.options.context
});
@ -44,7 +45,7 @@ class HashedModuleIdsPlugin {
let len = options.hashDigestLength;
while (usedIds.has(hashId.substr(0, len))) len++;
const moduleId = hashId.substr(0, len);
module.id = moduleId;
chunkGraph.setModuleId(module, moduleId);
usedIds.add(moduleId);
}
}

View File

@ -227,7 +227,7 @@ module.exports = class HotModuleReplacementPlugin {
chunk,
compareModulesById(chunkGraph)
),
m => m.id
m => chunkGraph.getModuleId(m)
);
}
}
@ -303,7 +303,7 @@ module.exports = class HotModuleReplacementPlugin {
for (const module of chunkGraph.getChunkModulesIterable(
currentChunk
)) {
allModules.add(module.id);
allModules.add(chunkGraph.getModuleId(module));
}
const removedModules = records.chunkModuleIds[chunkId].filter(
id => !allModules.has(id)

View File

@ -68,7 +68,7 @@ class LibManifestPlugin {
return {
ident,
data: {
id: module.id,
id: chunkGraph.getModuleId(module),
buildMeta: module.buildMeta
}
};

View File

@ -141,12 +141,13 @@ module.exports = class MainTemplate {
chunk
)) {
const mayReturn = --i === 0 ? "return " : "";
const moduleId = chunkGraph.getModuleId(entryModule);
buf.push(
`${mayReturn}${this.renderRequireFunctionForModule(
hash,
chunk,
JSON.stringify(entryModule.id)
)}(${this.requireFn}.s = ${JSON.stringify(entryModule.id)});`
JSON.stringify(moduleId)
)}(${this.requireFn}.s = ${JSON.stringify(moduleId)});`
);
}
}

View File

@ -94,16 +94,25 @@ class Module extends DependenciesBlock {
/** @type {object} */
this.buildInfo = undefined;
// Info from Compilation (per Compilation)
/** @type {number|string} */
this.id = null;
/** @type {boolean} */
this.useSourceMap = false;
}
// TODO remove in webpack 6
// BACKWARD-COMPAT START
get id() {
return ChunkGraph.getChunkGraphForModule(this, "Module.id").getModuleId(
this
);
}
set id(value) {
ChunkGraph.getChunkGraphForModule(this, "Module.id").setModuleId(
this,
value
);
}
/**
* @returns {string} the hash of the module
*/
@ -284,24 +293,6 @@ class Module extends DependenciesBlock {
return (this.buildInfo && this.buildInfo.moduleArgument) || "module";
}
/**
* disconnect the module from the graph
* @returns {void}
*/
disconnect() {
this.id = null;
super.disconnect();
}
/**
* @returns {void}
*/
unseal() {
this.id = null;
super.unseal();
}
/**
* @param {ModuleGraph} moduleGraph the module graph
* @returns {boolean} true, if the module is optional
@ -469,7 +460,7 @@ class Module extends DependenciesBlock {
* @returns {void}
*/
updateHash(hash, chunkGraph) {
hash.update(`${this.id}`);
hash.update(`${chunkGraph.getModuleId(this)}`);
const usedExports = chunkGraph.moduleGraph.getUsedExports(this);
if (typeof usedExports === "boolean") {
hash.update(JSON.stringify(usedExports));
@ -489,7 +480,6 @@ class Module extends DependenciesBlock {
this.blocks.length = 0;
this.buildMeta = undefined;
this.buildInfo = undefined;
this.disconnect();
}
/**

View File

@ -87,7 +87,7 @@ ModuleFilenameHelpers.createFilename = (
} else {
shortIdentifier = module.readableIdentifier(requestShortener);
identifier = requestShortener.shorten(module.identifier());
moduleId = module.id;
moduleId = chunkGraph.getModuleId(module);
absoluteResourcePath = module
.identifier()
.split("!")

View File

@ -22,16 +22,17 @@ class NamedModulesPlugin {
apply(compiler) {
compiler.hooks.compilation.tap("NamedModulesPlugin", compilation => {
compilation.hooks.beforeModuleIds.tap("NamedModulesPlugin", modules => {
const chunkGraph = compilation.chunkGraph;
const namedModules = new Map();
const context = this.options.context || compiler.options.context;
for (const module of modules) {
let moduleId = module.id;
let moduleId = chunkGraph.getModuleId(module);
if (moduleId === null) {
const id = module.libIdent({ context });
if (id) {
moduleId = id;
module.id = id;
chunkGraph.setModuleId(module, id);
}
}
@ -49,9 +50,12 @@ class NamedModulesPlugin {
if (namedModule.length > 1) {
for (const module of namedModule) {
const requestShortener = new RequestShortener(context);
module.id = `${module.id}?${getHash(
requestShortener.shorten(module.identifier())
)}`;
chunkGraph.setModuleId(
module,
`${chunkGraph.getModuleId(module)}?${getHash(
requestShortener.shorten(module.identifier())
)}`
);
}
}
}

View File

@ -55,11 +55,12 @@ class RecordIdsPlugin {
* @returns {void}
*/
(modules, records) => {
const chunkGraph = compilation.chunkGraph;
if (!records.modules) records.modules = {};
if (!records.modules.byIdentifier) records.modules.byIdentifier = {};
if (!records.modules.usedIds) records.modules.usedIds = {};
for (const module of modules) {
const moduleId = module.id;
const moduleId = chunkGraph.getModuleId(module);
if (typeof moduleId !== "number") continue;
const identifier = portableIds
? identifierUtils.makePathsRelative(
@ -83,10 +84,11 @@ class RecordIdsPlugin {
(modules, records) => {
if (!records.modules) return;
if (records.modules.byIdentifier) {
const chunkGraph = compilation.chunkGraph;
/** @type {Set<number>} */
const usedIds = new Set();
for (const module of modules) {
const moduleId = module.id;
const moduleId = chunkGraph.getModuleId(module);
if (moduleId !== null) continue;
const identifier = portableIds
? identifierUtils.makePathsRelative(
@ -99,7 +101,7 @@ class RecordIdsPlugin {
if (id === undefined) continue;
if (usedIds.has(id)) continue;
usedIds.add(id);
module.id = id;
chunkGraph.setModuleId(module, id);
}
}
if (Array.isArray(records.modules.usedIds)) {

View File

@ -117,7 +117,7 @@ module.exports = class RuntimeTemplate {
* @returns {string} the code
*/
weakError({ module, chunkGraph, request, idExpr, type }) {
const moduleId = module.id;
const moduleId = chunkGraph.getModuleId(module);
const errorMessage =
moduleId === null
? JSON.stringify("Module is not available (weak dependency)")
@ -155,7 +155,7 @@ module.exports = class RuntimeTemplate {
request
});
}
const moduleId = module.id;
const moduleId = chunkGraph.getModuleId(module);
if (moduleId === null) {
if (weak) {
return "null /* weak dependency, without id */";
@ -181,7 +181,7 @@ module.exports = class RuntimeTemplate {
request
});
}
const moduleId = module.id;
const moduleId = chunkGraph.getModuleId(module);
if (moduleId === null) {
if (weak) {
// only weak referenced modules don't get an id
@ -237,7 +237,7 @@ module.exports = class RuntimeTemplate {
request
});
}
if (module.id === null) {
if (chunkGraph.getModuleId(module) === null) {
if (weak) {
// only weak referenced modules don't get an id
// we can always emit an error emitting code here
@ -301,7 +301,7 @@ module.exports = class RuntimeTemplate {
request
});
}
const moduleId = module.id;
const moduleId = chunkGraph.getModuleId(module);
if (moduleId === null) {
if (weak) {
// only weak referenced modules don't get an id
@ -324,7 +324,7 @@ module.exports = class RuntimeTemplate {
});
let getModuleFunction;
let idExpr = JSON.stringify(module.id);
let idExpr = JSON.stringify(chunkGraph.getModuleId(module));
const comment = this.comment({
request
});
@ -411,7 +411,7 @@ module.exports = class RuntimeTemplate {
request
});
}
if (module.id === null) {
if (chunkGraph.getModuleId(module) === null) {
if (weak) {
// only weak referenced modules don't get an id
// we can always emit an error emitting code here

View File

@ -553,7 +553,7 @@ class Stats {
}
path.reverse();
const obj = {
id: module.id,
id: chunkGraph.getModuleId(module),
identifier: module.identifier(),
name: module.readableIdentifier(requestShortener),
index: moduleGraph.getPreOrderIndex(module),
@ -569,12 +569,12 @@ class Stats {
chunk => chunk.id
),
issuer: issuer && issuer.identifier(),
issuerId: issuer && issuer.id,
issuerId: issuer && chunkGraph.getModuleId(issuer),
issuerName: issuer && issuer.readableIdentifier(requestShortener),
issuerPath:
issuer &&
path.map(module => ({
id: module.id,
id: chunkGraph.getModuleId(module),
identifier: module.identifier(),
name: module.readableIdentifier(requestShortener),
profile: fnProfile(moduleGraph.getProfile(module))
@ -618,7 +618,9 @@ class Stats {
.map(reason => {
const depAsAny = /** @type {TODO} */ (reason.dependency);
const obj = {
moduleId: reason.originModule ? reason.originModule.id : null,
moduleId: reason.originModule
? chunkGraph.getModuleId(reason.originModule)
: null,
moduleIdentifier: reason.originModule
? reason.originModule.identifier()
: null,
@ -737,7 +739,9 @@ class Stats {
obj.origins = Array.from(chunk.groupsIterable, g => g.origins)
.reduce((a, b) => a.concat(b), [])
.map(origin => ({
moduleId: origin.module ? origin.module.id : undefined,
moduleId: origin.module
? chunkGraph.getModuleId(origin.module)
: undefined,
module: origin.module ? origin.module.identifier() : "",
moduleIdentifier: origin.module ? origin.module.identifier() : "",
moduleName: origin.module

View File

@ -256,7 +256,7 @@ class Template {
/** @type {{id: string|number, source: Source|string}[]} */
const allModules = modules.map(module => {
return {
id: module.id,
id: chunkGraph.getModuleId(module),
source: moduleTemplate.render(module, renderContext)
};
});

View File

@ -83,7 +83,9 @@ const replacePathVariables = (path, data) => {
? chunk.contentHashWithLength[contentHashType]
: undefined);
const module = data.module;
const moduleId = module && module.id;
const moduleId =
module &&
(module instanceof Module ? chunkGraph.getModuleId(module) : module.id);
const moduleHash =
module &&
(module instanceof Module

View File

@ -186,7 +186,9 @@ class UmdMainTemplatePlugin {
return modules
.map(
m =>
`__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(`${m.id}`)}__`
`__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(
`${chunkGraph.getModuleId(m)}`
)}__`
)
.join(", ");
};

View File

@ -58,7 +58,7 @@ ModuleDecoratorDependency.Template = class ModuleDecoratorDependencyTemplate ext
})}(${originModule.moduleArgument});\n`,
InitFragment.STAGE_PROVIDES,
0,
`module decorator ${originModule.id}`
`module decorator ${chunkGraph.getModuleId(originModule)}`
)
];
}

View File

@ -63,8 +63,8 @@ class ChunkModuleIdRangePlugin {
let currentId = options.start || 0;
for (let i = 0; i < chunkModules.length; i++) {
const m = chunkModules[i];
if (m.id === null) {
m.id = currentId++;
if (chunkGraph.getModuleId(m) === null) {
chunkGraph.setModuleId(m, currentId++);
}
if (options.end && currentId > options.end) break;
}

View File

@ -1175,7 +1175,7 @@ class ConcatenatedModule extends Module {
);
result.add(
`var ${info.name} = __webpack_require__(${JSON.stringify(
info.module.id
chunkGraph.getModuleId(info.module)
)});\n`
);
if (info.interopNamespaceObjectUsed) {
@ -1255,7 +1255,7 @@ class ConcatenatedModule extends Module {
info.module.updateHash(hash, chunkGraph);
break;
case "external":
hash.update(`${info.module.id}`);
hash.update(`${chunkGraph.getModuleId(info.module)}`);
break;
}
}

View File

@ -63,7 +63,7 @@ exports.compareModulesByIdentifier = (a, b) => {
* @returns {-1|0|1} compare result
*/
const compareModulesById = (chunkGraph, a, b) => {
return compareIds(a.id, b.id);
return compareIds(chunkGraph.getModuleId(a), chunkGraph.getModuleId(b));
};
/** @type {ParamizedComparator<ChunkGraph, Module>} */
exports.compareModulesById = createCachedParamizedComparator(
@ -145,7 +145,7 @@ exports.compareModulesByIndexOrIdentifier = createCachedParamizedComparator(
* @returns {-1|0|1} compare result
*/
const compareModulesByIdOrIdentifier = (chunkGraph, a, b) => {
const cmp1 = compareIds(a.id, b.id);
const cmp1 = compareIds(chunkGraph.getModuleId(a), chunkGraph.getModuleId(b));
if (cmp1 !== 0) return cmp1;
const cmp2 = compareIds(a.identifier(), b.identifier());
return cmp2;

View File

@ -63,7 +63,7 @@ const generateImportObject = (chunkGraph, module, mangle) => {
if (direct) {
const instanceVar = `m${waitForInstances.size}`;
waitForInstances.set(instanceVar, importedModule.id);
waitForInstances.set(instanceVar, chunkGraph.getModuleId(importedModule));
properties.push({
module,
name,
@ -74,7 +74,9 @@ const generateImportObject = (chunkGraph, module, mangle) => {
(param, k) => "p" + k + param.valtype
);
const mod = `installedModules[${JSON.stringify(importedModule.id)}]`;
const mod = `installedModules[${JSON.stringify(
chunkGraph.getModuleId(importedModule)
)}]`;
const func = `${mod}.exports[${JSON.stringify(usedName)}]`;
properties.push({
@ -126,12 +128,13 @@ const generateImportObject = (chunkGraph, module, mangle) => {
];
}
const moduleIdStringified = JSON.stringify(chunkGraph.getModuleId(module));
if (waitForInstances.size === 1) {
const moduleId = Array.from(waitForInstances.values())[0];
const promise = `installedWasmModules[${JSON.stringify(moduleId)}]`;
const variable = Array.from(waitForInstances.keys())[0];
return Template.asString([
`${JSON.stringify(module.id)}: function() {`,
`${moduleIdStringified}: function() {`,
Template.indent([
`return promiseResolve().then(function() { return ${promise}; }).then(function(${variable}) {`,
Template.indent(importObject),
@ -149,7 +152,7 @@ const generateImportObject = (chunkGraph, module, mangle) => {
(name, i) => `${name} = array[${i}]`
).join(", ");
return Template.asString([
`${JSON.stringify(module.id)}: function() {`,
`${moduleIdStringified}: function() {`,
Template.indent([
`return promiseResolve().then(function() { return Promise.all([${promises}]); }).then(function(array) {`,
Template.indent([`var ${variables};`, ...importObject]),
@ -159,7 +162,7 @@ const generateImportObject = (chunkGraph, module, mangle) => {
]);
} else {
return Template.asString([
`${JSON.stringify(module.id)}: function() {`,
`${moduleIdStringified}: function() {`,
Template.indent(importObject),
"},"
]);

View File

@ -100,7 +100,9 @@ class WebAssemblyModulesPlugin {
chunkGraph,
module
},
identifier: `webassemblyModule${module.id}`,
identifier: `webassemblyModule${chunkGraph.getModuleId(
module
)}`,
hash: chunkGraph.getModuleHash(module)
});
}

View File

@ -19,7 +19,7 @@ exports.getEntryInfo = (chunkGraph, chunk) => {
return Array.from(
chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
).map(([module, chunkGroup]) =>
[module.id].concat(
[chunkGraph.getModuleId(module)].concat(
chunkGroup.chunks.filter(c => c !== chunk).map(c => c.id)
)
);