10
0

Compiler.js 40 KB

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