核心作用总结
- 该类作为 Webpack 构建系统中模块表示的核心抽象,维护了从模块定义、构建信息、优化状态到 Chunk 映射等全生命周期数据。
- 同时提供了与
ModuleGraph 和 ChunkGraph 的交互能力,是整个模块依赖图和输出资源图的核心支点。
- 插件、loader、优化器等都通过这个类实例获取模块相关信息或注入新信息。
constructor(type, context = null, layer = null) {
super();
this.type = type;
this.context = context;
this.layer = layer;
this.needId = true;
this.debugId = debugId++;
this.resolveOptions = EMPTY_RESOLVE_OPTIONS;
this.factoryMeta = undefined;
this.useSourceMap = false;
this.useSimpleSourceMap = false;
this.hot = false;
this._warnings = undefined;
this._errors = undefined;
this.buildMeta = undefined;
this.buildInfo = undefined;
this.presentationalDependencies = undefined;
this.codeGenerationDependencies = undefined;
}
get id() {
return ChunkGraph.getChunkGraphForModule(this, "Module.id", "DEP_WEBPACK_MODULE_ID").getModuleId(this);
}
set id(value) {
if (value === "") {
this.needId = false;
return;
}
ChunkGraph.getChunkGraphForModule(this, "Module.id", "DEP_WEBPACK_MODULE_ID").setModuleId(this, value);
}
get hash() {
return ChunkGraph.getChunkGraphForModule(this, "Module.hash", "DEP_WEBPACK_MODULE_HASH").getModuleHash(this, undefined);
}
get renderedHash() {
return ChunkGraph.getChunkGraphForModule(this, "Module.renderedHash", "DEP_WEBPACK_MODULE_RENDERED_HASH").getRenderedModuleHash(this, undefined);
}
get profile() {
return ModuleGraph.getModuleGraphForModule(this, "Module.profile", "DEP_WEBPACK_MODULE_PROFILE").getProfile(this);
}
set profile(value) {
ModuleGraph.getModuleGraphForModule(this, "Module.profile", "DEP_WEBPACK_MODULE_PROFILE").setProfile(this, value);
}
get index() {
return ModuleGraph.getModuleGraphForModule(this, "Module.index", "DEP_WEBPACK_MODULE_INDEX").getPreOrderIndex(this);
}
set index(value) {
ModuleGraph.getModuleGraphForModule(this, "Module.index", "DEP_WEBPACK_MODULE_INDEX").setPreOrderIndex(this, value);
}
get index2() {
return ModuleGraph.getModuleGraphForModule(this, "Module.index2", "DEP_WEBPACK_MODULE_INDEX2").getPostOrderIndex(this);
}
set index2(value) {
ModuleGraph.getModuleGraphForModule(this, "Module.index2", "DEP_WEBPACK_MODULE_INDEX2").setPostOrderIndex(this, value);
}
get depth() {
return ModuleGraph.getModuleGraphForModule(this, "Module.depth", "DEP_WEBPACK_MODULE_DEPTH").getDepth(this);
}
set depth(value) {
ModuleGraph.getModuleGraphForModule(this, "Module.depth", "DEP_WEBPACK_MODULE_DEPTH").setDepth(this, value);
}
get issuer() {
return ModuleGraph.getModuleGraphForModule(this, "Module.issuer", "DEP_WEBPACK_MODULE_ISSUER").getIssuer(this);
}
set issuer(value) {
ModuleGraph.getModuleGraphForModule(this, "Module.issuer", "DEP_WEBPACK_MODULE_ISSUER").setIssuer(this, value);
}
get usedExports() {
return ModuleGraph.getModuleGraphForModule(this, "Module.usedExports", "DEP_WEBPACK_MODULE_USED_EXPORTS").getUsedExports(this, undefined);
}
get optimizationBailout() {
return ModuleGraph.getModuleGraphForModule(this, "Module.optimizationBailout", "DEP_WEBPACK_MODULE_OPTIMIZATION_BAILOUT").getOptimizationBailout(this);
}
get optional() {
return this.isOptional(ModuleGraph.getModuleGraphForModule(this, "Module.optional", "DEP_WEBPACK_MODULE_OPTIONAL"));
}
addChunk(chunk) {
const chunkGraph = ChunkGraph.getChunkGraphForModule(this, "Module.addChunk", "DEP_WEBPACK_MODULE_ADD_CHUNK");
if (chunkGraph.isModuleInChunk(this, chunk)) return false;
chunkGraph.connectChunkAndModule(chunk, this);
return true;
}
removeChunk(chunk) {
return ChunkGraph.getChunkGraphForModule(this, "Module.removeChunk", "DEP_WEBPACK_MODULE_REMOVE_CHUNK").disconnectChunkAndModule(chunk, this);
}
isInChunk(chunk) {
return ChunkGraph.getChunkGraphForModule(this, "Module.isInChunk", "DEP_WEBPACK_MODULE_IS_IN_CHUNK").isModuleInChunk(this, chunk);
}