Compiler.js 35 KB

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