1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const parseJson = require("json-parse-better-errors");
|
9 | const asyncLib = require("neo-async");
|
10 | const {
|
11 | SyncHook,
|
12 | SyncBailHook,
|
13 | AsyncParallelHook,
|
14 | AsyncSeriesHook
|
15 | } = require("tapable");
|
16 | const { SizeOnlySource } = require("webpack-sources");
|
17 | const webpack = require("./");
|
18 | const Cache = require("./Cache");
|
19 | const CacheFacade = require("./CacheFacade");
|
20 | const ChunkGraph = require("./ChunkGraph");
|
21 | const Compilation = require("./Compilation");
|
22 | const ConcurrentCompilationError = require("./ConcurrentCompilationError");
|
23 | const ContextModuleFactory = require("./ContextModuleFactory");
|
24 | const ModuleGraph = require("./ModuleGraph");
|
25 | const NormalModuleFactory = require("./NormalModuleFactory");
|
26 | const RequestShortener = require("./RequestShortener");
|
27 | const ResolverFactory = require("./ResolverFactory");
|
28 | const Stats = require("./Stats");
|
29 | const Watching = require("./Watching");
|
30 | const WebpackError = require("./WebpackError");
|
31 | const { Logger } = require("./logging/Logger");
|
32 | const { join, dirname, mkdirp } = require("./util/fs");
|
33 | const { makePathsRelative } = require("./util/identifier");
|
34 | const { isSourceEqual } = require("./util/source");
|
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 | const isSorted = array => {
|
84 | for (let i = 1; i < array.length; i++) {
|
85 | if (array[i - 1] > array[i]) return false;
|
86 | }
|
87 | return true;
|
88 | };
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | const sortObject = (obj, keys) => {
|
96 | const o = {};
|
97 | for (const k of keys.sort()) {
|
98 | o[k] = obj[k];
|
99 | }
|
100 | return o;
|
101 | };
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | const includesHash = (filename, hashes) => {
|
109 | if (!hashes) return false;
|
110 | if (Array.isArray(hashes)) {
|
111 | return hashes.some(hash => filename.includes(hash));
|
112 | } else {
|
113 | return filename.includes(hashes);
|
114 | }
|
115 | };
|
116 |
|
117 | class Compiler {
|
118 | |
119 |
|
120 |
|
121 | constructor(context) {
|
122 | this.hooks = Object.freeze({
|
123 |
|
124 | initialize: new SyncHook([]),
|
125 |
|
126 |
|
127 | shouldEmit: new SyncBailHook(["compilation"]),
|
128 |
|
129 | done: new AsyncSeriesHook(["stats"]),
|
130 |
|
131 | afterDone: new SyncHook(["stats"]),
|
132 |
|
133 | additionalPass: new AsyncSeriesHook([]),
|
134 |
|
135 | beforeRun: new AsyncSeriesHook(["compiler"]),
|
136 |
|
137 | run: new AsyncSeriesHook(["compiler"]),
|
138 |
|
139 | emit: new AsyncSeriesHook(["compilation"]),
|
140 |
|
141 | assetEmitted: new AsyncSeriesHook(["file", "info"]),
|
142 |
|
143 | afterEmit: new AsyncSeriesHook(["compilation"]),
|
144 |
|
145 |
|
146 | thisCompilation: new SyncHook(["compilation", "params"]),
|
147 |
|
148 | compilation: new SyncHook(["compilation", "params"]),
|
149 |
|
150 | normalModuleFactory: new SyncHook(["normalModuleFactory"]),
|
151 |
|
152 | contextModuleFactory: new SyncHook(["contextModuleFactory"]),
|
153 |
|
154 |
|
155 | beforeCompile: new AsyncSeriesHook(["params"]),
|
156 |
|
157 | compile: new SyncHook(["params"]),
|
158 |
|
159 | make: new AsyncParallelHook(["compilation"]),
|
160 |
|
161 | finishMake: new AsyncSeriesHook(["compilation"]),
|
162 |
|
163 | afterCompile: new AsyncSeriesHook(["compilation"]),
|
164 |
|
165 |
|
166 | watchRun: new AsyncSeriesHook(["compiler"]),
|
167 |
|
168 | failed: new SyncHook(["error"]),
|
169 |
|
170 | invalid: new SyncHook(["filename", "changeTime"]),
|
171 |
|
172 | watchClose: new SyncHook([]),
|
173 |
|
174 | shutdown: new AsyncSeriesHook([]),
|
175 |
|
176 |
|
177 | infrastructureLog: new SyncBailHook(["origin", "type", "args"]),
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | environment: new SyncHook([]),
|
183 |
|
184 | afterEnvironment: new SyncHook([]),
|
185 |
|
186 | afterPlugins: new SyncHook(["compiler"]),
|
187 |
|
188 | afterResolvers: new SyncHook(["compiler"]),
|
189 |
|
190 | entryOption: new SyncBailHook(["context", "entry"])
|
191 | });
|
192 |
|
193 | this.webpack = webpack;
|
194 |
|
195 |
|
196 | this.name = undefined;
|
197 |
|
198 | this.parentCompilation = undefined;
|
199 |
|
200 | this.root = this;
|
201 |
|
202 | this.outputPath = "";
|
203 |
|
204 | this.watching = undefined;
|
205 |
|
206 |
|
207 | this.outputFileSystem = null;
|
208 |
|
209 | this.intermediateFileSystem = null;
|
210 |
|
211 | this.inputFileSystem = null;
|
212 |
|
213 | this.watchFileSystem = null;
|
214 |
|
215 |
|
216 | this.recordsInputPath = null;
|
217 |
|
218 | this.recordsOutputPath = null;
|
219 | this.records = {};
|
220 |
|
221 | this.managedPaths = new Set();
|
222 |
|
223 | this.immutablePaths = new Set();
|
224 |
|
225 |
|
226 | this.modifiedFiles = undefined;
|
227 |
|
228 | this.removedFiles = undefined;
|
229 |
|
230 | this.fileTimestamps = undefined;
|
231 |
|
232 | this.contextTimestamps = undefined;
|
233 |
|
234 |
|
235 | this.resolverFactory = new ResolverFactory();
|
236 |
|
237 | this.infrastructureLogger = undefined;
|
238 |
|
239 |
|
240 | this.options = ({});
|
241 |
|
242 | this.context = context;
|
243 |
|
244 | this.requestShortener = new RequestShortener(context, this.root);
|
245 |
|
246 | this.cache = new Cache();
|
247 |
|
248 | this.compilerPath = "";
|
249 |
|
250 |
|
251 | this.running = false;
|
252 |
|
253 |
|
254 | this.idle = false;
|
255 |
|
256 |
|
257 | this.watchMode = false;
|
258 |
|
259 |
|
260 | this._lastCompilation = undefined;
|
261 |
|
262 |
|
263 | this._assetEmittingSourceCache = new WeakMap();
|
264 |
|
265 | this._assetEmittingWrittenFiles = new Map();
|
266 | }
|
267 |
|
268 | |
269 |
|
270 |
|
271 |
|
272 | getCache(name) {
|
273 | return new CacheFacade(this.cache, `${this.compilerPath}${name}`);
|
274 | }
|
275 |
|
276 | |
277 |
|
278 |
|
279 |
|
280 | getInfrastructureLogger(name) {
|
281 | if (!name) {
|
282 | throw new TypeError(
|
283 | "Compiler.getInfrastructureLogger(name) called without a name"
|
284 | );
|
285 | }
|
286 | return new Logger(
|
287 | (type, args) => {
|
288 | if (typeof name === "function") {
|
289 | name = name();
|
290 | if (!name) {
|
291 | throw new TypeError(
|
292 | "Compiler.getInfrastructureLogger(name) called with a function not returning a name"
|
293 | );
|
294 | }
|
295 | }
|
296 | if (this.hooks.infrastructureLog.call(name, type, args) === undefined) {
|
297 | if (this.infrastructureLogger !== undefined) {
|
298 | this.infrastructureLogger(name, type, args);
|
299 | }
|
300 | }
|
301 | },
|
302 | childName => {
|
303 | if (typeof name === "function") {
|
304 | if (typeof childName === "function") {
|
305 | return this.getInfrastructureLogger(() => {
|
306 | if (typeof name === "function") {
|
307 | name = name();
|
308 | if (!name) {
|
309 | throw new TypeError(
|
310 | "Compiler.getInfrastructureLogger(name) called with a function not returning a name"
|
311 | );
|
312 | }
|
313 | }
|
314 | if (typeof childName === "function") {
|
315 | childName = childName();
|
316 | if (!childName) {
|
317 | throw new TypeError(
|
318 | "Logger.getChildLogger(name) called with a function not returning a name"
|
319 | );
|
320 | }
|
321 | }
|
322 | return `${name}/${childName}`;
|
323 | });
|
324 | } else {
|
325 | return this.getInfrastructureLogger(() => {
|
326 | if (typeof name === "function") {
|
327 | name = name();
|
328 | if (!name) {
|
329 | throw new TypeError(
|
330 | "Compiler.getInfrastructureLogger(name) called with a function not returning a name"
|
331 | );
|
332 | }
|
333 | }
|
334 | return `${name}/${childName}`;
|
335 | });
|
336 | }
|
337 | } else {
|
338 | if (typeof childName === "function") {
|
339 | return this.getInfrastructureLogger(() => {
|
340 | if (typeof childName === "function") {
|
341 | childName = childName();
|
342 | if (!childName) {
|
343 | throw new TypeError(
|
344 | "Logger.getChildLogger(name) called with a function not returning a name"
|
345 | );
|
346 | }
|
347 | }
|
348 | return `${name}/${childName}`;
|
349 | });
|
350 | } else {
|
351 | return this.getInfrastructureLogger(`${name}/${childName}`);
|
352 | }
|
353 | }
|
354 | }
|
355 | );
|
356 | }
|
357 |
|
358 |
|
359 |
|
360 | _cleanupLastCompilation() {
|
361 | if (this._lastCompilation !== undefined) {
|
362 | for (const module of this._lastCompilation.modules) {
|
363 | ChunkGraph.clearChunkGraphForModule(module);
|
364 | ModuleGraph.clearModuleGraphForModule(module);
|
365 | module.cleanupForCache();
|
366 | }
|
367 | for (const chunk of this._lastCompilation.chunks) {
|
368 | ChunkGraph.clearChunkGraphForChunk(chunk);
|
369 | }
|
370 | this._lastCompilation = undefined;
|
371 | }
|
372 | }
|
373 |
|
374 | |
375 |
|
376 |
|
377 |
|
378 |
|
379 | watch(watchOptions, handler) {
|
380 | if (this.running) {
|
381 | return handler(new ConcurrentCompilationError());
|
382 | }
|
383 |
|
384 | this.running = true;
|
385 | this.watchMode = true;
|
386 | this.watching = new Watching(this, watchOptions, handler);
|
387 | return this.watching;
|
388 | }
|
389 |
|
390 | |
391 |
|
392 |
|
393 |
|
394 | run(callback) {
|
395 | if (this.running) {
|
396 | return callback(new ConcurrentCompilationError());
|
397 | }
|
398 |
|
399 | let logger;
|
400 |
|
401 | const finalCallback = (err, stats) => {
|
402 | if (logger) logger.time("beginIdle");
|
403 | this.idle = true;
|
404 | this.cache.beginIdle();
|
405 | this.idle = true;
|
406 | if (logger) logger.timeEnd("beginIdle");
|
407 | this.running = false;
|
408 | if (err) {
|
409 | this.hooks.failed.call(err);
|
410 | }
|
411 | if (callback !== undefined) callback(err, stats);
|
412 | this.hooks.afterDone.call(stats);
|
413 | };
|
414 |
|
415 | const startTime = Date.now();
|
416 |
|
417 | this.running = true;
|
418 |
|
419 | const onCompiled = (err, compilation) => {
|
420 | if (err) return finalCallback(err);
|
421 |
|
422 | if (this.hooks.shouldEmit.call(compilation) === false) {
|
423 | compilation.startTime = startTime;
|
424 | compilation.endTime = Date.now();
|
425 | const stats = new Stats(compilation);
|
426 | this.hooks.done.callAsync(stats, err => {
|
427 | if (err) return finalCallback(err);
|
428 | return finalCallback(null, stats);
|
429 | });
|
430 | return;
|
431 | }
|
432 |
|
433 | process.nextTick(() => {
|
434 | logger = compilation.getLogger("webpack.Compiler");
|
435 | logger.time("emitAssets");
|
436 | this.emitAssets(compilation, err => {
|
437 | logger.timeEnd("emitAssets");
|
438 | if (err) return finalCallback(err);
|
439 |
|
440 | if (compilation.hooks.needAdditionalPass.call()) {
|
441 | compilation.needAdditionalPass = true;
|
442 |
|
443 | compilation.startTime = startTime;
|
444 | compilation.endTime = Date.now();
|
445 | logger.time("done hook");
|
446 | const stats = new Stats(compilation);
|
447 | this.hooks.done.callAsync(stats, err => {
|
448 | logger.timeEnd("done hook");
|
449 | if (err) return finalCallback(err);
|
450 |
|
451 | this.hooks.additionalPass.callAsync(err => {
|
452 | if (err) return finalCallback(err);
|
453 | this.compile(onCompiled);
|
454 | });
|
455 | });
|
456 | return;
|
457 | }
|
458 |
|
459 | logger.time("emitRecords");
|
460 | this.emitRecords(err => {
|
461 | logger.timeEnd("emitRecords");
|
462 | if (err) return finalCallback(err);
|
463 |
|
464 | compilation.startTime = startTime;
|
465 | compilation.endTime = Date.now();
|
466 | logger.time("done hook");
|
467 | const stats = new Stats(compilation);
|
468 | this.hooks.done.callAsync(stats, err => {
|
469 | logger.timeEnd("done hook");
|
470 | if (err) return finalCallback(err);
|
471 | this.cache.storeBuildDependencies(
|
472 | compilation.buildDependencies,
|
473 | err => {
|
474 | if (err) return finalCallback(err);
|
475 | return finalCallback(null, stats);
|
476 | }
|
477 | );
|
478 | });
|
479 | });
|
480 | });
|
481 | });
|
482 | };
|
483 |
|
484 | const run = () => {
|
485 | this.hooks.beforeRun.callAsync(this, err => {
|
486 | if (err) return finalCallback(err);
|
487 |
|
488 | this.hooks.run.callAsync(this, err => {
|
489 | if (err) return finalCallback(err);
|
490 |
|
491 | this.readRecords(err => {
|
492 | if (err) return finalCallback(err);
|
493 |
|
494 | this.compile(onCompiled);
|
495 | });
|
496 | });
|
497 | });
|
498 | };
|
499 |
|
500 | if (this.idle) {
|
501 | this.cache.endIdle(err => {
|
502 | if (err) return finalCallback(err);
|
503 |
|
504 | this.idle = false;
|
505 | run();
|
506 | });
|
507 | } else {
|
508 | run();
|
509 | }
|
510 | }
|
511 |
|
512 | |
513 |
|
514 |
|
515 |
|
516 | runAsChild(callback) {
|
517 | const startTime = Date.now();
|
518 | this.compile((err, compilation) => {
|
519 | if (err) return callback(err);
|
520 |
|
521 | this.parentCompilation.children.push(compilation);
|
522 | for (const { name, source, info } of compilation.getAssets()) {
|
523 | this.parentCompilation.emitAsset(name, source, info);
|
524 | }
|
525 |
|
526 | const entries = [];
|
527 | for (const ep of compilation.entrypoints.values()) {
|
528 | entries.push(...ep.chunks);
|
529 | }
|
530 |
|
531 | compilation.startTime = startTime;
|
532 | compilation.endTime = Date.now();
|
533 |
|
534 | return callback(null, entries, compilation);
|
535 | });
|
536 | }
|
537 |
|
538 | purgeInputFileSystem() {
|
539 | if (this.inputFileSystem && this.inputFileSystem.purge) {
|
540 | this.inputFileSystem.purge();
|
541 | }
|
542 | }
|
543 |
|
544 | |
545 |
|
546 |
|
547 |
|
548 |
|
549 | emitAssets(compilation, callback) {
|
550 | let outputPath;
|
551 |
|
552 | const emitFiles = err => {
|
553 | if (err) return callback(err);
|
554 |
|
555 | const assets = compilation.getAssets();
|
556 | compilation.assets = { ...compilation.assets };
|
557 |
|
558 | const caseInsensitiveMap = new Map();
|
559 | asyncLib.forEachLimit(
|
560 | assets,
|
561 | 15,
|
562 | ({ name: file, source, info }, callback) => {
|
563 | let targetFile = file;
|
564 | let immutable = info.immutable;
|
565 | const queryStringIdx = targetFile.indexOf("?");
|
566 | if (queryStringIdx >= 0) {
|
567 | targetFile = targetFile.substr(0, queryStringIdx);
|
568 |
|
569 |
|
570 |
|
571 | immutable =
|
572 | immutable &&
|
573 | (includesHash(targetFile, info.contenthash) ||
|
574 | includesHash(targetFile, info.chunkhash) ||
|
575 | includesHash(targetFile, info.modulehash) ||
|
576 | includesHash(targetFile, info.fullhash));
|
577 | }
|
578 |
|
579 | const writeOut = err => {
|
580 | if (err) return callback(err);
|
581 | const targetPath = join(
|
582 | this.outputFileSystem,
|
583 | outputPath,
|
584 | targetFile
|
585 | );
|
586 |
|
587 |
|
588 | const targetFileGeneration = this._assetEmittingWrittenFiles.get(
|
589 | targetPath
|
590 | );
|
591 |
|
592 |
|
593 | let cacheEntry = this._assetEmittingSourceCache.get(source);
|
594 | if (cacheEntry === undefined) {
|
595 | cacheEntry = {
|
596 | sizeOnlySource: undefined,
|
597 | writtenTo: new Map()
|
598 | };
|
599 | this._assetEmittingSourceCache.set(source, cacheEntry);
|
600 | }
|
601 |
|
602 | let similarEntry;
|
603 |
|
604 | const checkSimilarFile = () => {
|
605 | const caseInsensitiveTargetPath = targetPath.toLowerCase();
|
606 | similarEntry = caseInsensitiveMap.get(caseInsensitiveTargetPath);
|
607 | if (similarEntry !== undefined) {
|
608 | const { path: other, source: otherSource } = similarEntry;
|
609 | if (isSourceEqual(otherSource, source)) {
|
610 |
|
611 |
|
612 | if (similarEntry.size !== undefined) {
|
613 | updateWithReplacementSource(similarEntry.size);
|
614 | } else {
|
615 | if (!similarEntry.waiting) similarEntry.waiting = [];
|
616 | similarEntry.waiting.push({ file, cacheEntry });
|
617 | }
|
618 | alreadyWritten();
|
619 | } else {
|
620 | const err = new WebpackError(`Prevent writing to file that only differs in casing or query string from already written file.
|
621 | This will lead to a race-condition and corrupted files on case-insensitive file systems.
|
622 | ${targetPath}
|
623 | ${other}`);
|
624 | err.file = file;
|
625 | callback(err);
|
626 | }
|
627 | return true;
|
628 | } else {
|
629 | caseInsensitiveMap.set(
|
630 | caseInsensitiveTargetPath,
|
631 | (similarEntry = {
|
632 | path: targetPath,
|
633 | source,
|
634 | size: undefined,
|
635 | waiting: undefined
|
636 | })
|
637 | );
|
638 | return false;
|
639 | }
|
640 | };
|
641 |
|
642 | |
643 |
|
644 |
|
645 |
|
646 | const getContent = () => {
|
647 | if (typeof source.buffer === "function") {
|
648 | return source.buffer();
|
649 | } else {
|
650 | const bufferOrString = source.source();
|
651 | if (Buffer.isBuffer(bufferOrString)) {
|
652 | return bufferOrString;
|
653 | } else {
|
654 | return Buffer.from(bufferOrString, "utf8");
|
655 | }
|
656 | }
|
657 | };
|
658 |
|
659 | const alreadyWritten = () => {
|
660 |
|
661 | if (targetFileGeneration === undefined) {
|
662 | const newGeneration = 1;
|
663 | this._assetEmittingWrittenFiles.set(targetPath, newGeneration);
|
664 | cacheEntry.writtenTo.set(targetPath, newGeneration);
|
665 | } else {
|
666 | cacheEntry.writtenTo.set(targetPath, targetFileGeneration);
|
667 | }
|
668 | callback();
|
669 | };
|
670 |
|
671 | |
672 |
|
673 |
|
674 |
|
675 |
|
676 | const doWrite = content => {
|
677 | this.outputFileSystem.writeFile(targetPath, content, err => {
|
678 | if (err) return callback(err);
|
679 |
|
680 |
|
681 | compilation.emittedAssets.add(file);
|
682 |
|
683 |
|
684 | const newGeneration =
|
685 | targetFileGeneration === undefined
|
686 | ? 1
|
687 | : targetFileGeneration + 1;
|
688 | cacheEntry.writtenTo.set(targetPath, newGeneration);
|
689 | this._assetEmittingWrittenFiles.set(targetPath, newGeneration);
|
690 | this.hooks.assetEmitted.callAsync(
|
691 | file,
|
692 | {
|
693 | content,
|
694 | source,
|
695 | outputPath,
|
696 | compilation,
|
697 | targetPath
|
698 | },
|
699 | callback
|
700 | );
|
701 | });
|
702 | };
|
703 |
|
704 | const updateWithReplacementSource = size => {
|
705 | updateFileWithReplacementSource(file, cacheEntry, size);
|
706 | similarEntry.size = size;
|
707 | if (similarEntry.waiting !== undefined) {
|
708 | for (const { file, cacheEntry } of similarEntry.waiting) {
|
709 | updateFileWithReplacementSource(file, cacheEntry, size);
|
710 | }
|
711 | }
|
712 | };
|
713 |
|
714 | const updateFileWithReplacementSource = (
|
715 | file,
|
716 | cacheEntry,
|
717 | size
|
718 | ) => {
|
719 |
|
720 |
|
721 |
|
722 | if (!cacheEntry.sizeOnlySource) {
|
723 | cacheEntry.sizeOnlySource = new SizeOnlySource(size);
|
724 | }
|
725 | compilation.updateAsset(file, cacheEntry.sizeOnlySource, {
|
726 | size
|
727 | });
|
728 | };
|
729 |
|
730 | const processExistingFile = stats => {
|
731 |
|
732 | if (immutable) {
|
733 | updateWithReplacementSource(stats.size);
|
734 | return alreadyWritten();
|
735 | }
|
736 |
|
737 | const content = getContent();
|
738 |
|
739 | updateWithReplacementSource(content.length);
|
740 |
|
741 |
|
742 |
|
743 |
|
744 |
|
745 | if (content.length === stats.size) {
|
746 | compilation.comparedForEmitAssets.add(file);
|
747 | return this.outputFileSystem.readFile(
|
748 | targetPath,
|
749 | (err, existingContent) => {
|
750 | if (
|
751 | err ||
|
752 | !content.equals( (existingContent))
|
753 | ) {
|
754 | return doWrite(content);
|
755 | } else {
|
756 | return alreadyWritten();
|
757 | }
|
758 | }
|
759 | );
|
760 | }
|
761 |
|
762 | return doWrite(content);
|
763 | };
|
764 |
|
765 | const processMissingFile = () => {
|
766 | const content = getContent();
|
767 |
|
768 | updateWithReplacementSource(content.length);
|
769 |
|
770 | return doWrite(content);
|
771 | };
|
772 |
|
773 |
|
774 | if (targetFileGeneration !== undefined) {
|
775 |
|
776 | const writtenGeneration = cacheEntry.writtenTo.get(targetPath);
|
777 | if (writtenGeneration === targetFileGeneration) {
|
778 |
|
779 |
|
780 |
|
781 |
|
782 | compilation.updateAsset(file, cacheEntry.sizeOnlySource, {
|
783 | size: cacheEntry.sizeOnlySource.size()
|
784 | });
|
785 |
|
786 | return callback();
|
787 | }
|
788 |
|
789 | if (!immutable) {
|
790 | if (checkSimilarFile()) return;
|
791 |
|
792 |
|
793 |
|
794 | return processMissingFile();
|
795 | }
|
796 | }
|
797 |
|
798 | if (checkSimilarFile()) return;
|
799 | if (this.options.output.compareBeforeEmit) {
|
800 | this.outputFileSystem.stat(targetPath, (err, stats) => {
|
801 | const exists = !err && stats.isFile();
|
802 |
|
803 | if (exists) {
|
804 | processExistingFile(stats);
|
805 | } else {
|
806 | processMissingFile();
|
807 | }
|
808 | });
|
809 | } else {
|
810 | processMissingFile();
|
811 | }
|
812 | };
|
813 |
|
814 | if (targetFile.match(/\/|\\/)) {
|
815 | const fs = this.outputFileSystem;
|
816 | const dir = dirname(fs, join(fs, outputPath, targetFile));
|
817 | mkdirp(fs, dir, writeOut);
|
818 | } else {
|
819 | writeOut();
|
820 | }
|
821 | },
|
822 | err => {
|
823 |
|
824 | caseInsensitiveMap.clear();
|
825 | if (err) return callback(err);
|
826 |
|
827 | this.hooks.afterEmit.callAsync(compilation, err => {
|
828 | if (err) return callback(err);
|
829 |
|
830 | return callback();
|
831 | });
|
832 | }
|
833 | );
|
834 | };
|
835 |
|
836 | this.hooks.emit.callAsync(compilation, err => {
|
837 | if (err) return callback(err);
|
838 | outputPath = compilation.getPath(this.outputPath, {});
|
839 | mkdirp(this.outputFileSystem, outputPath, emitFiles);
|
840 | });
|
841 | }
|
842 |
|
843 | |
844 |
|
845 |
|
846 |
|
847 | emitRecords(callback) {
|
848 | if (!this.recordsOutputPath) return callback();
|
849 |
|
850 | const writeFile = () => {
|
851 | this.outputFileSystem.writeFile(
|
852 | this.recordsOutputPath,
|
853 | JSON.stringify(
|
854 | this.records,
|
855 | (n, value) => {
|
856 | if (
|
857 | typeof value === "object" &&
|
858 | value !== null &&
|
859 | !Array.isArray(value)
|
860 | ) {
|
861 | const keys = Object.keys(value);
|
862 | if (!isSorted(keys)) {
|
863 | return sortObject(value, keys);
|
864 | }
|
865 | }
|
866 | return value;
|
867 | },
|
868 | 2
|
869 | ),
|
870 | callback
|
871 | );
|
872 | };
|
873 |
|
874 | const recordsOutputPathDirectory = dirname(
|
875 | this.outputFileSystem,
|
876 | this.recordsOutputPath
|
877 | );
|
878 | if (!recordsOutputPathDirectory) {
|
879 | return writeFile();
|
880 | }
|
881 | mkdirp(this.outputFileSystem, recordsOutputPathDirectory, err => {
|
882 | if (err) return callback(err);
|
883 | writeFile();
|
884 | });
|
885 | }
|
886 |
|
887 | |
888 |
|
889 |
|
890 |
|
891 | readRecords(callback) {
|
892 | if (!this.recordsInputPath) {
|
893 | this.records = {};
|
894 | return callback();
|
895 | }
|
896 | this.inputFileSystem.stat(this.recordsInputPath, err => {
|
897 |
|
898 |
|
899 | if (err) return callback();
|
900 |
|
901 | this.inputFileSystem.readFile(this.recordsInputPath, (err, content) => {
|
902 | if (err) return callback(err);
|
903 |
|
904 | try {
|
905 | this.records = parseJson(content.toString("utf-8"));
|
906 | } catch (e) {
|
907 | e.message = "Cannot parse records: " + e.message;
|
908 | return callback(e);
|
909 | }
|
910 |
|
911 | return callback();
|
912 | });
|
913 | });
|
914 | }
|
915 |
|
916 | |
917 |
|
918 |
|
919 |
|
920 |
|
921 |
|
922 |
|
923 |
|
924 | createChildCompiler(
|
925 | compilation,
|
926 | compilerName,
|
927 | compilerIndex,
|
928 | outputOptions,
|
929 | plugins
|
930 | ) {
|
931 | const childCompiler = new Compiler(this.context);
|
932 | childCompiler.name = compilerName;
|
933 | childCompiler.outputPath = this.outputPath;
|
934 | childCompiler.inputFileSystem = this.inputFileSystem;
|
935 | childCompiler.outputFileSystem = null;
|
936 | childCompiler.resolverFactory = this.resolverFactory;
|
937 | childCompiler.modifiedFiles = this.modifiedFiles;
|
938 | childCompiler.removedFiles = this.removedFiles;
|
939 | childCompiler.fileTimestamps = this.fileTimestamps;
|
940 | childCompiler.contextTimestamps = this.contextTimestamps;
|
941 | childCompiler.cache = this.cache;
|
942 | childCompiler.compilerPath = `${this.compilerPath}${compilerName}|${compilerIndex}|`;
|
943 |
|
944 | const relativeCompilerName = makePathsRelative(
|
945 | this.context,
|
946 | compilerName,
|
947 | this.root
|
948 | );
|
949 | if (!this.records[relativeCompilerName]) {
|
950 | this.records[relativeCompilerName] = [];
|
951 | }
|
952 | if (this.records[relativeCompilerName][compilerIndex]) {
|
953 | childCompiler.records = this.records[relativeCompilerName][compilerIndex];
|
954 | } else {
|
955 | this.records[relativeCompilerName].push((childCompiler.records = {}));
|
956 | }
|
957 |
|
958 | childCompiler.options = {
|
959 | ...this.options,
|
960 | output: {
|
961 | ...this.options.output,
|
962 | ...outputOptions
|
963 | }
|
964 | };
|
965 | childCompiler.parentCompilation = compilation;
|
966 | childCompiler.root = this.root;
|
967 | if (Array.isArray(plugins)) {
|
968 | for (const plugin of plugins) {
|
969 | plugin.apply(childCompiler);
|
970 | }
|
971 | }
|
972 | for (const name in this.hooks) {
|
973 | if (
|
974 | ![
|
975 | "make",
|
976 | "compile",
|
977 | "emit",
|
978 | "afterEmit",
|
979 | "invalid",
|
980 | "done",
|
981 | "thisCompilation"
|
982 | ].includes(name)
|
983 | ) {
|
984 | if (childCompiler.hooks[name]) {
|
985 | childCompiler.hooks[name].taps = this.hooks[name].taps.slice();
|
986 | }
|
987 | }
|
988 | }
|
989 |
|
990 | compilation.hooks.childCompiler.call(
|
991 | childCompiler,
|
992 | compilerName,
|
993 | compilerIndex
|
994 | );
|
995 |
|
996 | return childCompiler;
|
997 | }
|
998 |
|
999 | isChild() {
|
1000 | return !!this.parentCompilation;
|
1001 | }
|
1002 |
|
1003 | createCompilation() {
|
1004 | this._cleanupLastCompilation();
|
1005 | return (this._lastCompilation = new Compilation(this));
|
1006 | }
|
1007 |
|
1008 | |
1009 |
|
1010 |
|
1011 |
|
1012 | newCompilation(params) {
|
1013 | const compilation = this.createCompilation();
|
1014 | compilation.name = this.name;
|
1015 | compilation.records = this.records;
|
1016 | this.hooks.thisCompilation.call(compilation, params);
|
1017 | this.hooks.compilation.call(compilation, params);
|
1018 | return compilation;
|
1019 | }
|
1020 |
|
1021 | createNormalModuleFactory() {
|
1022 | const normalModuleFactory = new NormalModuleFactory({
|
1023 | context: this.options.context,
|
1024 | fs: this.inputFileSystem,
|
1025 | resolverFactory: this.resolverFactory,
|
1026 | options: this.options.module,
|
1027 | associatedObjectForCache: this.root,
|
1028 | layers: this.options.experiments.layers
|
1029 | });
|
1030 | this.hooks.normalModuleFactory.call(normalModuleFactory);
|
1031 | return normalModuleFactory;
|
1032 | }
|
1033 |
|
1034 | createContextModuleFactory() {
|
1035 | const contextModuleFactory = new ContextModuleFactory(this.resolverFactory);
|
1036 | this.hooks.contextModuleFactory.call(contextModuleFactory);
|
1037 | return contextModuleFactory;
|
1038 | }
|
1039 |
|
1040 | newCompilationParams() {
|
1041 | const params = {
|
1042 | normalModuleFactory: this.createNormalModuleFactory(),
|
1043 | contextModuleFactory: this.createContextModuleFactory()
|
1044 | };
|
1045 | return params;
|
1046 | }
|
1047 |
|
1048 | |
1049 |
|
1050 |
|
1051 |
|
1052 | compile(callback) {
|
1053 | const params = this.newCompilationParams();
|
1054 | this.hooks.beforeCompile.callAsync(params, err => {
|
1055 | if (err) return callback(err);
|
1056 |
|
1057 | this.hooks.compile.call(params);
|
1058 |
|
1059 | const compilation = this.newCompilation(params);
|
1060 |
|
1061 | const logger = compilation.getLogger("webpack.Compiler");
|
1062 |
|
1063 | logger.time("make hook");
|
1064 | this.hooks.make.callAsync(compilation, err => {
|
1065 | logger.timeEnd("make hook");
|
1066 | if (err) return callback(err);
|
1067 |
|
1068 | logger.time("finish make hook");
|
1069 | this.hooks.finishMake.callAsync(compilation, err => {
|
1070 | logger.timeEnd("finish make hook");
|
1071 | if (err) return callback(err);
|
1072 |
|
1073 | process.nextTick(() => {
|
1074 | logger.time("finish compilation");
|
1075 | compilation.finish(err => {
|
1076 | logger.timeEnd("finish compilation");
|
1077 | if (err) return callback(err);
|
1078 |
|
1079 | logger.time("seal compilation");
|
1080 | compilation.seal(err => {
|
1081 | logger.timeEnd("seal compilation");
|
1082 | if (err) return callback(err);
|
1083 |
|
1084 | logger.time("afterCompile hook");
|
1085 | this.hooks.afterCompile.callAsync(compilation, err => {
|
1086 | logger.timeEnd("afterCompile hook");
|
1087 | if (err) return callback(err);
|
1088 |
|
1089 | return callback(null, compilation);
|
1090 | });
|
1091 | });
|
1092 | });
|
1093 | });
|
1094 | });
|
1095 | });
|
1096 | });
|
1097 | }
|
1098 |
|
1099 | |
1100 |
|
1101 |
|
1102 |
|
1103 | close(callback) {
|
1104 | this.hooks.shutdown.callAsync(err => {
|
1105 | if (err) return callback(err);
|
1106 |
|
1107 |
|
1108 |
|
1109 | this._lastCompilation = undefined;
|
1110 | this.cache.shutdown(callback);
|
1111 | });
|
1112 | }
|
1113 | }
|
1114 |
|
1115 | module.exports = Compiler;
|