webpack4流程分析8

1.compiler

// 资源生成之后 compilation.seal 执行回调
// 负责构建资源输出
this.emitAssets(compilation, err => {})
// 再次执行回调 
this.hooks.afterEmit.callAsync(compilation, (err) => {
  return callback();
})
// 执行 emitRecords
this.emitRecords()
复制代码

2.emitAssets

// 核心就是遍历 assets 调用fs.writeFile
function emitAssets(compilation, callback) {
  // neo-async并行执行
  asyncLib.forEachLimit(
    compilation.getAssets(),
    15,
    ({ name: file, source }, callback) => {
      const writeOut = (err) => {
        // 真实路径
        const targetPath = this.outputFileSystem.join(outputPath, targetFile);
        let content = source.source();
        if (!Buffer.isBuffer(content)) {
          content = Buffer.from(content, "utf8"); // 使用buffer
        }
        // fs.writeFile
        this.outputFileSystem.writeFile(targetPath, content, (err) => {
          this.hooks.assetEmitted.callAsync(file, content, callback);
        });
      };
      // 递归创建目录和输出资源
      this.outputFileSystem.mkdirp(
        this.outputFileSystem.join(outputPath, dir),
        writeOut
      );
    },
    (err) => {
      return callback();
    }
  );
}
复制代码

3.emitRecords

// 设置stats相关信息 给run的回调参数
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享