Fancy Front End Fancy Front End
  • 开始上手
  • 基础
  • 调度器(Scheduler)
  • 更新器(Updater)
  • 渲染器(Render)
  • 更新周期
  • hooks 原理
  • 总结
  • 📙 React源码漂流记
  • 开始上手
  • 基础
  • reactivity
  • runtime-core
  • runtime-dom
  • Awesome Web
  • Awesome NodeJS
话题
  • 导航
  • Q&A
  • 幻灯片
  • 关于
  • 分类
  • 标签
  • 归档
博客 (opens new window)
GitHub (opens new window)

Jonsam NG

让有意义的事变得有意思,让有意思的事变得有意义
  • 开始上手
  • 基础
  • 调度器(Scheduler)
  • 更新器(Updater)
  • 渲染器(Render)
  • 更新周期
  • hooks 原理
  • 总结
  • 📙 React源码漂流记
  • 开始上手
  • 基础
  • reactivity
  • runtime-core
  • runtime-dom
  • Awesome Web
  • Awesome NodeJS
话题
  • 导航
  • Q&A
  • 幻灯片
  • 关于
  • 分类
  • 标签
  • 归档
博客 (opens new window)
GitHub (opens new window)
  • 开始上手
  • Plan 计划
  • typescript-utility

  • single-spa源码

  • qiankun源码

  • webpack

    • 开始阅读
    • tapable源码

    • init阶段

    • make阶段

      • 本章概要
      • make 阶段:compilation
        • 目录
        • compilation.addEntry
        • addModuleTree
        • handleModuleCreation
        • moduleGraph
        • AsyncQueue
        • compilation.Hook
      • make 阶段:module
      • make 阶段:walk
    • 总结

  • axios

  • solid

  • vite源码

  • jquery源码

  • snabbdom

  • am-editor

  • html2canvas

  • express

  • acorn源码

  • immutable.js源码

  • web
  • webpack
  • make阶段
jonsam
2022-04-22
目录

make 阶段:compilation

# 目录

  • 目录
  • compilation.addEntry
  • addModuleTree
  • handleModuleCreation
    • factorizeModule
    • addModule
  • moduleGraph
  • AsyncQueue
  • compilation.Hook

我们已经知道在 compiler.compile 中会创建 compilation,并且在 CompilerHook.make 触发时会调用 compilation.addEntry ,此时 EntryDependency 已经被创建。

当 CompilerHook.make 触发之后,就正式进入了 make 阶段的内容。

本节将探讨 webpack 构建流程中 make 阶段的如下工作:

  • compilation.addEntry

# compilation.addEntry

内部调用 _addEntryItem 。 _addEntryItem 将会被 addEntry 和 addInclude 。

