Compiler.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const parseJson = require("json-parse-even-better-errors");
  7. const asyncLib = require("neo-async");
  8. const {
  9. SyncHook,
  10. SyncBailHook,
  11. AsyncParallelHook,
  12. AsyncSeriesHook
  13. } = require("tapable");
  14. const { SizeOnlySource } = require("webpack-sources");
  15. const webpack = require("./");
  16. const Cache = require("./Cache");
  17. const CacheFacade = require("./CacheFacade");
  18. const ChunkGraph = require("./ChunkGraph");
  19. const Compilation = require("./Compilation");
  20. const ConcurrentCompilationError = require("./ConcurrentCompilationError");
  21. const ContextModuleFactory = require("./ContextModuleFactory");
  22. const ModuleGraph = require("./ModuleGraph");
  23. const NormalModuleFactory = require("./NormalModuleFactory");
  24. const RequestShortener = require("./RequestShortener");
  25. const ResolverFactory = require("./ResolverFactory");
  26. const Stats = require("./Stats");
  27. const Watching = require("./Watching");
  28. const WebpackError = require("./WebpackError");
  29. const { Logger } = require("./logging/Logger");
  30. const { join, dirname, mkdirp } = require("./util/fs");
  31. const { makePathsRelative } = require("./util/identifier");
  32. const { isSourceEqual } = require("./util/source");
  33. /** @typedef {import("webpack-sources").Source} Source */
  34. /** @typedef {import("../declarations/WebpackOptions").EntryNormalized} Entry */
  35. /** @typedef {import("../declarations/WebpackOptions").OutputNormalized} OutputOptions */
  36. /** @typedef {import("../declarations/WebpackOptions").WatchOptions} WatchOptions */
  37. /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
  38. /** @typedef {import("../declarations/WebpackOptions").WebpackPluginInstance} WebpackPluginInstance */
  39. /** @typedef {import("./Chunk")} Chunk */
  40. /** @typedef {import("./Compilation").References} References */
  41. /** @typedef {import("./Dependency")} Dependency */
  42. /** @typedef {import("./FileSystemInfo").FileSystemInfoEntry} FileSystemInfoEntry */
  43. /** @typedef {import("./Module")} Module */
  44. /** @typedef {import("./Module").BuildInfo} BuildInfo */
  45. /** @typedef {import("./config/target").PlatformTargetProperties} PlatformTargetProperties */
  46. /** @typedef {import("./logging/createConsoleLogger").LoggingFunction} LoggingFunction */
  47. /** @typedef {import("./util/WeakTupleMap")} WeakTupleMap */
  48. /** @typedef {import("./util/fs").IStats} IStats */
  49. /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
  50. /** @typedef {import("./util/fs").IntermediateFileSystem} IntermediateFileSystem */
  51. /** @typedef {import("./util/fs").OutputFileSystem} OutputFileSystem */
  52. /** @typedef {import("./util/fs").WatchFileSystem} WatchFileSystem */
  53. /**
  54. * @typedef {object} CompilationParams
  55. * @property {NormalModuleFactory} normalModuleFactory
  56. * @property {ContextModuleFactory} contextModuleFactory
  57. */
  58. /**
  59. * @template T
  60. * @callback RunCallback
  61. * @param {Error | null} err
  62. * @param {T=} result
  63. */
  64. /**
  65. * @template T
  66. * @callback Callback
  67. * @param {(Error | null)=} err
  68. * @param {T=} result
  69. */
  70. /**
  71. * @callback RunAsChildCallback
  72. * @param {Error | null} err
  73. * @param {Chunk[]=} entries
  74. * @param {Compilation=} compilation
  75. */
  76. /**
  77. * @typedef {object} AssetEmittedInfo
  78. * @property {Buffer} content
  79. * @property {Source} source
  80. * @property {Compilation} compilation
  81. * @property {string} outputPath
  82. * @property {string} targetPath
  83. */
  84. /** @typedef {{ sizeOnlySource: SizeOnlySource | undefined, writtenTo: Map<string, number> }} CacheEntry */
  85. /** @typedef {{ path: string, source: Source, size: number | undefined, waiting: ({ cacheEntry: any, file: string }[] | undefined) }} SimilarEntry */
  86. /**
  87. * @param {string[]} array an array
  88. * @returns {boolean} true, if the array is sorted
  89. */
  90. const isSorted = array => {
  91. for (let i = 1; i < array.length; i++) {
  92. if (array[i - 1] > array[i]) return false;
  93. }
  94. return true;
  95. };
  96. /**
  97. * @param {{[key: string]: any}} obj an object
  98. * @param {string[]} keys the keys of the object
  99. * @returns {{[key: string]: any}} the object with properties sorted by property name
  100. */
  101. const sortObject = (obj, keys) => {
  102. /** @type {{[key: string]: any}} */
  103. const o = {};
  104. for (const k of keys.sort()) {
  105. o[k] = obj[k];
  106. }
  107. return o;
  108. };
  109. /**
  110. * @param {string} filename filename
  111. * @param {string | string[] | undefined} hashes list of hashes
  112. * @returns {boolean} true, if the filename contains any hash
  113. */
  114. const includesHash = (filename, hashes) => {
  115. if (!hashes) return false;
  116. if (Array.isArray(hashes)) {
  117. return hashes.some(hash => filename.includes(hash));
  118. } else {
  119. return filename.includes(hashes);
  120. }
  121. };
  122. class Compiler {
  123. /**
  124. * @param {string} context the compilation path
  125. * @param {WebpackOptions} options options
  126. */
  127. constructor(context, options = /** @type {WebpackOptions} */ ({})) {
  128. this.hooks = Object.freeze({
  129. /** @type {SyncHook<[]>} */
  130. initialize: new SyncHook([]),
  131. /** @type {SyncBailHook<[Compilation], boolean | undefined>} */
  132. shouldEmit: new SyncBailHook(["compilation"]),
  133. /** @type {AsyncSeriesHook<[Stats]>} */
  134. done: new AsyncSeriesHook(["stats"]),
  135. /** @type {SyncHook<[Stats]>} */
  136. afterDone: new SyncHook(["stats"]),
  137. /** @type {AsyncSeriesHook<[]>} */
  138. additionalPass: new AsyncSeriesHook([]),
  139. /** @type {AsyncSeriesHook<[Compiler]>} */
  140. beforeRun: new AsyncSeriesHook(["compiler"]),
  141. /** @type {AsyncSeriesHook<[Compiler]>} */
  142. run: new AsyncSeriesHook(["compiler"]),
  143. /** @type {AsyncSeriesHook<[Compilation]>} */
  144. emit: new AsyncSeriesHook(["compilation"]),
  145. /** @type {AsyncSeriesHook<[string, AssetEmittedInfo]>} */
  146. assetEmitted: new AsyncSeriesHook(["file", "info"]),
  147. /** @type {AsyncSeriesHook<[Compilation]>} */
  148. afterEmit: new AsyncSeriesHook(["compilation"]),
  149. /** @type {SyncHook<[Compilation, CompilationParams]>} */
  150. thisCompilation: new SyncHook(["compilation", "params"]),
  151. /** @type {SyncHook<[Compilation, CompilationParams]>} */
  152. compilation: new SyncHook(["compilation", "params"]),
  153. /** @type {SyncHook<[NormalModuleFactory]>} */
  154. normalModuleFactory: new SyncHook(["normalModuleFactory"]),
  155. /** @type {SyncHook<[ContextModuleFactory]>} */
  156. contextModuleFactory: new SyncHook(["contextModuleFactory"]),
  157. /** @type {AsyncSeriesHook<[CompilationParams]>} */
  158. beforeCompile: new AsyncSeriesHook(["params"]),
  159. /** @type {SyncHook<[CompilationParams]>} */
  160. compile: new SyncHook(["params"]),
  161. /** @type {AsyncParallelHook<[Compilation]>} */
  162. make: new AsyncParallelHook(["compilation"]),
  163. /** @type {AsyncParallelHook<[Compilation]>} */
  164. finishMake: new AsyncSeriesHook(["compilation"]),
  165. /** @type {AsyncSeriesHook<[Compilation]>} */
  166. afterCompile: new AsyncSeriesHook(["compilation"]),
  167. /** @type {AsyncSeriesHook<[]>} */
  168. readRecords: new AsyncSeriesHook([]),
  169. /** @type {AsyncSeriesHook<[]>} */
  170. emitRecords: new AsyncSeriesHook([]),
  171. /** @type {AsyncSeriesHook<[Compiler]>} */
  172. watchRun: new AsyncSeriesHook(["compiler"]),
  173. /** @type {SyncHook<[Error]>} */
  174. failed: new SyncHook(["error"]),
  175. /** @type {SyncHook<[string | null, number]>} */
  176. invalid: new SyncHook(["filename", "changeTime"]),
  177. /** @type {SyncHook<[]>} */
  178. watchClose: new SyncHook([]),
  179. /** @type {AsyncSeriesHook<[]>} */
  180. shutdown: new AsyncSeriesHook([]),
  181. /** @type {SyncBailHook<[string, string, any[]], true>} */
  182. infrastructureLog: new SyncBailHook(["origin", "type", "args"]),
  183. // TODO the following hooks are weirdly located here
  184. // TODO move them for webpack 5
  185. /** @type {SyncHook<[]>} */
  186. environment: new SyncHook([]),
  187. /** @type {SyncHook<[]>} */
  188. afterEnvironment: new SyncHook([]),
  189. /** @type {SyncHook<[Compiler]>} */
  190. afterPlugins: new SyncHook(["compiler"]),
  191. /** @type {SyncHook<[Compiler]>} */
  192. afterResolvers: new SyncHook(["compiler"]),
  193. /** @type {SyncBailHook<[string, Entry], boolean>} */
  194. entryOption: new SyncBailHook(["context", "entry"])
  195. });
  196. this.webpack = webpack;
  197. /** @type {string | undefined} */
  198. this.name = undefined;
  199. /** @type {Compilation | undefined} */
  200. this.parentCompilation = undefined;
  201. /** @type {Compiler} */
  202. this.root = this;
  203. /** @type {string} */
  204. this.outputPath = "";
  205. /** @type {Watching | undefined} */
  206. this.watching = undefined;
  207. /** @type {OutputFileSystem | null} */
  208. this.outputFileSystem = null;
  209. /** @type {IntermediateFileSystem | null} */
  210. this.intermediateFileSystem = null;
  211. /** @type {InputFileSystem | null} */
  212. this.inputFileSystem = null;
  213. /** @type {WatchFileSystem | null} */
  214. this.watchFileSystem = null;
  215. /** @type {string|null} */
  216. this.recordsInputPath = null;
  217. /** @type {string|null} */
  218. this.recordsOutputPath = null;
  219. /** @type {Record<string, TODO>} */
  220. this.records = {};
  221. /** @type {Set<string | RegExp>} */
  222. this.managedPaths = new Set();
  223. /** @type {Set<string | RegExp>} */
  224. this.unmanagedPaths = new Set();
  225. /** @type {Set<string | RegExp>} */
  226. this.immutablePaths = new Set();
  227. /** @type {ReadonlySet<string> | undefined} */
  228. this.modifiedFiles = undefined;
  229. /** @type {ReadonlySet<string> | undefined} */
  230. this.removedFiles = undefined;
  231. /** @type {ReadonlyMap<string, FileSystemInfoEntry | "ignore" | null> | undefined} */
  232. this.fileTimestamps = undefined;
  233. /** @type {ReadonlyMap<string, FileSystemInfoEntry | "ignore" | null> | undefined} */
  234. this.contextTimestamps = undefined;
  235. /** @type {number | undefined} */
  236. this.fsStartTime = undefined;
  237. /** @type {ResolverFactory} */
  238. this.resolverFactory = new ResolverFactory();
  239. /** @type {LoggingFunction | undefined} */
  240. this.infrastructureLogger = undefined;
  241. /** @type {Readonly<PlatformTargetProperties>} */
  242. this.platform = {
  243. web: null,
  244. browser: null,
  245. webworker: null,
  246. node: null,
  247. nwjs: null,
  248. electron: null
  249. };
  250. this.options = options;
  251. this.context = context;
  252. this.requestShortener = new RequestShortener(context, this.root);
  253. this.cache = new Cache();
  254. /** @type {Map<Module, { buildInfo: BuildInfo, references: References | undefined, memCache: WeakTupleMap }> | undefined} */
  255. this.moduleMemCaches = undefined;
  256. this.compilerPath = "";
  257. /** @type {boolean} */
  258. this.running = false;
  259. /** @type {boolean} */
  260. this.idle = false;
  261. /** @type {boolean} */
  262. this.watchMode = false;
  263. this._backCompat = this.options.experiments.backCompat !== false;
  264. /** @type {Compilation | undefined} */
  265. this._lastCompilation = undefined;
  266. /** @type {NormalModuleFactory | undefined} */
  267. this._lastNormalModuleFactory = undefined;
  268. /**
  269. * @private
  270. * @type {WeakMap<Source, CacheEntry>}
  271. */
  272. this._assetEmittingSourceCache = new WeakMap();
  273. /**
  274. * @private
  275. * @type {Map<string, number>}
  276. */
  277. this._assetEmittingWrittenFiles = new Map();
  278. /**
  279. * @private
  280. * @type {Set<string>}
  281. */
  282. this._assetEmittingPreviousFiles = new Set();
  283. }
  284. /**
  285. * @param {string} name cache name
  286. * @returns {CacheFacade} the cache facade instance
  287. */
  288. getCache(name) {
  289. return new CacheFacade(
  290. this.cache,
  291. `${this.compilerPath}${name}`,
  292. this.options.output.hashFunction
  293. );
  294. }
  295. /**
  296. * @param {string | (function(): string)} name name of the logger, or function called once to get the logger name
  297. * @returns {Logger} a logger with that name
  298. */
  299. getInfrastructureLogger(name) {
  300. if (!name) {
  301. throw new TypeError(
  302. "Compiler.getInfrastructureLogger(name) called without a name"
  303. );
  304. }
  305. return new Logger(
  306. (type, args) => {
  307. if (typeof name === "function") {
  308. name = name();
  309. if (!name) {
  310. throw new TypeError(
  311. "Compiler.getInfrastructureLogger(name) called with a function not returning a name"
  312. );
  313. }
  314. }
  315. if (this.hooks.infrastructureLog.call(name, type, args) === undefined) {
  316. if (this.infrastructureLogger !== undefined) {
  317. this.infrastructureLogger(name, type, args);
  318. }
  319. }
  320. },
  321. childName => {
  322. if (typeof name === "function") {
  323. if (typeof childName === "function") {
  324. return this.getInfrastructureLogger(() => {
  325. if (typeof name === "function") {
  326. name = name();
  327. if (!name) {
  328. throw new TypeError(
  329. "Compiler.getInfrastructureLogger(name) called with a function not returning a name"
  330. );
  331. }
  332. }
  333. if (typeof childName === "function") {
  334. childName = childName();
  335. if (!childName) {
  336. throw new TypeError(
  337. "Logger.getChildLogger(name) called with a function not returning a name"
  338. );
  339. }
  340. }
  341. return `${name}/${childName}`;
  342. });
  343. } else {
  344. return this.getInfrastructureLogger(() => {
  345. if (typeof name === "function") {
  346. name = name();
  347. if (!name) {
  348. throw new TypeError(
  349. "Compiler.getInfrastructureLogger(name) called with a function not returning a name"
  350. );
  351. }
  352. }
  353. return `${name}/${childName}`;
  354. });
  355. }
  356. } else {
  357. if (typeof childName === "function") {
  358. return this.getInfrastructureLogger(() => {
  359. if (typeof childName === "function") {
  360. childName = childName();
  361. if (!childName) {
  362. throw new TypeError(
  363. "Logger.getChildLogger(name) called with a function not returning a name"
  364. );
  365. }
  366. }
  367. return `${name}/${childName}`;
  368. });
  369. } else {
  370. return this.getInfrastructureLogger(`${name}/${childName}`);
  371. }
  372. }
  373. }
  374. );
  375. }
  376. // TODO webpack 6: solve this in a better way
  377. // e.g. move compilation specific info from Modules into ModuleGraph
  378. _cleanupLastCompilation() {
  379. if (this._lastCompilation !== undefined) {
  380. for (const childCompilation of this._lastCompilation.children) {
  381. for (const module of childCompilation.modules) {
  382. ChunkGraph.clearChunkGraphForModule(module);
  383. ModuleGraph.clearModuleGraphForModule(module);
  384. module.cleanupForCache();
  385. }
  386. for (const chunk of childCompilation.chunks) {
  387. ChunkGraph.clearChunkGraphForChunk(chunk);
  388. }
  389. }
  390. for (const module of this._lastCompilation.modules) {
  391. ChunkGraph.clearChunkGraphForModule(module);
  392. ModuleGraph.clearModuleGraphForModule(module);
  393. module.cleanupForCache();
  394. }
  395. for (const chunk of this._lastCompilation.chunks) {
  396. ChunkGraph.clearChunkGraphForChunk(chunk);
  397. }
  398. this._lastCompilation = undefined;
  399. }
  400. }
  401. // TODO webpack 6: solve this in a better way
  402. _cleanupLastNormalModuleFactory() {
  403. if (this._lastNormalModuleFactory !== undefined) {
  404. this._lastNormalModuleFactory.cleanupForCache();
  405. this._lastNormalModuleFactory = undefined;
  406. }
  407. }
  408. /**
  409. * @param {WatchOptions} watchOptions the watcher's options
  410. * @param {RunCallback<Stats>} handler signals when the call finishes
  411. * @returns {Watching} a compiler watcher
  412. */
  413. watch(watchOptions, handler) {
  414. if (this.running) {
  415. return handler(new ConcurrentCompilationError());
  416. }
  417. this.running = true;
  418. this.watchMode = true;
  419. this.watching = new Watching(this, watchOptions, handler);
  420. return this.watching;
  421. }
  422. /**
  423. * @param {RunCallback<Stats>} callback signals when the call finishes
  424. * @returns {void}
  425. */
  426. run(callback) {
  427. if (this.running) {
  428. return callback(new ConcurrentCompilationError());
  429. }
  430. /** @type {Logger | undefined} */
  431. let logger;
  432. /**
  433. * @param {Error | null} err error
  434. * @param {Stats=} stats stats
  435. */
  436. const finalCallback = (err, stats) => {
  437. if (logger) logger.time("beginIdle");
  438. this.idle = true;
  439. this.cache.beginIdle();
  440. this.idle = true;
  441. if (logger) logger.timeEnd("beginIdle");
  442. this.running = false;
  443. if (err) {
  444. this.hooks.failed.call(err);
  445. }
  446. if (callback !== undefined) callback(err, stats);
  447. this.hooks.afterDone.call(/** @type {Stats} */ (stats));
  448. };
  449. const startTime = Date.now();
  450. this.running = true;
  451. /**
  452. * @param {Error | null} err error
  453. * @param {Compilation=} _compilation compilation
  454. * @returns {void}
  455. */
  456. const onCompiled = (err, _compilation) => {
  457. if (err) return finalCallback(err);
  458. const compilation = /** @type {Compilation} */ (_compilation);
  459. if (this.hooks.shouldEmit.call(compilation) === false) {
  460. compilation.startTime = startTime;
  461. compilation.endTime = Date.now();
  462. const stats = new Stats(compilation);
  463. this.hooks.done.callAsync(stats, err => {
  464. if (err) return finalCallback(err);
  465. return finalCallback(null, stats);
  466. });
  467. return;
  468. }
  469. process.nextTick(() => {
  470. logger = compilation.getLogger("webpack.Compiler");
  471. logger.time("emitAssets");
  472. this.emitAssets(compilation, err => {
  473. /** @type {Logger} */
  474. (logger).timeEnd("emitAssets");
  475. if (err) return finalCallback(err);
  476. if (compilation.hooks.needAdditionalPass.call()) {
  477. compilation.needAdditionalPass = true;
  478. compilation.startTime = startTime;
  479. compilation.endTime = Date.now();
  480. /** @type {Logger} */
  481. (logger).time("done hook");
  482. const stats = new Stats(compilation);
  483. this.hooks.done.callAsync(stats, err => {
  484. /** @type {Logger} */
  485. (logger).timeEnd("done hook");
  486. if (err) return finalCallback(err);
  487. this.hooks.additionalPass.callAsync(err => {
  488. if (err) return finalCallback(err);
  489. this.compile(onCompiled);
  490. });
  491. });
  492. return;
  493. }
  494. /** @type {Logger} */
  495. (logger).time("emitRecords");
  496. this.emitRecords(err => {
  497. /** @type {Logger} */
  498. (logger).timeEnd("emitRecords");
  499. if (err) return finalCallback(err);
  500. compilation.startTime = startTime;
  501. compilation.endTime = Date.now();
  502. /** @type {Logger} */
  503. (logger).time("done hook");
  504. const stats = new Stats(compilation);
  505. this.hooks.done.callAsync(stats, err => {
  506. /** @type {Logger} */
  507. (logger).timeEnd("done hook");
  508. if (err) return finalCallback(err);
  509. this.cache.storeBuildDependencies(
  510. compilation.buildDependencies,
  511. err => {
  512. if (err) return finalCallback(err);
  513. return finalCallback(null, stats);
  514. }
  515. );
  516. });
  517. });
  518. });
  519. });
  520. };
  521. const run = () => {
  522. this.hooks.beforeRun.callAsync(this, err => {
  523. if (err) return finalCallback(err);
  524. this.hooks.run.callAsync(this, err => {
  525. if (err) return finalCallback(err);
  526. this.readRecords(err => {
  527. if (err) return finalCallback(err);
  528. this.compile(onCompiled);
  529. });
  530. });
  531. });
  532. };
  533. if (this.idle) {
  534. this.cache.endIdle(err => {
  535. if (err) return finalCallback(err);
  536. this.idle = false;
  537. run();
  538. });
  539. } else {
  540. run();
  541. }
  542. }
  543. /**
  544. * @param {RunAsChildCallback} callback signals when the call finishes
  545. * @returns {void}
  546. */
  547. runAsChild(callback) {
  548. const startTime = Date.now();
  549. /**
  550. * @param {Error | null} err error
  551. * @param {Chunk[]=} entries entries
  552. * @param {Compilation=} compilation compilation
  553. */
  554. const finalCallback = (err, entries, compilation) => {
  555. try {
  556. callback(err, entries, compilation);
  557. } catch (e) {
  558. const err = new WebpackError(
  559. `compiler.runAsChild callback error: ${e}`
  560. );
  561. err.details = /** @type {Error} */ (e).stack;
  562. /** @type {Compilation} */
  563. (this.parentCompilation).errors.push(err);
  564. }
  565. };
  566. this.compile((err, _compilation) => {
  567. if (err) return finalCallback(err);
  568. const compilation = /** @type {Compilation} */ (_compilation);
  569. const parentCompilation = /** @type {Compilation} */ (
  570. this.parentCompilation
  571. );
  572. parentCompilation.children.push(compilation);
  573. for (const { name, source, info } of compilation.getAssets()) {
  574. parentCompilation.emitAsset(name, source, info);
  575. }
  576. /** @type {Chunk[]} */
  577. const entries = [];
  578. for (const ep of compilation.entrypoints.values()) {
  579. entries.push(...ep.chunks);
  580. }
  581. compilation.startTime = startTime;
  582. compilation.endTime = Date.now();
  583. return finalCallback(null, entries, compilation);
  584. });
  585. }
  586. purgeInputFileSystem() {
  587. if (this.inputFileSystem && this.inputFileSystem.purge) {
  588. this.inputFileSystem.purge();
  589. }
  590. }
  591. /**
  592. * @param {Compilation} compilation the compilation
  593. * @param {Callback<void>} callback signals when the assets are emitted
  594. * @returns {void}
  595. */
  596. emitAssets(compilation, callback) {
  597. /** @type {string} */
  598. let outputPath;
  599. /**
  600. * @param {Error=} err error
  601. * @returns {void}
  602. */
  603. const emitFiles = err => {
  604. if (err) return callback(err);
  605. const assets = compilation.getAssets();
  606. compilation.assets = { ...compilation.assets };
  607. /** @type {Map<string, SimilarEntry>} */
  608. const caseInsensitiveMap = new Map();
  609. /** @type {Set<string>} */
  610. const allTargetPaths = new Set();
  611. asyncLib.forEachLimit(
  612. assets,
  613. 15,
  614. ({ name: file, source, info }, callback) => {
  615. let targetFile = file;
  616. let immutable = info.immutable;
  617. const queryStringIdx = targetFile.indexOf("?");
  618. if (queryStringIdx >= 0) {
  619. targetFile = targetFile.slice(0, queryStringIdx);
  620. // We may remove the hash, which is in the query string
  621. // So we recheck if the file is immutable
  622. // This doesn't cover all cases, but immutable is only a performance optimization anyway
  623. immutable =
  624. immutable &&
  625. (includesHash(targetFile, info.contenthash) ||
  626. includesHash(targetFile, info.chunkhash) ||
  627. includesHash(targetFile, info.modulehash) ||
  628. includesHash(targetFile, info.fullhash));
  629. }
  630. /**
  631. * @param {Error=} err error
  632. * @returns {void}
  633. */
  634. const writeOut = err => {
  635. if (err) return callback(err);
  636. const targetPath = join(
  637. /** @type {OutputFileSystem} */
  638. (this.outputFileSystem),
  639. outputPath,
  640. targetFile
  641. );
  642. allTargetPaths.add(targetPath);
  643. // check if the target file has already been written by this Compiler
  644. const targetFileGeneration =
  645. this._assetEmittingWrittenFiles.get(targetPath);
  646. // create an cache entry for this Source if not already existing
  647. let cacheEntry = this._assetEmittingSourceCache.get(source);
  648. if (cacheEntry === undefined) {
  649. cacheEntry = {
  650. sizeOnlySource: undefined,
  651. writtenTo: new Map()
  652. };
  653. this._assetEmittingSourceCache.set(source, cacheEntry);
  654. }
  655. /** @type {SimilarEntry | undefined} */
  656. let similarEntry;
  657. const checkSimilarFile = () => {
  658. const caseInsensitiveTargetPath = targetPath.toLowerCase();
  659. similarEntry = caseInsensitiveMap.get(caseInsensitiveTargetPath);
  660. if (similarEntry !== undefined) {
  661. const { path: other, source: otherSource } = similarEntry;
  662. if (isSourceEqual(otherSource, source)) {
  663. // Size may or may not be available at this point.
  664. // If it's not available add to "waiting" list and it will be updated once available
  665. if (similarEntry.size !== undefined) {
  666. updateWithReplacementSource(similarEntry.size);
  667. } else {
  668. if (!similarEntry.waiting) similarEntry.waiting = [];
  669. similarEntry.waiting.push({ file, cacheEntry });
  670. }
  671. alreadyWritten();
  672. } else {
  673. const err =
  674. new WebpackError(`Prevent writing to file that only differs in casing or query string from already written file.
  675. This will lead to a race-condition and corrupted files on case-insensitive file systems.
  676. ${targetPath}
  677. ${other}`);
  678. err.file = file;
  679. callback(err);
  680. }
  681. return true;
  682. } else {
  683. caseInsensitiveMap.set(
  684. caseInsensitiveTargetPath,
  685. (similarEntry = /** @type {SimilarEntry} */ ({
  686. path: targetPath,
  687. source,
  688. size: undefined,
  689. waiting: undefined
  690. }))
  691. );
  692. return false;
  693. }
  694. };
  695. /**
  696. * get the binary (Buffer) content from the Source
  697. * @returns {Buffer} content for the source
  698. */
  699. const getContent = () => {
  700. if (typeof source.buffer === "function") {
  701. return source.buffer();
  702. } else {
  703. const bufferOrString = source.source();
  704. if (Buffer.isBuffer(bufferOrString)) {
  705. return bufferOrString;
  706. } else {
  707. return Buffer.from(bufferOrString, "utf8");
  708. }
  709. }
  710. };
  711. const alreadyWritten = () => {
  712. // cache the information that the Source has been already been written to that location
  713. if (targetFileGeneration === undefined) {
  714. const newGeneration = 1;
  715. this._assetEmittingWrittenFiles.set(targetPath, newGeneration);
  716. /** @type {CacheEntry} */
  717. (cacheEntry).writtenTo.set(targetPath, newGeneration);
  718. } else {
  719. /** @type {CacheEntry} */
  720. (cacheEntry).writtenTo.set(targetPath, targetFileGeneration);
  721. }
  722. callback();
  723. };
  724. /**
  725. * Write the file to output file system
  726. * @param {Buffer} content content to be written
  727. * @returns {void}
  728. */
  729. const doWrite = content => {
  730. /** @type {OutputFileSystem} */
  731. (this.outputFileSystem).writeFile(targetPath, content, err => {
  732. if (err) return callback(err);
  733. // information marker that the asset has been emitted
  734. compilation.emittedAssets.add(file);
  735. // cache the information that the Source has been written to that location
  736. const newGeneration =
  737. targetFileGeneration === undefined
  738. ? 1
  739. : targetFileGeneration + 1;
  740. /** @type {CacheEntry} */
  741. (cacheEntry).writtenTo.set(targetPath, newGeneration);
  742. this._assetEmittingWrittenFiles.set(targetPath, newGeneration);
  743. this.hooks.assetEmitted.callAsync(
  744. file,
  745. {
  746. content,
  747. source,
  748. outputPath,
  749. compilation,
  750. targetPath
  751. },
  752. callback
  753. );
  754. });
  755. };
  756. /**
  757. * @param {number} size size
  758. */
  759. const updateWithReplacementSource = size => {
  760. updateFileWithReplacementSource(
  761. file,
  762. /** @type {CacheEntry} */ (cacheEntry),
  763. size
  764. );
  765. /** @type {SimilarEntry} */
  766. (similarEntry).size = size;
  767. if (
  768. /** @type {SimilarEntry} */ (similarEntry).waiting !== undefined
  769. ) {
  770. for (const { file, cacheEntry } of /** @type {SimilarEntry} */ (
  771. similarEntry
  772. ).waiting) {
  773. updateFileWithReplacementSource(file, cacheEntry, size);
  774. }
  775. }
  776. };
  777. /**
  778. * @param {string} file file
  779. * @param {CacheEntry} cacheEntry cache entry
  780. * @param {number} size size
  781. */
  782. const updateFileWithReplacementSource = (
  783. file,
  784. cacheEntry,
  785. size
  786. ) => {
  787. // Create a replacement resource which only allows to ask for size
  788. // This allows to GC all memory allocated by the Source
  789. // (expect when the Source is stored in any other cache)
  790. if (!cacheEntry.sizeOnlySource) {
  791. cacheEntry.sizeOnlySource = new SizeOnlySource(size);
  792. }
  793. compilation.updateAsset(file, cacheEntry.sizeOnlySource, {
  794. size
  795. });
  796. };
  797. /**
  798. * @param {IStats} stats stats
  799. * @returns {void}
  800. */
  801. const processExistingFile = stats => {
  802. // skip emitting if it's already there and an immutable file
  803. if (immutable) {
  804. updateWithReplacementSource(/** @type {number} */ (stats.size));
  805. return alreadyWritten();
  806. }
  807. const content = getContent();
  808. updateWithReplacementSource(content.length);
  809. // if it exists and content on disk matches content
  810. // skip writing the same content again
  811. // (to keep mtime and don't trigger watchers)
  812. // for a fast negative match file size is compared first
  813. if (content.length === stats.size) {
  814. compilation.comparedForEmitAssets.add(file);
  815. return /** @type {OutputFileSystem} */ (
  816. this.outputFileSystem
  817. ).readFile(targetPath, (err, existingContent) => {
  818. if (
  819. err ||
  820. !content.equals(/** @type {Buffer} */ (existingContent))
  821. ) {
  822. return doWrite(content);
  823. } else {
  824. return alreadyWritten();
  825. }
  826. });
  827. }
  828. return doWrite(content);
  829. };
  830. const processMissingFile = () => {
  831. const content = getContent();
  832. updateWithReplacementSource(content.length);
  833. return doWrite(content);
  834. };
  835. // if the target file has already been written
  836. if (targetFileGeneration !== undefined) {
  837. // check if the Source has been written to this target file
  838. const writtenGeneration = /** @type {CacheEntry} */ (
  839. cacheEntry
  840. ).writtenTo.get(targetPath);
  841. if (writtenGeneration === targetFileGeneration) {
  842. // if yes, we may skip writing the file
  843. // if it's already there
  844. // (we assume one doesn't modify files while the Compiler is running, other then removing them)
  845. if (this._assetEmittingPreviousFiles.has(targetPath)) {
  846. const sizeOnlySource = /** @type {SizeOnlySource} */ (
  847. /** @type {CacheEntry} */ (cacheEntry).sizeOnlySource
  848. );
  849. // We assume that assets from the last compilation say intact on disk (they are not removed)
  850. compilation.updateAsset(file, sizeOnlySource, {
  851. size: sizeOnlySource.size()
  852. });
  853. return callback();
  854. } else {
  855. // Settings immutable will make it accept file content without comparing when file exist
  856. immutable = true;
  857. }
  858. } else if (!immutable) {
  859. if (checkSimilarFile()) return;
  860. // We wrote to this file before which has very likely a different content
  861. // skip comparing and assume content is different for performance
  862. // This case happens often during watch mode.
  863. return processMissingFile();
  864. }
  865. }
  866. if (checkSimilarFile()) return;
  867. if (this.options.output.compareBeforeEmit) {
  868. /** @type {OutputFileSystem} */
  869. (this.outputFileSystem).stat(targetPath, (err, stats) => {
  870. const exists = !err && /** @type {IStats} */ (stats).isFile();
  871. if (exists) {
  872. processExistingFile(/** @type {IStats} */ (stats));
  873. } else {
  874. processMissingFile();
  875. }
  876. });
  877. } else {
  878. processMissingFile();
  879. }
  880. };
  881. if (targetFile.match(/\/|\\/)) {
  882. const fs = /** @type {OutputFileSystem} */ (this.outputFileSystem);
  883. const dir = dirname(fs, join(fs, outputPath, targetFile));
  884. mkdirp(fs, dir, writeOut);
  885. } else {
  886. writeOut();
  887. }
  888. },
  889. err => {
  890. // Clear map to free up memory
  891. caseInsensitiveMap.clear();
  892. if (err) {
  893. this._assetEmittingPreviousFiles.clear();
  894. return callback(err);
  895. }
  896. this._assetEmittingPreviousFiles = allTargetPaths;
  897. this.hooks.afterEmit.callAsync(compilation, err => {
  898. if (err) return callback(err);
  899. return callback();
  900. });
  901. }
  902. );
  903. };
  904. this.hooks.emit.callAsync(compilation, err => {
  905. if (err) return callback(err);
  906. outputPath = compilation.getPath(this.outputPath, {});
  907. mkdirp(
  908. /** @type {OutputFileSystem} */ (this.outputFileSystem),
  909. outputPath,
  910. emitFiles
  911. );
  912. });
  913. }
  914. /**
  915. * @param {Callback<void>} callback signals when the call finishes
  916. * @returns {void}
  917. */
  918. emitRecords(callback) {
  919. if (this.hooks.emitRecords.isUsed()) {
  920. if (this.recordsOutputPath) {
  921. asyncLib.parallel(
  922. [
  923. cb => this.hooks.emitRecords.callAsync(cb),
  924. this._emitRecords.bind(this)
  925. ],
  926. err => callback(err)
  927. );
  928. } else {
  929. this.hooks.emitRecords.callAsync(callback);
  930. }
  931. } else {
  932. if (this.recordsOutputPath) {
  933. this._emitRecords(callback);
  934. } else {
  935. callback();
  936. }
  937. }
  938. }
  939. /**
  940. * @param {Callback<void>} callback signals when the call finishes
  941. * @returns {void}
  942. */
  943. _emitRecords(callback) {
  944. const writeFile = () => {
  945. /** @type {OutputFileSystem} */
  946. (this.outputFileSystem).writeFile(
  947. /** @type {string} */ (this.recordsOutputPath),
  948. JSON.stringify(
  949. this.records,
  950. (n, value) => {
  951. if (
  952. typeof value === "object" &&
  953. value !== null &&
  954. !Array.isArray(value)
  955. ) {
  956. const keys = Object.keys(value);
  957. if (!isSorted(keys)) {
  958. return sortObject(value, keys);
  959. }
  960. }
  961. return value;
  962. },
  963. 2
  964. ),
  965. callback
  966. );
  967. };
  968. const recordsOutputPathDirectory = dirname(
  969. /** @type {OutputFileSystem} */ (this.outputFileSystem),
  970. /** @type {string} */ (this.recordsOutputPath)
  971. );
  972. if (!recordsOutputPathDirectory) {
  973. return writeFile();
  974. }
  975. mkdirp(
  976. /** @type {OutputFileSystem} */ (this.outputFileSystem),
  977. recordsOutputPathDirectory,
  978. err => {
  979. if (err) return callback(err);
  980. writeFile();
  981. }
  982. );
  983. }
  984. /**
  985. * @param {Callback<void>} callback signals when the call finishes
  986. * @returns {void}
  987. */
  988. readRecords(callback) {
  989. if (this.hooks.readRecords.isUsed()) {
  990. if (this.recordsInputPath) {
  991. asyncLib.parallel(
  992. [
  993. cb => this.hooks.readRecords.callAsync(cb),
  994. this._readRecords.bind(this)
  995. ],
  996. err => callback(err)
  997. );
  998. } else {
  999. this.records = {};
  1000. this.hooks.readRecords.callAsync(callback);
  1001. }
  1002. } else {
  1003. if (this.recordsInputPath) {
  1004. this._readRecords(callback);
  1005. } else {
  1006. this.records = {};
  1007. callback();
  1008. }
  1009. }
  1010. }
  1011. /**
  1012. * @param {Callback<void>} callback signals when the call finishes
  1013. * @returns {void}
  1014. */
  1015. _readRecords(callback) {
  1016. if (!this.recordsInputPath) {
  1017. this.records = {};
  1018. return callback();
  1019. }
  1020. /** @type {InputFileSystem} */
  1021. (this.inputFileSystem).stat(this.recordsInputPath, err => {
  1022. // It doesn't exist
  1023. // We can ignore this.
  1024. if (err) return callback();
  1025. /** @type {InputFileSystem} */
  1026. (this.inputFileSystem).readFile(
  1027. /** @type {string} */ (this.recordsInputPath),
  1028. (err, content) => {
  1029. if (err) return callback(err);
  1030. try {
  1031. this.records = parseJson(
  1032. /** @type {Buffer} */ (content).toString("utf-8")
  1033. );
  1034. } catch (e) {
  1035. return callback(
  1036. new Error(
  1037. `Cannot parse records: ${/** @type {Error} */ (e).message}`
  1038. )
  1039. );
  1040. }
  1041. return callback();
  1042. }
  1043. );
  1044. });
  1045. }
  1046. /**
  1047. * @param {Compilation} compilation the compilation
  1048. * @param {string} compilerName the compiler's name
  1049. * @param {number} compilerIndex the compiler's index
  1050. * @param {OutputOptions=} outputOptions the output options
  1051. * @param {WebpackPluginInstance[]=} plugins the plugins to apply
  1052. * @returns {Compiler} a child compiler
  1053. */
  1054. createChildCompiler(
  1055. compilation,
  1056. compilerName,
  1057. compilerIndex,
  1058. outputOptions,
  1059. plugins
  1060. ) {
  1061. const childCompiler = new Compiler(this.context, {
  1062. ...this.options,
  1063. output: {
  1064. ...this.options.output,
  1065. ...outputOptions
  1066. }
  1067. });
  1068. childCompiler.name = compilerName;
  1069. childCompiler.outputPath = this.outputPath;
  1070. childCompiler.inputFileSystem = this.inputFileSystem;
  1071. childCompiler.outputFileSystem = null;
  1072. childCompiler.resolverFactory = this.resolverFactory;
  1073. childCompiler.modifiedFiles = this.modifiedFiles;
  1074. childCompiler.removedFiles = this.removedFiles;
  1075. childCompiler.fileTimestamps = this.fileTimestamps;
  1076. childCompiler.contextTimestamps = this.contextTimestamps;
  1077. childCompiler.fsStartTime = this.fsStartTime;
  1078. childCompiler.cache = this.cache;
  1079. childCompiler.compilerPath = `${this.compilerPath}${compilerName}|${compilerIndex}|`;
  1080. childCompiler._backCompat = this._backCompat;
  1081. const relativeCompilerName = makePathsRelative(
  1082. this.context,
  1083. compilerName,
  1084. this.root
  1085. );
  1086. if (!this.records[relativeCompilerName]) {
  1087. this.records[relativeCompilerName] = [];
  1088. }
  1089. if (this.records[relativeCompilerName][compilerIndex]) {
  1090. childCompiler.records = this.records[relativeCompilerName][compilerIndex];
  1091. } else {
  1092. this.records[relativeCompilerName].push((childCompiler.records = {}));
  1093. }
  1094. childCompiler.parentCompilation = compilation;
  1095. childCompiler.root = this.root;
  1096. if (Array.isArray(plugins)) {
  1097. for (const plugin of plugins) {
  1098. if (plugin) {
  1099. plugin.apply(childCompiler);
  1100. }
  1101. }
  1102. }
  1103. for (const name in this.hooks) {
  1104. if (
  1105. ![
  1106. "make",
  1107. "compile",
  1108. "emit",
  1109. "afterEmit",
  1110. "invalid",
  1111. "done",
  1112. "thisCompilation"
  1113. ].includes(name)
  1114. ) {
  1115. if (childCompiler.hooks[name]) {
  1116. childCompiler.hooks[name].taps = this.hooks[name].taps.slice();
  1117. }
  1118. }
  1119. }
  1120. compilation.hooks.childCompiler.call(
  1121. childCompiler,
  1122. compilerName,
  1123. compilerIndex
  1124. );
  1125. return childCompiler;
  1126. }
  1127. isChild() {
  1128. return !!this.parentCompilation;
  1129. }
  1130. /**
  1131. * @param {CompilationParams} params the compilation parameters
  1132. * @returns {Compilation} compilation
  1133. */
  1134. createCompilation(params) {
  1135. this._cleanupLastCompilation();
  1136. return (this._lastCompilation = new Compilation(this, params));
  1137. }
  1138. /**
  1139. * @param {CompilationParams} params the compilation parameters
  1140. * @returns {Compilation} the created compilation
  1141. */
  1142. newCompilation(params) {
  1143. const compilation = this.createCompilation(params);
  1144. compilation.name = this.name;
  1145. compilation.records = this.records;
  1146. this.hooks.thisCompilation.call(compilation, params);
  1147. this.hooks.compilation.call(compilation, params);
  1148. return compilation;
  1149. }
  1150. createNormalModuleFactory() {
  1151. this._cleanupLastNormalModuleFactory();
  1152. const normalModuleFactory = new NormalModuleFactory({
  1153. context: this.options.context,
  1154. fs: /** @type {InputFileSystem} */ (this.inputFileSystem),
  1155. resolverFactory: this.resolverFactory,
  1156. options: this.options.module,
  1157. associatedObjectForCache: this.root,
  1158. layers: this.options.experiments.layers
  1159. });
  1160. this._lastNormalModuleFactory = normalModuleFactory;
  1161. this.hooks.normalModuleFactory.call(normalModuleFactory);
  1162. return normalModuleFactory;
  1163. }
  1164. createContextModuleFactory() {
  1165. const contextModuleFactory = new ContextModuleFactory(this.resolverFactory);
  1166. this.hooks.contextModuleFactory.call(contextModuleFactory);
  1167. return contextModuleFactory;
  1168. }
  1169. newCompilationParams() {
  1170. const params = {
  1171. normalModuleFactory: this.createNormalModuleFactory(),
  1172. contextModuleFactory: this.createContextModuleFactory()
  1173. };
  1174. return params;
  1175. }
  1176. /**
  1177. * @param {RunCallback<Compilation>} callback signals when the compilation finishes
  1178. * @returns {void}
  1179. */
  1180. compile(callback) {
  1181. const params = this.newCompilationParams();
  1182. this.hooks.beforeCompile.callAsync(params, err => {
  1183. if (err) return callback(err);
  1184. this.hooks.compile.call(params);
  1185. const compilation = this.newCompilation(params);
  1186. const logger = compilation.getLogger("webpack.Compiler");
  1187. logger.time("make hook");
  1188. this.hooks.make.callAsync(compilation, err => {
  1189. logger.timeEnd("make hook");
  1190. if (err) return callback(err);
  1191. logger.time("finish make hook");
  1192. this.hooks.finishMake.callAsync(compilation, err => {
  1193. logger.timeEnd("finish make hook");
  1194. if (err) return callback(err);
  1195. process.nextTick(() => {
  1196. logger.time("finish compilation");
  1197. compilation.finish(err => {
  1198. logger.timeEnd("finish compilation");
  1199. if (err) return callback(err);
  1200. logger.time("seal compilation");
  1201. compilation.seal(err => {
  1202. logger.timeEnd("seal compilation");
  1203. if (err) return callback(err);
  1204. logger.time("afterCompile hook");
  1205. this.hooks.afterCompile.callAsync(compilation, err => {
  1206. logger.timeEnd("afterCompile hook");
  1207. if (err) return callback(err);
  1208. return callback(null, compilation);
  1209. });
  1210. });
  1211. });
  1212. });
  1213. });
  1214. });
  1215. });
  1216. }
  1217. /**
  1218. * @param {RunCallback<void>} callback signals when the compiler closes
  1219. * @returns {void}
  1220. */
  1221. close(callback) {
  1222. if (this.watching) {
  1223. // When there is still an active watching, close this first
  1224. this.watching.close(err => {
  1225. this.close(callback);
  1226. });
  1227. return;
  1228. }
  1229. this.hooks.shutdown.callAsync(err => {
  1230. if (err) return callback(err);
  1231. // Get rid of reference to last compilation to avoid leaking memory
  1232. // We can't run this._cleanupLastCompilation() as the Stats to this compilation
  1233. // might be still in use. We try to get rid of the reference to the cache instead.
  1234. this._lastCompilation = undefined;
  1235. this._lastNormalModuleFactory = undefined;
  1236. this.cache.shutdown(callback);
  1237. });
  1238. }
  1239. }
  1240. module.exports = Compiler;