Compiler.js 39 KB

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