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 | 'use strict';
|
39 |
|
40 | const { HtmlWebpackChildCompiler } = require('./child-compiler');
|
41 | const fileWatcherApi = require('./file-watcher-api');
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | const compilerMap = new WeakMap();
|
50 |
|
51 | class CachedChildCompilation {
|
52 | |
53 |
|
54 |
|
55 | constructor (compiler) {
|
56 | |
57 |
|
58 |
|
59 |
|
60 | this.compiler = compiler;
|
61 |
|
62 |
|
63 | if (compilerMap.has(compiler)) {
|
64 | return;
|
65 | }
|
66 | const persistentChildCompilerSingletonPlugin = 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(this.compiler);
|
77 | if (!persistentChildCompilerSingletonPlugin) {
|
78 | throw new Error(
|
79 | 'PersistentChildCompilerSingletonPlugin instance not found.'
|
80 | );
|
81 | }
|
82 | persistentChildCompilerSingletonPlugin.addEntry(entry);
|
83 | }
|
84 |
|
85 | getCompilationResult () {
|
86 | const persistentChildCompilerSingletonPlugin = compilerMap.get(this.compiler);
|
87 | if (!persistentChildCompilerSingletonPlugin) {
|
88 | throw new Error(
|
89 | 'PersistentChildCompilerSingletonPlugin instance not found.'
|
90 | );
|
91 | }
|
92 | return persistentChildCompilerSingletonPlugin.getLatestResult();
|
93 | }
|
94 |
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | getCompilationEntryResult (entry) {
|
104 | const latestResult = this.getCompilationResult();
|
105 | const compilationResult = latestResult.compilationResult;
|
106 | return 'error' in compilationResult ? {
|
107 | mainCompilationHash: latestResult.mainCompilationHash,
|
108 | error: compilationResult.error
|
109 | } : {
|
110 | mainCompilationHash: latestResult.mainCompilationHash,
|
111 | compiledEntry: compilationResult.compiledEntries[entry]
|
112 | };
|
113 | }
|
114 | }
|
115 |
|
116 | class PersistentChildCompilerSingletonPlugin {
|
117 | constructor () {
|
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 | this.compilationState = {
|
143 | isCompiling: false,
|
144 | isVerifyingCache: false,
|
145 | entries: [],
|
146 | compiledEntries: [],
|
147 | mainCompilationHash: 'initial',
|
148 | compilationResult: {
|
149 | dependencies: {
|
150 | fileDependencies: [],
|
151 | contextDependencies: [],
|
152 | missingDependencies: []
|
153 | },
|
154 | compiledEntries: {}
|
155 | }
|
156 | };
|
157 | }
|
158 |
|
159 | |
160 |
|
161 |
|
162 |
|
163 | apply (compiler) {
|
164 |
|
165 | let childCompilationResultPromise = Promise.resolve({
|
166 | dependencies: {
|
167 | fileDependencies: [],
|
168 | contextDependencies: [],
|
169 | missingDependencies: []
|
170 | },
|
171 | compiledEntries: {}
|
172 | });
|
173 | |
174 |
|
175 |
|
176 |
|
177 | let mainCompilationHashOfLastChildRecompile = '';
|
178 |
|
179 | let previousFileSystemSnapshot;
|
180 | let compilationStartTime = new Date().getTime();
|
181 |
|
182 | compiler.hooks.make.tapAsync(
|
183 | 'PersistentChildCompilerSingletonPlugin',
|
184 | (mainCompilation, callback) => {
|
185 | if (this.compilationState.isCompiling || this.compilationState.isVerifyingCache) {
|
186 | return callback(new Error('Child compilation has already started'));
|
187 | }
|
188 |
|
189 |
|
190 | compilationStartTime = new Date().getTime();
|
191 |
|
192 |
|
193 | this.compilationState = {
|
194 | isCompiling: false,
|
195 | isVerifyingCache: true,
|
196 | previousEntries: this.compilationState.compiledEntries,
|
197 | previousResult: this.compilationState.compilationResult,
|
198 | entries: this.compilationState.entries
|
199 | };
|
200 |
|
201 |
|
202 | const isCacheValidPromise = this.isCacheValid(previousFileSystemSnapshot, mainCompilation);
|
203 |
|
204 | let cachedResult = childCompilationResultPromise;
|
205 | childCompilationResultPromise = isCacheValidPromise.then((isCacheValid) => {
|
206 |
|
207 | if (isCacheValid) {
|
208 | return cachedResult;
|
209 | }
|
210 |
|
211 | const compiledEntriesPromise = this.compileEntries(
|
212 | mainCompilation,
|
213 | this.compilationState.entries
|
214 | );
|
215 |
|
216 |
|
217 |
|
218 | compiledEntriesPromise.then((childCompilationResult) => {
|
219 | return fileWatcherApi.createSnapshot(childCompilationResult.dependencies, mainCompilation, compilationStartTime);
|
220 | }).then((snapshot) => {
|
221 | previousFileSystemSnapshot = snapshot;
|
222 | });
|
223 | return compiledEntriesPromise;
|
224 | });
|
225 |
|
226 |
|
227 | mainCompilation.hooks.optimizeTree.tapAsync(
|
228 | 'PersistentChildCompilerSingletonPlugin',
|
229 | (chunks, modules, callback) => {
|
230 | const handleCompilationDonePromise = childCompilationResultPromise.then(
|
231 | childCompilationResult => {
|
232 | this.watchFiles(
|
233 | mainCompilation,
|
234 | childCompilationResult.dependencies
|
235 | );
|
236 | });
|
237 | handleCompilationDonePromise.then(() => callback(null, chunks, modules), callback);
|
238 | }
|
239 | );
|
240 |
|
241 |
|
242 | mainCompilation.hooks.additionalAssets.tapAsync(
|
243 | 'PersistentChildCompilerSingletonPlugin',
|
244 | (callback) => {
|
245 | const didRecompilePromise = Promise.all([childCompilationResultPromise, cachedResult]).then(
|
246 | ([childCompilationResult, cachedResult]) => {
|
247 |
|
248 | return (cachedResult !== childCompilationResult);
|
249 | }
|
250 | );
|
251 |
|
252 | const handleCompilationDonePromise = Promise.all([childCompilationResultPromise, didRecompilePromise]).then(
|
253 | ([childCompilationResult, didRecompile]) => {
|
254 |
|
255 | if (didRecompile) {
|
256 | mainCompilationHashOfLastChildRecompile = mainCompilation.hash;
|
257 | }
|
258 | this.compilationState = {
|
259 | isCompiling: false,
|
260 | isVerifyingCache: false,
|
261 | entries: this.compilationState.entries,
|
262 | compiledEntries: this.compilationState.entries,
|
263 | compilationResult: childCompilationResult,
|
264 | mainCompilationHash: mainCompilationHashOfLastChildRecompile
|
265 | };
|
266 | });
|
267 | handleCompilationDonePromise.then(() => callback(null), callback);
|
268 | }
|
269 | );
|
270 |
|
271 |
|
272 | callback(null);
|
273 | }
|
274 | );
|
275 | }
|
276 |
|
277 | |
278 |
|
279 |
|
280 |
|
281 | addEntry (entry) {
|
282 | if (this.compilationState.isCompiling || this.compilationState.isVerifyingCache) {
|
283 | throw new Error(
|
284 | 'The child compiler has already started to compile. ' +
|
285 | "Please add entries before the main compiler 'make' phase has started or " +
|
286 | 'after the compilation is done.'
|
287 | );
|
288 | }
|
289 | if (this.compilationState.entries.indexOf(entry) === -1) {
|
290 | this.compilationState.entries = [...this.compilationState.entries, entry];
|
291 | }
|
292 | }
|
293 |
|
294 | getLatestResult () {
|
295 | if (this.compilationState.isCompiling || this.compilationState.isVerifyingCache) {
|
296 | throw new Error(
|
297 | 'The child compiler is not done compiling. ' +
|
298 | "Please access the result after the compiler 'make' phase has started or " +
|
299 | 'after the compilation is done.'
|
300 | );
|
301 | }
|
302 | return {
|
303 | mainCompilationHash: this.compilationState.mainCompilationHash,
|
304 | compilationResult: this.compilationState.compilationResult
|
305 | };
|
306 | }
|
307 |
|
308 | |
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 | isCacheValid (snapshot, mainCompilation) {
|
316 | if (!this.compilationState.isVerifyingCache) {
|
317 | return Promise.reject(new Error('Cache validation can only be done right before the compilation starts'));
|
318 | }
|
319 |
|
320 | if (this.compilationState.entries.length === 0) {
|
321 | return Promise.resolve(true);
|
322 | }
|
323 |
|
324 | if (this.compilationState.entries !== this.compilationState.previousEntries) {
|
325 | return Promise.resolve(false);
|
326 | }
|
327 |
|
328 | if (!snapshot) {
|
329 | return Promise.resolve(false);
|
330 | }
|
331 | return fileWatcherApi.isSnapShotValid(snapshot, mainCompilation);
|
332 | }
|
333 |
|
334 | |
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 | compileEntries (mainCompilation, entries) {
|
343 | const compiler = new HtmlWebpackChildCompiler(entries);
|
344 | return compiler.compileTemplates(mainCompilation).then((result) => {
|
345 | return {
|
346 |
|
347 | compiledEntries: result,
|
348 |
|
349 |
|
350 | dependencies: compiler.fileDependencies,
|
351 |
|
352 |
|
353 | mainCompilationHash: mainCompilation.hash
|
354 | };
|
355 | }, error => ({
|
356 |
|
357 | error,
|
358 |
|
359 |
|
360 | dependencies: compiler.fileDependencies,
|
361 |
|
362 |
|
363 | mainCompilationHash: mainCompilation.hash
|
364 | }));
|
365 | }
|
366 |
|
367 | |
368 |
|
369 |
|
370 |
|
371 |
|
372 | watchFiles (mainCompilation, files) {
|
373 | fileWatcherApi.watchFiles(mainCompilation, files);
|
374 | }
|
375 | }
|
376 |
|
377 | module.exports = {
|
378 | CachedChildCompilation
|
379 | };
|