cached-child-compiler.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 asyncronously.
  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 = new PersistentChildCompilerSingletonPlugin();
  62. compilerMap.set(compiler, persistentChildCompilerSingletonPlugin);
  63. persistentChildCompilerSingletonPlugin.apply(compiler);
  64. }
  65. /**
  66. * apply is called by the webpack main compiler during the start phase
  67. * @param {string} entry
  68. */
  69. addEntry (entry) {
  70. const persistentChildCompilerSingletonPlugin = compilerMap.get(this.compiler);
  71. if (!persistentChildCompilerSingletonPlugin) {
  72. throw new Error(
  73. 'PersistentChildCompilerSingletonPlugin instance not found.'
  74. );
  75. }
  76. persistentChildCompilerSingletonPlugin.addEntry(entry);
  77. }
  78. getCompilationResult () {
  79. const persistentChildCompilerSingletonPlugin = compilerMap.get(this.compiler);
  80. if (!persistentChildCompilerSingletonPlugin) {
  81. throw new Error(
  82. 'PersistentChildCompilerSingletonPlugin instance not found.'
  83. );
  84. }
  85. return persistentChildCompilerSingletonPlugin.getLatestResult();
  86. }
  87. /**
  88. * Returns the result for the given entry
  89. * @param {string} entry
  90. * @returns {
  91. | { mainCompilationHash: string, error: Error }
  92. | { mainCompilationHash: string, compiledEntry: ChildCompilationTemplateResult }
  93. }
  94. */
  95. getCompilationEntryResult (entry) {
  96. const latestResult = this.getCompilationResult();
  97. const compilationResult = latestResult.compilationResult;
  98. return 'error' in compilationResult ? {
  99. mainCompilationHash: latestResult.mainCompilationHash,
  100. error: compilationResult.error
  101. } : {
  102. mainCompilationHash: latestResult.mainCompilationHash,
  103. compiledEntry: compilationResult.compiledEntries[entry]
  104. };
  105. }
  106. }
  107. class PersistentChildCompilerSingletonPlugin {
  108. /**
  109. *
  110. * @param {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]}} fileDependencies
  111. * @param {Compilation} mainCompilation
  112. * @param {number} startTime
  113. */
  114. static createSnapshot (fileDependencies, mainCompilation, startTime) {
  115. return new Promise((resolve, reject) => {
  116. mainCompilation.fileSystemInfo.createSnapshot(
  117. startTime,
  118. fileDependencies.fileDependencies,
  119. fileDependencies.contextDependencies,
  120. fileDependencies.missingDependencies,
  121. // @ts-ignore
  122. null,
  123. (err, snapshot) => {
  124. if (err) {
  125. return reject(err);
  126. }
  127. resolve(snapshot);
  128. }
  129. );
  130. });
  131. }
  132. /**
  133. * Returns true if the files inside this snapshot
  134. * have not been changed
  135. *
  136. * @param {Snapshot} snapshot
  137. * @param {Compilation} mainCompilation
  138. * @returns {Promise<boolean | undefined>}
  139. */
  140. static isSnapshotValid (snapshot, mainCompilation) {
  141. return new Promise((resolve, reject) => {
  142. mainCompilation.fileSystemInfo.checkSnapshotValid(
  143. snapshot,
  144. (err, isValid) => {
  145. if (err) {
  146. reject(err);
  147. }
  148. resolve(isValid);
  149. }
  150. );
  151. });
  152. }
  153. static watchFiles (mainCompilation, fileDependencies) {
  154. Object.keys(fileDependencies).forEach((depencyTypes) => {
  155. fileDependencies[depencyTypes].forEach(fileDependency => {
  156. mainCompilation[depencyTypes].add(fileDependency);
  157. });
  158. });
  159. }
  160. constructor () {
  161. /**
  162. * @private
  163. * @type {
  164. | {
  165. isCompiling: false,
  166. isVerifyingCache: false,
  167. entries: string[],
  168. compiledEntries: string[],
  169. mainCompilationHash: string,
  170. compilationResult: ChildCompilationResult
  171. }
  172. | Readonly<{
  173. isCompiling: false,
  174. isVerifyingCache: true,
  175. entries: string[],
  176. previousEntries: string[],
  177. previousResult: ChildCompilationResult
  178. }>
  179. | Readonly <{
  180. isVerifyingCache: false,
  181. isCompiling: true,
  182. entries: string[],
  183. }>
  184. } the internal compilation state */
  185. this.compilationState = {
  186. isCompiling: false,
  187. isVerifyingCache: false,
  188. entries: [],
  189. compiledEntries: [],
  190. mainCompilationHash: 'initial',
  191. compilationResult: {
  192. dependencies: {
  193. fileDependencies: [],
  194. contextDependencies: [],
  195. missingDependencies: []
  196. },
  197. compiledEntries: {}
  198. }
  199. };
  200. }
  201. /**
  202. * apply is called by the webpack main compiler during the start phase
  203. * @param {Compiler} compiler
  204. */
  205. apply (compiler) {
  206. /** @type Promise<ChildCompilationResult> */
  207. let childCompilationResultPromise = Promise.resolve({
  208. dependencies: {
  209. fileDependencies: [],
  210. contextDependencies: [],
  211. missingDependencies: []
  212. },
  213. compiledEntries: {}
  214. });
  215. /**
  216. * The main compilation hash which will only be updated
  217. * if the childCompiler changes
  218. */
  219. /** @type {string} */
  220. let mainCompilationHashOfLastChildRecompile = '';
  221. /** @type {Snapshot | undefined} */
  222. let previousFileSystemSnapshot;
  223. let compilationStartTime = new Date().getTime();
  224. compiler.hooks.make.tapAsync(
  225. 'PersistentChildCompilerSingletonPlugin',
  226. (mainCompilation, callback) => {
  227. if (this.compilationState.isCompiling || this.compilationState.isVerifyingCache) {
  228. return callback(new Error('Child compilation has already started'));
  229. }
  230. // Update the time to the current compile start time
  231. compilationStartTime = new Date().getTime();
  232. // The compilation starts - adding new templates is now not possible anymore
  233. this.compilationState = {
  234. isCompiling: false,
  235. isVerifyingCache: true,
  236. previousEntries: this.compilationState.compiledEntries,
  237. previousResult: this.compilationState.compilationResult,
  238. entries: this.compilationState.entries
  239. };
  240. // Validate cache:
  241. const isCacheValidPromise = this.isCacheValid(previousFileSystemSnapshot, mainCompilation);
  242. let cachedResult = childCompilationResultPromise;
  243. childCompilationResultPromise = isCacheValidPromise.then((isCacheValid) => {
  244. // Reuse cache
  245. if (isCacheValid) {
  246. return cachedResult;
  247. }
  248. // Start the compilation
  249. const compiledEntriesPromise = this.compileEntries(
  250. mainCompilation,
  251. this.compilationState.entries
  252. );
  253. // Update snapshot as soon as we know the filedependencies
  254. // this might possibly cause bugs if files were changed inbetween
  255. // compilation start and snapshot creation
  256. compiledEntriesPromise.then((childCompilationResult) => {
  257. return PersistentChildCompilerSingletonPlugin.createSnapshot(childCompilationResult.dependencies, mainCompilation, compilationStartTime);
  258. }).then((snapshot) => {
  259. previousFileSystemSnapshot = snapshot;
  260. });
  261. return compiledEntriesPromise;
  262. });
  263. // Add files to compilation which needs to be watched:
  264. mainCompilation.hooks.optimizeTree.tapAsync(
  265. 'PersistentChildCompilerSingletonPlugin',
  266. (chunks, modules, callback) => {
  267. const handleCompilationDonePromise = childCompilationResultPromise.then(
  268. childCompilationResult => {
  269. this.watchFiles(
  270. mainCompilation,
  271. childCompilationResult.dependencies
  272. );
  273. });
  274. // @ts-ignore
  275. handleCompilationDonePromise.then(() => callback(null, chunks, modules), callback);
  276. }
  277. );
  278. // Store the final compilation once the main compilation hash is known
  279. mainCompilation.hooks.additionalAssets.tapAsync(
  280. 'PersistentChildCompilerSingletonPlugin',
  281. (callback) => {
  282. const didRecompilePromise = Promise.all([childCompilationResultPromise, cachedResult]).then(
  283. ([childCompilationResult, cachedResult]) => {
  284. // Update if childCompilation changed
  285. return (cachedResult !== childCompilationResult);
  286. }
  287. );
  288. const handleCompilationDonePromise = Promise.all([childCompilationResultPromise, didRecompilePromise]).then(
  289. ([childCompilationResult, didRecompile]) => {
  290. // Update hash and snapshot if childCompilation changed
  291. if (didRecompile) {
  292. mainCompilationHashOfLastChildRecompile = /** @type {string} */ (mainCompilation.hash);
  293. }
  294. this.compilationState = {
  295. isCompiling: false,
  296. isVerifyingCache: false,
  297. entries: this.compilationState.entries,
  298. compiledEntries: this.compilationState.entries,
  299. compilationResult: childCompilationResult,
  300. mainCompilationHash: mainCompilationHashOfLastChildRecompile
  301. };
  302. });
  303. handleCompilationDonePromise.then(() => callback(null), callback);
  304. }
  305. );
  306. // Continue compilation:
  307. callback(null);
  308. }
  309. );
  310. }
  311. /**
  312. * Add a new entry to the next compile run
  313. * @param {string} entry
  314. */
  315. addEntry (entry) {
  316. if (this.compilationState.isCompiling || this.compilationState.isVerifyingCache) {
  317. throw new Error(
  318. 'The child compiler has already started to compile. ' +
  319. "Please add entries before the main compiler 'make' phase has started or " +
  320. 'after the compilation is done.'
  321. );
  322. }
  323. if (this.compilationState.entries.indexOf(entry) === -1) {
  324. this.compilationState.entries = [...this.compilationState.entries, entry];
  325. }
  326. }
  327. getLatestResult () {
  328. if (this.compilationState.isCompiling || this.compilationState.isVerifyingCache) {
  329. throw new Error(
  330. 'The child compiler is not done compiling. ' +
  331. "Please access the result after the compiler 'make' phase has started or " +
  332. 'after the compilation is done.'
  333. );
  334. }
  335. return {
  336. mainCompilationHash: this.compilationState.mainCompilationHash,
  337. compilationResult: this.compilationState.compilationResult
  338. };
  339. }
  340. /**
  341. * Verify that the cache is still valid
  342. * @private
  343. * @param {Snapshot | undefined} snapshot
  344. * @param {Compilation} mainCompilation
  345. * @returns {Promise<boolean | undefined>}
  346. */
  347. isCacheValid (snapshot, mainCompilation) {
  348. if (!this.compilationState.isVerifyingCache) {
  349. return Promise.reject(new Error('Cache validation can only be done right before the compilation starts'));
  350. }
  351. // If there are no entries we don't need a new child compilation
  352. if (this.compilationState.entries.length === 0) {
  353. return Promise.resolve(true);
  354. }
  355. // If there are new entries the cache is invalid
  356. if (this.compilationState.entries !== this.compilationState.previousEntries) {
  357. return Promise.resolve(false);
  358. }
  359. // Mark the cache as invalid if there is no snapshot
  360. if (!snapshot) {
  361. return Promise.resolve(false);
  362. }
  363. return PersistentChildCompilerSingletonPlugin.isSnapshotValid(snapshot, mainCompilation);
  364. }
  365. /**
  366. * Start to compile all templates
  367. *
  368. * @private
  369. * @param {Compilation} mainCompilation
  370. * @param {string[]} entries
  371. * @returns {Promise<ChildCompilationResult>}
  372. */
  373. compileEntries (mainCompilation, entries) {
  374. const compiler = new HtmlWebpackChildCompiler(entries);
  375. return compiler.compileTemplates(mainCompilation).then((result) => {
  376. return {
  377. // The compiled sources to render the content
  378. compiledEntries: result,
  379. // The file dependencies to find out if a
  380. // recompilation is required
  381. dependencies: compiler.fileDependencies,
  382. // The main compilation hash can be used to find out
  383. // if this compilation was done during the current compilation
  384. mainCompilationHash: mainCompilation.hash
  385. };
  386. }, error => ({
  387. // The compiled sources to render the content
  388. error,
  389. // The file dependencies to find out if a
  390. // recompilation is required
  391. dependencies: compiler.fileDependencies,
  392. // The main compilation hash can be used to find out
  393. // if this compilation was done during the current compilation
  394. mainCompilationHash: mainCompilation.hash
  395. }));
  396. }
  397. /**
  398. * @private
  399. * @param {Compilation} mainCompilation
  400. * @param {FileDependencies} files
  401. */
  402. watchFiles (mainCompilation, files) {
  403. PersistentChildCompilerSingletonPlugin.watchFiles(mainCompilation, files);
  404. }
  405. }
  406. module.exports = {
  407. CachedChildCompilation
  408. };