Compiler.js 41 KB

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