我正在编写一个带有一些 WebPack 插件的 Angular Builder,我遇到的问题是我的块在构建后包括以下自执行子模块:==> [[0, "runtime"]]]);
(window["webpackJsonp"] = window["webpackJsonp"] || []).push([["chunk-name"],{
0: (function(module, exports, __webpack_require__) {
module.exports = __webpack_require__("chunk1-main-module");
}),
"chunk1-main-module": (function(module, exports, __webpack_require__) {
//... exports of main modules...
}),
"other-modules": (function(module, exports, __webpack_require__) {
//... exports of main module...
})
},[[0, "runtime"]]]);
在所有情况下,我都需要从延迟模块列表中删除运行时块,使其看起来像 ==> [[0]]]);
此外,下面的代码能够通过 selfExecute 标记使块不是自执行的,如果它是真的,它将 chunk.entryModule 分配给 undefined。
export class ChunkIdPlugin {
constructor(private selfExecute: boolean = false) { }
public apply = (compiler: Compiler): void =>
compiler.hooks.compilation.tap("ChunkIdPlugin", this.beforeChunkIds);
private beforeChunkIds = (compilation: compilation.Compilation): void =>
compilation.hooks.beforeChunkIds.tap("ChunkIdPlugin", this.iterateChunks);
private iterateChunks = (chunks: compilation.Chunk[]): void => {
const chunk = chunks.find(_ => _.name === 'main');
if (!chunk) return;
chunk.id = this.chunkId as any;
// If scenario demand that chunk doesn't self-execute assign the chunk.entryModule to undefined
(!this.selfExecute || chunk._groups.size < 1) && (chunk.entryModule = undefined);
// Loop through chunks and remove runtime chunk from compilation
for (let group of chunk._groups) {
const chunk = group.chunks.find(_ => _.name === 'runtime');
if (chunk) { chunk.remove(); break; }
}
}
}
在需要自执行的多个块上运行此插件后,WebPack 运行时将有多个 ID 为“0”的块,这将导致冲突。
所以我的问题是如何修改这个插件以使延迟模块指向特定的子模块而不是“0”以避免冲突,如下所示:注意最后一行 ==> },[["chunk1-main-module "]]]); 而不是“0”
(window["webpackJsonp"] = window["webpackJsonp"] || []).push([["chunk-name"],{
0: (function(module, exports, __webpack_require__) {
module.exports = __webpack_require__("chunk1-main-module");
}),
"chunk1-main-module": (function(module, exports, __webpack_require__) {
//... exports of main module...
})
},[["chunk1-main-module"]]]);