// lib/Compilation.js
_addEntryItem(context, entry, target, options, callback) {
  const { name } = options;
  // 获取 entryData,无 name 则使用 globalEntry
  let entryData =
    name !== undefined ? this.entries.get(name) : this.globalEntry;
  if (entryData === undefined) {
    // 创建 entryData,包含 dependencies 和 includeDependencies
    entryData = {
      dependencies: [],
      includeDependencies: [],
      options: {
        name: undefined,
        ...options
      }
    };
    // target 为 dependencies 或者 includeDependencies
    // 将 entry 推入 entryData 中相应类型的依赖列表
    entryData[target].push(entry);
    // 将 entryData 缓存到 entries,entries<name, entryData>
    this.entries.set(name, entryData);
  } else {
    entryData[target].push(entry);
    // 将 options 中的新增属性复制到 entryData.options
    for (const key of Object.keys(options)) {
      if (options[key] === undefined) continue;
      if (entryData.options[key] === options[key]) continue;
      if (
        Array.isArray(entryData.options[key]) &&
        Array.isArray(options[key]) &&
        arrayEquals(entryData.options[key], options[key])
      ) {
        continue;
      }
      if (entryData.options[key] === undefined) {
        entryData.options[key] = options[key];
      } else {
        return callback(
          new WebpackError(
            `Conflicting entry option ${key} = ${entryData.options[key]} vs ${options[key]}`
          )
        );
      }
    }
  }
  // 触发 CompilationHook.addEntry => call
  this.hooks.addEntry.call(entry, options);
  // 调用 compilation.addModuleTree 开始构建 moduleTree
  this.addModuleTree(
    {
      context,
      dependency: entry,
      contextInfo: entryData.options.layer
        ? { issuerLayer: entryData.options.layer }
        : undefined
    },
    // finished callback
    (err, module) => {
      if (err) {
        // 触发 CompilationHook.failedEntry => call
        this.hooks.failedEntry.call(entry, options, err);
        return callback(err);
      }
      // 触发 CompilationHook.succeedEntry => call
      this.hooks.succeedEntry.call(entry, options, module);
      return callback(null, module);
    }
  );
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  • entries 的结构是 Map<name, {dependencies: [],includeDependencies: []}> , entry 本质是 EntryDependency 对象。
  • 调用 addModuleTree 根据 Dependence 对象创建 Module 对象,并将 Module 添加到 moduleGraph。

# addModuleTree

传递 moduleFactory,并调用 handleModuleCreation 创建 Module。

// lib/Compilation.js
addModuleTree({ context, dependency, contextInfo }, callback) {
  const Dep = /** @type {DepConstructor} */ (dependency.constructor);
  // 根据 Dependency 的构造器可以获取到 moduleFactory
  const moduleFactory = this.dependencyFactories.get(Dep);

  this.handleModuleCreation(
    {
      factory: moduleFactory,
      dependencies: [dependency],
      originModule: null,
      contextInfo,
      context
    },
    // ......
  );
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# handleModuleCreation

标签: 重要
// lib/Compilation.js
handleModuleCreation = () => {
  // this.moduleGraph = new ModuleGraph();
  const moduleGraph = this.moduleGraph;
  // factorizeModule 通过相应的 ModuleFactory 创建 Module
  this.factorizeModule(
   {
    currentProfile,
    factory,
    dependencies,
    factoryResult: true,
    originModule,
    contextInfo,
    context
   }, (err, factoryResult) => {
   // 将 Entry 中  fileDependencies、contextDependencies、missingDependencies 收集起来
  const applyFactoryResultDependencies = () => {
   const { fileDependencies, contextDependencies, missingDependencies } =
    factoryResult;
   if (fileDependencies) {
    this.fileDependencies.addAll(fileDependencies);
   }
   if (contextDependencies) {
    this.contextDependencies.addAll(contextDependencies);
   }
   if (missingDependencies) {
    this.missingDependencies.addAll(missingDependencies);
   }
  };

  // 在任务回调中 获得 Module
  const newModule = factoryResult.module;
    // 将 module 加入到 addModuleQueue 队列
    this.addModule(newModule, (err, module) => {
      applyFactoryResultDependencies();
      // 循环 dependencies,将 dependency 设置到 moduleGraph
      for (let i = 0; i < dependencies.length; i++) {
       const dependency = dependencies[i];
       moduleGraph.setResolvedModule(
        connectOrigin ? originModule : null,
        dependency,
        module
       );
      }
      this._handleModuleBuildAndDependencies(
        originModule,
        module,
        recursive,
        callback
       );
    })
 })
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

总结如下:

  • factorizeModule 通过 ModuleFactory 创建 Module 和 ModuleDependencies。
  • addModule 将 Module 添加到 moduleGraph,同时将 ModuleDependencies 分别添加到 fileDependencies 、 contextDependencies 或者 missingDependencies 。

# factorizeModule

function (options, callback) {
  this.factorizeQueue.add(options, callback);
}
1
2
3

factorizeModule 将 factorizeModule 的任务加入到 factorizeQueue,其 processor 为 _factorizeModule。

_factorizeModule() {
  factory.create(/*......*/, (err, result) => {
    callback(null, factoryResult ? result : result.module);
  })
}
1
2
3
4
5

_factorizeModule 中的 factory 即为 EntryPlugin (或者其他插件) 为 Dependence 注册的 ModuleFactory ,是创建 Module 的工厂函数。以 normalModuleFactory 为例:

create(data, callback) {
  const resolveData = {
   contextInfo,
   resolveOptions,
   context,
   request,
   assertions,
   dependencies,
   dependencyType,
   fileDependencies,
   missingDependencies,
   contextDependencies,
   createData: {},
   cacheable: true
  };
  // 触发 CompilationHook.beforeResolve => callAsync
  this.hooks.beforeResolve.callAsync(resolveData, (err, result) => {
    // 触发 CompilationHook.factorize => callAsync
    this.hooks.factorize.callAsync(resolveData, (err, module) => {}
  )})
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# addModule

addModule 将 module 包装成任务加入 addModuleQueue 队列,processor 是 _addModule 函数。

addModule(module, callback) {
 this.addModuleQueue.add(module, callback);
}
_addModule(module, callback) {
 const identifier = module.identifier();
 // 如果 modules 已经在 _modules,直接 callback
 const alreadyAddedModule = this._modules.get(identifier);
 if (alreadyAddedModule) {
  return callback(null, alreadyAddedModule);
 }
// ......

 // 尝试从缓存中获取相同 identifier 的 module
 this._modulesCache.get(identifier, null, (err, cacheModule) => {
    // ......
  // 如果命中缓存,以 cacheModule 为模板进行更新
  if (cacheModule) {
   cacheModule.updateCacheModule(module);
   // 将 cacheModule 作为 module
   module = cacheModule;
  }
  // 将 module 设置到 _modules
  this._modules.set(identifier, module);
  // 将 module 加入到 modules,注意 _modules 是 Map,用于查找,modules 是 Set
  this.modules.add(module);
  // ......
  callback(null, module);
 });
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

_addModule 将 module 加入到 _modules 和 module 。

# moduleGraph

# AsyncQueue

compilation 中的 AsyncQueue,包括 processDependenciesQueue、addModuleQueue、factorizeQueue、buildQueue、rebuildQueue,前四者是父子结构。

/** @type {AsyncQueue<Module, Module, Module>} */
this.processDependenciesQueue = new AsyncQueue({
 name: "processDependencies",
 parallelism: options.parallelism || 100,
 processor: this._processModuleDependencies.bind(this),
});
/** @type {AsyncQueue<Module, string, Module>} */
this.addModuleQueue = new AsyncQueue({
 name: "addModule",
 parent: this.processDependenciesQueue,
 getKey: (module) => module.identifier(),
 processor: this._addModule.bind(this),
});
/** @type {AsyncQueue<FactorizeModuleOptions, string, Module | ModuleFactoryResult>} */
this.factorizeQueue = new AsyncQueue({
 name: "factorize",
 parent: this.addModuleQueue,
 processor: this._factorizeModule.bind(this),
});
/** @type {AsyncQueue<Module, Module, Module>} */
this.buildQueue = new AsyncQueue({
 name: "build",
 parent: this.factorizeQueue,
 processor: this._buildModule.bind(this),
});
/** @type {AsyncQueue<Module, Module, Module>} */
this.rebuildQueue = new AsyncQueue({
 name: "rebuild",
 parallelism: options.parallelism || 100,
 processor: this._rebuildModule.bind(this),
});

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# compilation.Hook

this.hooks = Object.freeze({
  /** @type {SyncHook<[Module]>} */
  buildModule: new SyncHook(["module"]),
  /** @type {SyncHook<[Module]>} */
  rebuildModule: new SyncHook(["module"]),
  /** @type {SyncHook<[Module, WebpackError]>} */
  failedModule: new SyncHook(["module", "error"]),
  /** @type {SyncHook<[Module]>} */
  succeedModule: new SyncHook(["module"]),
  /** @type {SyncHook<[Module]>} */
  stillValidModule: new SyncHook(["module"]),

  /** @type {SyncHook<[Dependency, EntryOptions]>} */
  addEntry: new SyncHook(["entry", "options"]),
  /** @type {SyncHook<[Dependency, EntryOptions, Error]>} */
  failedEntry: new SyncHook(["entry", "options", "error"]),
  /** @type {SyncHook<[Dependency, EntryOptions, Module]>} */
  succeedEntry: new SyncHook(["entry", "options", "module"]),

  /** @type {SyncWaterfallHook<[(string[] | ReferencedExport)[], Dependency, RuntimeSpec]>} */
  dependencyReferencedExports: new SyncWaterfallHook([
    "referencedExports",
    "dependency",
    "runtime"
  ]),

  /** @type {SyncHook<[ExecuteModuleArgument, ExecuteModuleContext]>} */
  executeModule: new SyncHook(["options", "context"]),
  /** @type {AsyncParallelHook<[ExecuteModuleArgument, ExecuteModuleContext]>} */
  prepareModuleExecution: new AsyncParallelHook(["options", "context"]),

  /** @type {AsyncSeriesHook<[Iterable<Module>]>} */
  finishModules: new AsyncSeriesHook(["modules"]),
  /** @type {AsyncSeriesHook<[Module]>} */
  finishRebuildingModule: new AsyncSeriesHook(["module"]),
  /** @type {SyncHook<[]>} */
  unseal: new SyncHook([]),
  /** @type {SyncHook<[]>} */
  seal: new SyncHook([]),

  /** @type {SyncHook<[]>} */
  beforeChunks: new SyncHook([]),
  /** @type {SyncHook<[Iterable<Chunk>]>} */
  afterChunks: new SyncHook(["chunks"]),

  /** @type {SyncBailHook<[Iterable<Module>]>} */
  optimizeDependencies: new SyncBailHook(["modules"]),
  /** @type {SyncHook<[Iterable<Module>]>} */
  afterOptimizeDependencies: new SyncHook(["modules"]),

  /** @type {SyncHook<[]>} */
  optimize: new SyncHook([]),
  /** @type {SyncBailHook<[Iterable<Module>]>} */
  optimizeModules: new SyncBailHook(["modules"]),
  /** @type {SyncHook<[Iterable<Module>]>} */
  afterOptimizeModules: new SyncHook(["modules"]),

  /** @type {SyncBailHook<[Iterable<Chunk>, ChunkGroup[]]>} */
  optimizeChunks: new SyncBailHook(["chunks", "chunkGroups"]),
  /** @type {SyncHook<[Iterable<Chunk>, ChunkGroup[]]>} */
  afterOptimizeChunks: new SyncHook(["chunks", "chunkGroups"]),

  /** @type {AsyncSeriesHook<[Iterable<Chunk>, Iterable<Module>]>} */
  optimizeTree: new AsyncSeriesHook(["chunks", "modules"]),
  /** @type {SyncHook<[Iterable<Chunk>, Iterable<Module>]>} */
  afterOptimizeTree: new SyncHook(["chunks", "modules"]),

  /** @type {AsyncSeriesBailHook<[Iterable<Chunk>, Iterable<Module>]>} */
  optimizeChunkModules: new AsyncSeriesBailHook(["chunks", "modules"]),
  /** @type {SyncHook<[Iterable<Chunk>, Iterable<Module>]>} */
  afterOptimizeChunkModules: new SyncHook(["chunks", "modules"]),
  /** @type {SyncBailHook<[], boolean>} */
  shouldRecord: new SyncBailHook([]),

  /** @type {SyncHook<[Chunk, Set<string>, RuntimeRequirementsContext]>} */
  additionalChunkRuntimeRequirements: new SyncHook([
    "chunk",
    "runtimeRequirements",
    "context"
  ]),
  /** @type {HookMap<SyncBailHook<[Chunk, Set<string>, RuntimeRequirementsContext]>>} */
  runtimeRequirementInChunk: new HookMap(
    () => new SyncBailHook(["chunk", "runtimeRequirements", "context"])
  ),
  /** @type {SyncHook<[Module, Set<string>, RuntimeRequirementsContext]>} */
  additionalModuleRuntimeRequirements: new SyncHook([
    "module",
    "runtimeRequirements",
    "context"
  ]),
  /** @type {HookMap<SyncBailHook<[Module, Set<string>, RuntimeRequirementsContext]>>} */
  runtimeRequirementInModule: new HookMap(
    () => new SyncBailHook(["module", "runtimeRequirements", "context"])
  ),
  /** @type {SyncHook<[Chunk, Set<string>, RuntimeRequirementsContext]>} */
  additionalTreeRuntimeRequirements: new SyncHook([
    "chunk",
    "runtimeRequirements",
    "context"
  ]),
  /** @type {HookMap<SyncBailHook<[Chunk, Set<string>, RuntimeRequirementsContext]>>} */
  runtimeRequirementInTree: new HookMap(
    () => new SyncBailHook(["chunk", "runtimeRequirements", "context"])
  ),

  /** @type {SyncHook<[RuntimeModule, Chunk]>} */
  runtimeModule: new SyncHook(["module", "chunk"]),

  /** @type {SyncHook<[Iterable<Module>, any]>} */
  reviveModules: new SyncHook(["modules", "records"]),
  /** @type {SyncHook<[Iterable<Module>]>} */
  beforeModuleIds: new SyncHook(["modules"]),
  /** @type {SyncHook<[Iterable<Module>]>} */
  moduleIds: new SyncHook(["modules"]),
  /** @type {SyncHook<[Iterable<Module>]>} */
  optimizeModuleIds: new SyncHook(["modules"]),
  /** @type {SyncHook<[Iterable<Module>]>} */
  afterOptimizeModuleIds: new SyncHook(["modules"]),

  /** @type {SyncHook<[Iterable<Chunk>, any]>} */
  reviveChunks: new SyncHook(["chunks", "records"]),
  /** @type {SyncHook<[Iterable<Chunk>]>} */
  beforeChunkIds: new SyncHook(["chunks"]),
  /** @type {SyncHook<[Iterable<Chunk>]>} */
  chunkIds: new SyncHook(["chunks"]),
  /** @type {SyncHook<[Iterable<Chunk>]>} */
  optimizeChunkIds: new SyncHook(["chunks"]),
  /** @type {SyncHook<[Iterable<Chunk>]>} */
  afterOptimizeChunkIds: new SyncHook(["chunks"]),

  /** @type {SyncHook<[Iterable<Module>, any]>} */
  recordModules: new SyncHook(["modules", "records"]),
  /** @type {SyncHook<[Iterable<Chunk>, any]>} */
  recordChunks: new SyncHook(["chunks", "records"]),

  /** @type {SyncHook<[Iterable<Module>]>} */
  optimizeCodeGeneration: new SyncHook(["modules"]),

  /** @type {SyncHook<[]>} */
  beforeModuleHash: new SyncHook([]),
  /** @type {SyncHook<[]>} */
  afterModuleHash: new SyncHook([]),

  /** @type {SyncHook<[]>} */
  beforeCodeGeneration: new SyncHook([]),
  /** @type {SyncHook<[]>} */
  afterCodeGeneration: new SyncHook([]),

  /** @type {SyncHook<[]>} */
  beforeRuntimeRequirements: new SyncHook([]),
  /** @type {SyncHook<[]>} */
  afterRuntimeRequirements: new SyncHook([]),

  /** @type {SyncHook<[]>} */
  beforeHash: new SyncHook([]),
  /** @type {SyncHook<[Chunk]>} */
  contentHash: new SyncHook(["chunk"]),
  /** @type {SyncHook<[]>} */
  afterHash: new SyncHook([]),
  /** @type {SyncHook<[any]>} */
  recordHash: new SyncHook(["records"]),
  /** @type {SyncHook<[Compilation, any]>} */
  record: new SyncHook(["compilation", "records"]),

  /** @type {SyncHook<[]>} */
  beforeModuleAssets: new SyncHook([]),
  /** @type {SyncBailHook<[], boolean>} */
  shouldGenerateChunkAssets: new SyncBailHook([]),
  /** @type {SyncHook<[]>} */
  beforeChunkAssets: new SyncHook([]),
  /** @type {AsyncSeriesHook<[CompilationAssets]>} */
  processAdditionalAssets: new AsyncSeriesHook(["assets"]),

  /** @type {SyncBailHook<[], boolean>} */
  needAdditionalSeal: new SyncBailHook([]),
  /** @type {AsyncSeriesHook<[]>} */
  afterSeal: new AsyncSeriesHook([]),

  /** @type {SyncWaterfallHook<[RenderManifestEntry[], RenderManifestOptions]>} */
  renderManifest: new SyncWaterfallHook(["result", "options"]),

  /** @type {SyncHook<[Hash]>} */
  fullHash: new SyncHook(["hash"]),
  /** @type {SyncHook<[Chunk, Hash, ChunkHashContext]>} */
  chunkHash: new SyncHook(["chunk", "chunkHash", "ChunkHashContext"]),

  /** @type {SyncHook<[Module, string]>} */
  moduleAsset: new SyncHook(["module", "filename"]),
  /** @type {SyncHook<[Chunk, string]>} */
  chunkAsset: new SyncHook(["chunk", "filename"]),

  /** @type {SyncWaterfallHook<[string, object, AssetInfo]>} */
  assetPath: new SyncWaterfallHook(["path", "options", "assetInfo"]),

  /** @type {SyncBailHook<[], boolean>} */
  needAdditionalPass: new SyncBailHook([]),

  /** @type {SyncHook<[Compiler, string, number]>} */
  childCompiler: new SyncHook([
    "childCompiler",
    "compilerName",
    "compilerIndex"
  ]),

  /** @type {SyncBailHook<[string, LogEntry], true>} */
  log: new SyncBailHook(["origin", "logEntry"]),

  /** @type {SyncWaterfallHook<[WebpackError[]]>} */
  processWarnings: new SyncWaterfallHook(["warnings"]),
  /** @type {SyncWaterfallHook<[WebpackError[]]>} */
  processErrors: new SyncWaterfallHook(["errors"]),

  /** @type {HookMap<SyncHook<[Partial<NormalizedStatsOptions>, CreateStatsOptionsContext]>>} */
  statsPreset: new HookMap(() => new SyncHook(["options", "context"])),
  /** @type {SyncHook<[Partial<NormalizedStatsOptions>, CreateStatsOptionsContext]>} */
  statsNormalize: new SyncHook(["options", "context"]),
  /** @type {SyncHook<[StatsFactory, NormalizedStatsOptions]>} */
  statsFactory: new SyncHook(["statsFactory", "options"]),
  /** @type {SyncHook<[StatsPrinter, NormalizedStatsOptions]>} */
  statsPrinter: new SyncHook(["statsPrinter", "options"]),

  get normalModuleLoader() {
    return getNormalModuleLoader();
  }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
编辑 (opens new window)
上次更新: 2022/09/06, 14:25:16
本章概要
make 阶段:module

← 本章概要 make 阶段:module→

最近更新
01
渲染原理之组件结构与 JSX 编译
09-07
02
计划跟踪
09-06
03
开始上手
09-06
更多文章>
Theme by Vdoing | Copyright © 2022-2022 Fancy Front End | Made by Jonsam by ❤
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式