Compiler.js 40 KB

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