Compiler.js 44 KB

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