1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | "use strict";
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | const { HtmlWebpackChildCompiler } = require("./child-compiler");
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | const compilerMap = new WeakMap();
|
49 |
|
50 | class CachedChildCompilation {
|
51 | |
52 |
|
53 |
|
54 | constructor(compiler) {
|
55 | |
56 |
|
57 |
|
58 |
|
59 | this.compiler = compiler;
|
60 |
|
61 |
|
62 | if (compilerMap.has(compiler)) {
|
63 | return;
|
64 | }
|
65 | const persistentChildCompilerSingletonPlugin =
|
66 | new PersistentChildCompilerSingletonPlugin();
|
67 | compilerMap.set(compiler, persistentChildCompilerSingletonPlugin);
|
68 | persistentChildCompilerSingletonPlugin.apply(compiler);
|
69 | }
|
70 |
|
71 | |
72 |
|
73 |
|
74 |
|
75 | addEntry(entry) {
|
76 | const persistentChildCompilerSingletonPlugin = compilerMap.get(
|
77 | this.compiler,
|
78 | );
|
79 | if (!persistentChildCompilerSingletonPlugin) {
|
80 | throw new Error(
|
81 | "PersistentChildCompilerSingletonPlugin instance not found.",
|
82 | );
|
83 | }
|
84 | persistentChildCompilerSingletonPlugin.addEntry(entry);
|
85 | }
|
86 |
|
87 | getCompilationResult() {
|
88 | const persistentChildCompilerSingletonPlugin = compilerMap.get(
|
89 | this.compiler,
|
90 | );
|
91 | if (!persistentChildCompilerSingletonPlugin) {
|
92 | throw new Error(
|
93 | "PersistentChildCompilerSingletonPlugin instance not found.",
|
94 | );
|
95 | }
|
96 | return persistentChildCompilerSingletonPlugin.getLatestResult();
|
97 | }
|
98 |
|
99 | |
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | getCompilationEntryResult(entry) {
|
108 | const latestResult = this.getCompilationResult();
|
109 | const compilationResult = latestResult.compilationResult;
|
110 | return "error" in compilationResult
|
111 | ? {
|
112 | mainCompilationHash: latestResult.mainCompilationHash,
|
113 | error: compilationResult.error,
|
114 | }
|
115 | : {
|
116 | mainCompilationHash: latestResult.mainCompilationHash,
|
117 | compiledEntry: compilationResult.compiledEntries[entry],
|
118 | };
|
119 | }
|
120 | }
|
121 |
|
122 | class PersistentChildCompilerSingletonPlugin {
|
123 | |
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | static createSnapshot(fileDependencies, mainCompilation, startTime) {
|
130 | return new Promise((resolve, reject) => {
|
131 | mainCompilation.fileSystemInfo.createSnapshot(
|
132 | startTime,
|
133 | fileDependencies.fileDependencies,
|
134 | fileDependencies.contextDependencies,
|
135 | fileDependencies.missingDependencies,
|
136 |
|
137 | null,
|
138 | (err, snapshot) => {
|
139 | if (err) {
|
140 | return reject(err);
|
141 | }
|
142 | resolve(snapshot);
|
143 | },
|
144 | );
|
145 | });
|
146 | }
|
147 |
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | static isSnapshotValid(snapshot, mainCompilation) {
|
157 | return new Promise((resolve, reject) => {
|
158 | mainCompilation.fileSystemInfo.checkSnapshotValid(
|
159 | snapshot,
|
160 | (err, isValid) => {
|
161 | if (err) {
|
162 | reject(err);
|
163 | }
|
164 | resolve(isValid);
|
165 | },
|
166 | );
|
167 | });
|
168 | }
|
169 |
|
170 | static watchFiles(mainCompilation, fileDependencies) {
|
171 | Object.keys(fileDependencies).forEach((dependencyType) => {
|
172 | fileDependencies[dependencyType].forEach((fileDependency) => {
|
173 | mainCompilation[dependencyType].add(fileDependency);
|
174 | });
|
175 | });
|
176 | }
|
177 |
|
178 | constructor() {
|
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 | this.compilationState = {
|
204 | isCompiling: false,
|
205 | isVerifyingCache: false,
|
206 | entries: [],
|
207 | compiledEntries: [],
|
208 | mainCompilationHash: "initial",
|
209 | compilationResult: {
|
210 | dependencies: {
|
211 | fileDependencies: [],
|
212 | contextDependencies: [],
|
213 | missingDependencies: [],
|
214 | },
|
215 | compiledEntries: {},
|
216 | },
|
217 | };
|
218 | }
|
219 |
|
220 | |
221 |
|
222 |
|
223 |
|
224 | apply(compiler) {
|
225 |
|
226 | let childCompilationResultPromise = Promise.resolve({
|
227 | dependencies: {
|
228 | fileDependencies: [],
|
229 | contextDependencies: [],
|
230 | missingDependencies: [],
|
231 | },
|
232 | compiledEntries: {},
|
233 | });
|
234 | |
235 |
|
236 |
|
237 |
|
238 |
|
239 | let mainCompilationHashOfLastChildRecompile = "";
|
240 |
|
241 | let previousFileSystemSnapshot;
|
242 | let compilationStartTime = new Date().getTime();
|
243 |
|
244 | compiler.hooks.make.tapAsync(
|
245 | "PersistentChildCompilerSingletonPlugin",
|
246 | (mainCompilation, callback) => {
|
247 | if (
|
248 | this.compilationState.isCompiling ||
|
249 | this.compilationState.isVerifyingCache
|
250 | ) {
|
251 | return callback(new Error("Child compilation has already started"));
|
252 | }
|
253 |
|
254 |
|
255 | compilationStartTime = new Date().getTime();
|
256 |
|
257 |
|
258 | this.compilationState = {
|
259 | isCompiling: false,
|
260 | isVerifyingCache: true,
|
261 | previousEntries: this.compilationState.compiledEntries,
|
262 | previousResult: this.compilationState.compilationResult,
|
263 | entries: this.compilationState.entries,
|
264 | };
|
265 |
|
266 |
|
267 | const isCacheValidPromise = this.isCacheValid(
|
268 | previousFileSystemSnapshot,
|
269 | mainCompilation,
|
270 | );
|
271 |
|
272 | let cachedResult = childCompilationResultPromise;
|
273 | childCompilationResultPromise = isCacheValidPromise.then(
|
274 | (isCacheValid) => {
|
275 |
|
276 | if (isCacheValid) {
|
277 | return cachedResult;
|
278 | }
|
279 |
|
280 | const compiledEntriesPromise = this.compileEntries(
|
281 | mainCompilation,
|
282 | this.compilationState.entries,
|
283 | );
|
284 |
|
285 |
|
286 |
|
287 | compiledEntriesPromise
|
288 | .then((childCompilationResult) => {
|
289 | return PersistentChildCompilerSingletonPlugin.createSnapshot(
|
290 | childCompilationResult.dependencies,
|
291 | mainCompilation,
|
292 | compilationStartTime,
|
293 | );
|
294 | })
|
295 | .then((snapshot) => {
|
296 | previousFileSystemSnapshot = snapshot;
|
297 | });
|
298 | return compiledEntriesPromise;
|
299 | },
|
300 | );
|
301 |
|
302 |
|
303 | mainCompilation.hooks.optimizeTree.tapAsync(
|
304 | "PersistentChildCompilerSingletonPlugin",
|
305 | (chunks, modules, callback) => {
|
306 | const handleCompilationDonePromise =
|
307 | childCompilationResultPromise.then((childCompilationResult) => {
|
308 | this.watchFiles(
|
309 | mainCompilation,
|
310 | childCompilationResult.dependencies,
|
311 | );
|
312 | });
|
313 | handleCompilationDonePromise.then(
|
314 |
|
315 | () => callback(null, chunks, modules),
|
316 | callback,
|
317 | );
|
318 | },
|
319 | );
|
320 |
|
321 |
|
322 | mainCompilation.hooks.additionalAssets.tapAsync(
|
323 | "PersistentChildCompilerSingletonPlugin",
|
324 | (callback) => {
|
325 | const didRecompilePromise = Promise.all([
|
326 | childCompilationResultPromise,
|
327 | cachedResult,
|
328 | ]).then(([childCompilationResult, cachedResult]) => {
|
329 |
|
330 | return cachedResult !== childCompilationResult;
|
331 | });
|
332 |
|
333 | const handleCompilationDonePromise = Promise.all([
|
334 | childCompilationResultPromise,
|
335 | didRecompilePromise,
|
336 | ]).then(([childCompilationResult, didRecompile]) => {
|
337 |
|
338 | if (didRecompile) {
|
339 | mainCompilationHashOfLastChildRecompile =
|
340 | (mainCompilation.hash);
|
341 | }
|
342 | this.compilationState = {
|
343 | isCompiling: false,
|
344 | isVerifyingCache: false,
|
345 | entries: this.compilationState.entries,
|
346 | compiledEntries: this.compilationState.entries,
|
347 | compilationResult: childCompilationResult,
|
348 | mainCompilationHash: mainCompilationHashOfLastChildRecompile,
|
349 | };
|
350 | });
|
351 | handleCompilationDonePromise.then(() => callback(null), callback);
|
352 | },
|
353 | );
|
354 |
|
355 |
|
356 | callback(null);
|
357 | },
|
358 | );
|
359 | }
|
360 |
|
361 | |
362 |
|
363 |
|
364 |
|
365 | addEntry(entry) {
|
366 | if (
|
367 | this.compilationState.isCompiling ||
|
368 | this.compilationState.isVerifyingCache
|
369 | ) {
|
370 | throw new Error(
|
371 | "The child compiler has already started to compile. " +
|
372 | "Please add entries before the main compiler 'make' phase has started or " +
|
373 | "after the compilation is done.",
|
374 | );
|
375 | }
|
376 | if (this.compilationState.entries.indexOf(entry) === -1) {
|
377 | this.compilationState.entries = [...this.compilationState.entries, entry];
|
378 | }
|
379 | }
|
380 |
|
381 | getLatestResult() {
|
382 | if (
|
383 | this.compilationState.isCompiling ||
|
384 | this.compilationState.isVerifyingCache
|
385 | ) {
|
386 | throw new Error(
|
387 | "The child compiler is not done compiling. " +
|
388 | "Please access the result after the compiler 'make' phase has started or " +
|
389 | "after the compilation is done.",
|
390 | );
|
391 | }
|
392 | return {
|
393 | mainCompilationHash: this.compilationState.mainCompilationHash,
|
394 | compilationResult: this.compilationState.compilationResult,
|
395 | };
|
396 | }
|
397 |
|
398 | |
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 | isCacheValid(snapshot, mainCompilation) {
|
406 | if (!this.compilationState.isVerifyingCache) {
|
407 | return Promise.reject(
|
408 | new Error(
|
409 | "Cache validation can only be done right before the compilation starts",
|
410 | ),
|
411 | );
|
412 | }
|
413 |
|
414 | if (this.compilationState.entries.length === 0) {
|
415 | return Promise.resolve(true);
|
416 | }
|
417 |
|
418 | if (
|
419 | this.compilationState.entries !== this.compilationState.previousEntries
|
420 | ) {
|
421 | return Promise.resolve(false);
|
422 | }
|
423 |
|
424 | if (!snapshot) {
|
425 | return Promise.resolve(false);
|
426 | }
|
427 |
|
428 | return PersistentChildCompilerSingletonPlugin.isSnapshotValid(
|
429 | snapshot,
|
430 | mainCompilation,
|
431 | );
|
432 | }
|
433 |
|
434 | |
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 | compileEntries(mainCompilation, entries) {
|
443 | const compiler = new HtmlWebpackChildCompiler(entries);
|
444 | return compiler.compileTemplates(mainCompilation).then(
|
445 | (result) => {
|
446 | return {
|
447 |
|
448 | compiledEntries: result,
|
449 |
|
450 |
|
451 | dependencies: compiler.fileDependencies,
|
452 |
|
453 |
|
454 | mainCompilationHash: mainCompilation.hash,
|
455 | };
|
456 | },
|
457 | (error) => ({
|
458 |
|
459 | error,
|
460 |
|
461 |
|
462 | dependencies: compiler.fileDependencies,
|
463 |
|
464 |
|
465 | mainCompilationHash: mainCompilation.hash,
|
466 | }),
|
467 | );
|
468 | }
|
469 |
|
470 | |
471 |
|
472 |
|
473 |
|
474 |
|
475 | watchFiles(mainCompilation, files) {
|
476 | PersistentChildCompilerSingletonPlugin.watchFiles(mainCompilation, files);
|
477 | }
|
478 | }
|
479 |
|
480 | module.exports = {
|
481 | CachedChildCompilation,
|
482 | };
|