提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1412 lines
41 KiB

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