

createChunkAssets 是 compilation 实例方法

const manifest = template.getRenderManifest({ 配置 })file = this.getPath(filenameTemplate, fileManifest.pathOptions);source = fileManifest.render();this.assets[file] = source;this.hooks.chunkAsset.call(chunk, file);


source = fileManifest.render(); 执行到 MainTemplate 的 render 实例方法中,这个方法通过 render hook 生成一个 source, source.children 数组中可以看出是与 webpack 最终生成代码类似的内容了,不过其中文件 module 用了 // CONCATENATED MODULE 注释标识,源码也在 ReplaceSource 实例对象中

MainTemplate.js - constructor 实现 this.hooks.render
this.hooks.render - 对 source 进行 add 增加固定格式的代码,最后通过 modules hook 增加对应的 module 代码ReplaceSource 实例和 CONCATENATED MODULE 标记

this.hooks.modules - 对 module 进行解析,生成包裹源码的 ReplaceSource 实例
在 JavascriptModulesPlugin 中实现,调用静态方法 Template.renderChunkModules


Template.renderChunkModules 中的 allModule
allModule 数组内容就是每个 module 对应的 CONCATENATED MODULE 的标记和包裹 module 源码的 ReplaceSource

通过 module.source 创立 moduleSource, 这里就是我们需要的 module 代码内容, 最后打包返回到 compoilation.js 赋值给对应的 assets[file]


遍历 modulesWithInfo 组装 module 代码 ReplaceSource 实例标记 CONCATENATED MODULE


compilateion.seal:
1执行 hooks seal,运行大量 hooks 用于编写阻拦插件
2执行 this.hooks.optimizeTree,生成代码 this.createChunkAssets(),和压缩代码this.hooks.optimizeChunkAsset
3结束 seal 执行 hooks.afterSeal
生成代码 createChunkAssets
1读取 webpack 的输出配置 outputOptions
2生成 chunk 映射 module 的 manifest, 并增加通过 module 生成 chunk 代码的 render 函数
3manifest 的 render 函数, MainTemplate 的 render 实例方法生成 source 源码
4createChunkAssets 生成 source 源码结束返回 compilation 上赋值给 assets[file]