Compiler.js 40 KB

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