cached-child-compiler.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // @ts-check
  2. /**
  3. * @file
  4. * Helper plugin manages the cached state of the child compilation
  5. *
  6. * To optimize performance the child compilation is running asynchronously.
  7. * Therefore it needs to be started in the compiler.make phase and ends after
  8. * the compilation.afterCompile phase.
  9. *
  10. * To prevent bugs from blocked hooks there is no promise or event based api
  11. * for this plugin.
  12. *
  13. * Example usage:
  14. *
  15. * ```js
  16. const childCompilerPlugin = new PersistentChildCompilerPlugin();
  17. childCompilerPlugin.addEntry('./src/index.js');
  18. compiler.hooks.afterCompile.tapAsync('MyPlugin', (compilation, callback) => {
  19. console.log(childCompilerPlugin.getCompilationResult()['./src/index.js']));
  20. return true;
  21. });
  22. * ```
  23. */
  24. "use strict";
  25. // Import types
  26. /** @typedef {import("webpack").Compiler} Compiler */
  27. /** @typedef {import("webpack").Compilation} Compilation */
  28. /** @typedef {import("webpack/lib/FileSystemInfo").Snapshot} Snapshot */
  29. /** @typedef {import("./child-compiler").ChildCompilationTemplateResult} ChildCompilationTemplateResult */
  30. /** @typedef {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]}} FileDependencies */
  31. /** @typedef {{
  32. dependencies: FileDependencies,
  33. compiledEntries: {[entryName: string]: ChildCompilationTemplateResult}
  34. } | {
  35. dependencies: FileDependencies,
  36. error: Error
  37. }} ChildCompilationResult */
  38. const { HtmlWebpackChildCompiler } = require("./child-compiler");
  39. /**
  40. * This plugin is a singleton for performance reasons.
  41. * To keep track if a plugin does already exist for the compiler they are cached
  42. * in this map
  43. * @type {WeakMap<Compiler, PersistentChildCompilerSingletonPlugin>}}
  44. */
  45. const compilerMap = new WeakMap();
  46. class CachedChildCompilation {
  47. /**
  48. * @param {Compiler} compiler
  49. */
  50. constructor(compiler) {
  51. /**
  52. * @private
  53. * @type {Compiler}
  54. */
  55. this.compiler = compiler;
  56. // Create a singleton instance for the compiler
  57. // if there is none
  58. if (compilerMap.has(compiler)) {
  59. return;
  60. }
  61. const persistentChildCompilerSingletonPlugin =
  62. new PersistentChildCompilerSingletonPlugin();
  63. compilerMap.set(compiler, persistentChildCompilerSingletonPlugin);
  64. persistentChildCompilerSingletonPlugin.apply(compiler);
  65. }
  66. /**
  67. * apply is called by the webpack main compiler during the start phase
  68. * @param {string} entry
  69. */
  70. addEntry(entry) {
  71. const persistentChildCompilerSingletonPlugin = compilerMap.get(
  72. this.compiler,
  73. );
  74. if (!persistentChildCompilerSingletonPlugin) {
  75. throw new Error(
  76. "PersistentChildCompilerSingletonPlugin instance not found.",
  77. );
  78. }
  79. persistentChildCompilerSingletonPlugin.addEntry(entry);
  80. }
  81. getCompilationResult() {
  82. const persistentChildCompilerSingletonPlugin = compilerMap.get(
  83. this.compiler,
  84. );
  85. if (!persistentChildCompilerSingletonPlugin) {
  86. throw new Error(
  87. "PersistentChildCompilerSingletonPlugin instance not found.",
  88. );
  89. }
  90. return persistentChildCompilerSingletonPlugin.getLatestResult();
  91. }
  92. /**
  93. * Returns the result for the given entry
  94. * @param {string} entry
  95. * @returns {
  96. | { mainCompilationHash: string, error: Error }
  97. | { mainCompilationHash: string, compiledEntry: ChildCompilationTemplateResult }
  98. }
  99. */
  100. getCompilationEntryResult(entry) {
  101. const latestResult = this.getCompilationResult();
  102. const compilationResult = latestResult.compilationResult;
  103. return "error" in compilationResult
  104. ? {
  105. mainCompilationHash: latestResult.mainCompilationHash,
  106. error: compilationResult.error,
  107. }
  108. : {
  109. mainCompilationHash: latestResult.mainCompilationHash,
  110. compiledEntry: compilationResult.compiledEntries[entry],
  111. };
  112. }
  113. }
  114. class PersistentChildCompilerSingletonPlugin {
  115. /**
  116. *
  117. * @param {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]}} fileDependencies
  118. * @param {Compilation} mainCompilation
  119. * @param {number} startTime
  120. */
  121. static createSnapshot(fileDependencies, mainCompilation, startTime) {
  122. return new Promise((resolve, reject) => {
  123. mainCompilation.fileSystemInfo.createSnapshot(
  124. startTime,
  125. fileDependencies.fileDependencies,
  126. fileDependencies.contextDependencies,
  127. fileDependencies.missingDependencies,
  128. // @ts-ignore
  129. null,
  130. (err, snapshot) => {
  131. if (err) {
  132. return reject(err);
  133. }
  134. resolve(snapshot);
  135. },
  136. );
  137. });
  138. }
  139. /**
  140. * Returns true if the files inside this snapshot
  141. * have not been changed
  142. *
  143. * @param {Snapshot} snapshot
  144. * @param {Compilation} mainCompilation
  145. * @returns {Promise<boolean | undefined>}
  146. */
  147. static isSnapshotValid(snapshot, mainCompilation) {
  148. return new Promise((resolve, reject) => {
  149. mainCompilation.fileSystemInfo.checkSnapshotValid(
  150. snapshot,
  151. (err, isValid) => {
  152. if (err) {
  153. reject(err);
  154. }
  155. resolve(isValid);
  156. },
  157. );
  158. });
  159. }
  160. static watchFiles(mainCompilation, fileDependencies) {
  161. Object.keys(fileDependencies).forEach((dependencyType) => {
  162. fileDependencies[dependencyType].forEach((fileDependency) => {
  163. mainCompilation[dependencyType].add(fileDependency);
  164. });
  165. });
  166. }
  167. constructor() {
  168. /**
  169. * @private
  170. * @type {
  171. | {
  172. isCompiling: false,
  173. isVerifyingCache: false,
  174. entries: string[],
  175. compiledEntries: string[],
  176. mainCompilationHash: string,
  177. compilationResult: ChildCompilationResult
  178. }
  179. | Readonly<{
  180. isCompiling: false,
  181. isVerifyingCache: true,
  182. entries: string[],
  183. previousEntries: string[],
  184. previousResult: ChildCompilationResult
  185. }>
  186. | Readonly <{
  187. isVerifyingCache: false,
  188. isCompiling: true,
  189. entries: string[],
  190. }>
  191. } the internal compilation state */
  192. this.compilationState = {
  193. isCompiling: false,
  194. isVerifyingCache: false,
  195. entries: [],
  196. compiledEntries: [],
  197. mainCompilationHash: "initial",
  198. compilationResult: {
  199. dependencies: {
  200. fileDependencies: [],
  201. contextDependencies: [],
  202. missingDependencies: [],
  203. },
  204. compiledEntries: {},
  205. },
  206. };
  207. }
  208. /**
  209. * apply is called by the webpack main compiler during the start phase
  210. * @param {Compiler} compiler
  211. */
  212. apply(compiler) {
  213. /** @type Promise<ChildCompilationResult> */
  214. let childCompilationResultPromise = Promise.resolve({
  215. dependencies: {
  216. fileDependencies: [],
  217. contextDependencies: [],
  218. missingDependencies: [],
  219. },
  220. compiledEntries: {},
  221. });
  222. /**
  223. * The main compilation hash which will only be updated
  224. * if the childCompiler changes
  225. */
  226. /** @type {string} */
  227. let mainCompilationHashOfLastChildRecompile = "";
  228. /** @type {Snapshot | undefined} */
  229. let previousFileSystemSnapshot;
  230. let compilationStartTime = new Date().getTime();
  231. compiler.hooks.make.tapAsync(
  232. "PersistentChildCompilerSingletonPlugin",
  233. (mainCompilation, callback) => {
  234. if (
  235. this.compilationState.isCompiling ||
  236. this.compilationState.isVerifyingCache
  237. ) {
  238. return callback(new Error("Child compilation has already started"));
  239. }
  240. // Update the time to the current compile start time
  241. compilationStartTime = new Date().getTime();
  242. // The compilation starts - adding new templates is now not possible anymore
  243. this.compilationState = {
  244. isCompiling: false,
  245. isVerifyingCache: true,
  246. previousEntries: this.compilationState.compiledEntries,
  247. previousResult: this.compilationState.compilationResult,
  248. entries: this.compilationState.entries,
  249. };
  250. // Validate cache:
  251. const isCacheValidPromise = this.isCacheValid(
  252. previousFileSystemSnapshot,
  253. mainCompilation,
  254. );
  255. let cachedResult = childCompilationResultPromise;
  256. childCompilationResultPromise = isCacheValidPromise.then(
  257. (isCacheValid) => {
  258. // Reuse cache
  259. if (isCacheValid) {
  260. return cachedResult;
  261. }
  262. // Start the compilation
  263. const compiledEntriesPromise = this.compileEntries(
  264. mainCompilation,
  265. this.compilationState.entries,
  266. );
  267. // Update snapshot as soon as we know the fileDependencies
  268. // this might possibly cause bugs if files were changed between
  269. // compilation start and snapshot creation
  270. compiledEntriesPromise
  271. .then((childCompilationResult) => {
  272. return PersistentChildCompilerSingletonPlugin.createSnapshot(
  273. childCompilationResult.dependencies,
  274. mainCompilation,
  275. compilationStartTime,
  276. );
  277. })
  278. .then((snapshot) => {
  279. previousFileSystemSnapshot = snapshot;
  280. });
  281. return compiledEntriesPromise;
  282. },
  283. );
  284. // Add files to compilation which needs to be watched:
  285. mainCompilation.hooks.optimizeTree.tapAsync(
  286. "PersistentChildCompilerSingletonPlugin",
  287. (chunks, modules, callback) => {
  288. const handleCompilationDonePromise =
  289. childCompilationResultPromise.then((childCompilationResult) => {
  290. this.watchFiles(
  291. mainCompilation,
  292. childCompilationResult.dependencies,
  293. );
  294. });
  295. handleCompilationDonePromise.then(
  296. // @ts-ignore
  297. () => callback(null, chunks, modules),
  298. callback,
  299. );
  300. },
  301. );
  302. // Store the final compilation once the main compilation hash is known
  303. mainCompilation.hooks.additionalAssets.tapAsync(
  304. "PersistentChildCompilerSingletonPlugin",
  305. (callback) => {
  306. const didRecompilePromise = Promise.all([
  307. childCompilationResultPromise,
  308. cachedResult,
  309. ]).then(([childCompilationResult, cachedResult]) => {
  310. // Update if childCompilation changed
  311. return cachedResult !== childCompilationResult;
  312. });
  313. const handleCompilationDonePromise = Promise.all([
  314. childCompilationResultPromise,
  315. didRecompilePromise,
  316. ]).then(([childCompilationResult, didRecompile]) => {
  317. // Update hash and snapshot if childCompilation changed
  318. if (didRecompile) {
  319. mainCompilationHashOfLastChildRecompile =
  320. /** @type {string} */ (mainCompilation.hash);
  321. }
  322. this.compilationState = {
  323. isCompiling: false,
  324. isVerifyingCache: false,
  325. entries: this.compilationState.entries,
  326. compiledEntries: this.compilationState.entries,
  327. compilationResult: childCompilationResult,
  328. mainCompilationHash: mainCompilationHashOfLastChildRecompile,
  329. };
  330. });
  331. handleCompilationDonePromise.then(() => callback(null), callback);
  332. },
  333. );
  334. // Continue compilation:
  335. callback(null);
  336. },
  337. );
  338. }
  339. /**
  340. * Add a new entry to the next compile run
  341. * @param {string} entry
  342. */
  343. addEntry(entry) {
  344. if (
  345. this.compilationState.isCompiling ||
  346. this.compilationState.isVerifyingCache
  347. ) {
  348. throw new Error(
  349. "The child compiler has already started to compile. " +
  350. "Please add entries before the main compiler 'make' phase has started or " +
  351. "after the compilation is done.",
  352. );
  353. }
  354. if (this.compilationState.entries.indexOf(entry) === -1) {
  355. this.compilationState.entries = [...this.compilationState.entries, entry];
  356. }
  357. }
  358. getLatestResult() {
  359. if (
  360. this.compilationState.isCompiling ||
  361. this.compilationState.isVerifyingCache
  362. ) {
  363. throw new Error(
  364. "The child compiler is not done compiling. " +
  365. "Please access the result after the compiler 'make' phase has started or " +
  366. "after the compilation is done.",
  367. );
  368. }
  369. return {
  370. mainCompilationHash: this.compilationState.mainCompilationHash,
  371. compilationResult: this.compilationState.compilationResult,
  372. };
  373. }
  374. /**
  375. * Verify that the cache is still valid
  376. * @private
  377. * @param {Snapshot | undefined} snapshot
  378. * @param {Compilation} mainCompilation
  379. * @returns {Promise<boolean | undefined>}
  380. */
  381. isCacheValid(snapshot, mainCompilation) {
  382. if (!this.compilationState.isVerifyingCache) {
  383. return Promise.reject(
  384. new Error(
  385. "Cache validation can only be done right before the compilation starts",
  386. ),
  387. );
  388. }
  389. // If there are no entries we don't need a new child compilation
  390. if (this.compilationState.entries.length === 0) {
  391. return Promise.resolve(true);
  392. }
  393. // If there are new entries the cache is invalid
  394. if (
  395. this.compilationState.entries !== this.compilationState.previousEntries
  396. ) {
  397. return Promise.resolve(false);
  398. }
  399. // Mark the cache as invalid if there is no snapshot
  400. if (!snapshot) {
  401. return Promise.resolve(false);
  402. }
  403. return PersistentChildCompilerSingletonPlugin.isSnapshotValid(
  404. snapshot,
  405. mainCompilation,
  406. );
  407. }
  408. /**
  409. * Start to compile all templates
  410. *
  411. * @private
  412. * @param {Compilation} mainCompilation
  413. * @param {string[]} entries
  414. * @returns {Promise<ChildCompilationResult>}
  415. */
  416. compileEntries(mainCompilation, entries) {
  417. const compiler = new HtmlWebpackChildCompiler(entries);
  418. return compiler.compileTemplates(mainCompilation).then(
  419. (result) => {
  420. return {
  421. // The compiled sources to render the content
  422. compiledEntries: result,
  423. // The file dependencies to find out if a
  424. // recompilation is required
  425. dependencies: compiler.fileDependencies,
  426. // The main compilation hash can be used to find out
  427. // if this compilation was done during the current compilation
  428. mainCompilationHash: mainCompilation.hash,
  429. };
  430. },
  431. (error) => ({
  432. // The compiled sources to render the content
  433. error,
  434. // The file dependencies to find out if a
  435. // recompilation is required
  436. dependencies: compiler.fileDependencies,
  437. // The main compilation hash can be used to find out
  438. // if this compilation was done during the current compilation
  439. mainCompilationHash: mainCompilation.hash,
  440. }),
  441. );
  442. }
  443. /**
  444. * @private
  445. * @param {Compilation} mainCompilation
  446. * @param {FileDependencies} files
  447. */
  448. watchFiles(mainCompilation, files) {
  449. PersistentChildCompilerSingletonPlugin.watchFiles(mainCompilation, files);
  450. }
  451. }
  452. module.exports = {
  453. CachedChildCompilation,
  454. };