cached-child-compiler.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. // Import types
  25. /** @typedef {import("webpack/lib/Compiler.js")} WebpackCompiler */
  26. /** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */
  27. /** @typedef {{hash: string, entry: any, content: string }} ChildCompilationResultEntry */
  28. /** @typedef {import("./webpack4/file-watcher-api").Snapshot} Snapshot */
  29. /** @typedef {{fileDependencies: string[], contextDependencies: string[], missingDependencies: string[]}} FileDependencies */
  30. /** @typedef {{
  31. dependencies: FileDependencies,
  32. compiledEntries: {[entryName: string]: ChildCompilationResultEntry}
  33. } | {
  34. dependencies: FileDependencies,
  35. error: Error
  36. }} ChildCompilationResult */
  37. 'use strict';
  38. const { HtmlWebpackChildCompiler } = require('./child-compiler');
  39. const fileWatcherApi = require('./file-watcher-api');
  40. /**
  41. * This plugin is a singleton for performance reasons.
  42. * To keep track if a plugin does already exist for the compiler they are cached
  43. * in this map
  44. * @type {WeakMap<WebpackCompiler, PersistentChildCompilerSingletonPlugin>}}
  45. */
  46. const compilerMap = new WeakMap();
  47. class CachedChildCompilation {
  48. /**
  49. * @param {WebpackCompiler} compiler
  50. */
  51. constructor (compiler) {
  52. /**
  53. * @private
  54. * @type {WebpackCompiler}
  55. */
  56. this.compiler = compiler;
  57. // Create a singleton instance for the compiler
  58. // if there is none
  59. if (compilerMap.has(compiler)) {
  60. return;
  61. }
  62. const persistentChildCompilerSingletonPlugin = 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(this.compiler);
  72. if (!persistentChildCompilerSingletonPlugin) {
  73. throw new Error(
  74. 'PersistentChildCompilerSingletonPlugin instance not found.'
  75. );
  76. }
  77. persistentChildCompilerSingletonPlugin.addEntry(entry);
  78. }
  79. getCompilationResult () {
  80. const persistentChildCompilerSingletonPlugin = compilerMap.get(this.compiler);
  81. if (!persistentChildCompilerSingletonPlugin) {
  82. throw new Error(
  83. 'PersistentChildCompilerSingletonPlugin instance not found.'
  84. );
  85. }
  86. return persistentChildCompilerSingletonPlugin.getLatestResult();
  87. }
  88. /**
  89. * Returns the result for the given entry
  90. * @param {string} entry
  91. * @returns {
  92. | { mainCompilationHash: string, error: Error }
  93. | { mainCompilationHash: string, compiledEntry: ChildCompilationResultEntry }
  94. }
  95. */
  96. getCompilationEntryResult (entry) {
  97. const latestResult = this.getCompilationResult();
  98. const compilationResult = latestResult.compilationResult;
  99. return 'error' in compilationResult ? {
  100. mainCompilationHash: latestResult.mainCompilationHash,
  101. error: compilationResult.error
  102. } : {
  103. mainCompilationHash: latestResult.mainCompilationHash,
  104. compiledEntry: compilationResult.compiledEntries[entry]
  105. };
  106. }
  107. }
  108. class PersistentChildCompilerSingletonPlugin {
  109. constructor () {
  110. /**
  111. * @private
  112. * @type {
  113. | {
  114. isCompiling: false,
  115. isVerifyingCache: false,
  116. entries: string[],
  117. compiledEntries: string[],
  118. mainCompilationHash: string,
  119. compilationResult: ChildCompilationResult
  120. }
  121. | Readonly<{
  122. isCompiling: false,
  123. isVerifyingCache: true,
  124. entries: string[],
  125. previousEntries: string[],
  126. previousResult: ChildCompilationResult
  127. }>
  128. | Readonly <{
  129. isVerifyingCache: false,
  130. isCompiling: true,
  131. entries: string[],
  132. }>
  133. } the internal compilation state */
  134. this.compilationState = {
  135. isCompiling: false,
  136. isVerifyingCache: false,
  137. entries: [],
  138. compiledEntries: [],
  139. mainCompilationHash: 'initial',
  140. compilationResult: {
  141. dependencies: {
  142. fileDependencies: [],
  143. contextDependencies: [],
  144. missingDependencies: []
  145. },
  146. compiledEntries: {}
  147. }
  148. };
  149. }
  150. /**
  151. * apply is called by the webpack main compiler during the start phase
  152. * @param {WebpackCompiler} compiler
  153. */
  154. apply (compiler) {
  155. /** @type Promise<ChildCompilationResult> */
  156. let childCompilationResultPromise = Promise.resolve({
  157. dependencies: {
  158. fileDependencies: [],
  159. contextDependencies: [],
  160. missingDependencies: []
  161. },
  162. compiledEntries: {}
  163. });
  164. /**
  165. * The main compilation hash which will only be updated
  166. * if the childCompiler changes
  167. */
  168. let mainCompilationHashOfLastChildRecompile = '';
  169. /** @typedef{Snapshot|undefined} */
  170. let previousFileSystemSnapshot;
  171. let compilationStartTime = new Date().getTime();
  172. compiler.hooks.make.tapAsync(
  173. 'PersistentChildCompilerSingletonPlugin',
  174. (mainCompilation, callback) => {
  175. if (this.compilationState.isCompiling || this.compilationState.isVerifyingCache) {
  176. return callback(new Error('Child compilation has already started'));
  177. }
  178. // Update the time to the current compile start time
  179. compilationStartTime = new Date().getTime();
  180. // The compilation starts - adding new templates is now not possible anymore
  181. this.compilationState = {
  182. isCompiling: false,
  183. isVerifyingCache: true,
  184. previousEntries: this.compilationState.compiledEntries,
  185. previousResult: this.compilationState.compilationResult,
  186. entries: this.compilationState.entries
  187. };
  188. // Validate cache:
  189. const isCacheValidPromise = this.isCacheValid(previousFileSystemSnapshot, mainCompilation);
  190. let cachedResult = childCompilationResultPromise;
  191. childCompilationResultPromise = isCacheValidPromise.then((isCacheValid) => {
  192. // Reuse cache
  193. if (isCacheValid) {
  194. return cachedResult;
  195. }
  196. // Start the compilation
  197. const compiledEntriesPromise = this.compileEntries(
  198. mainCompilation,
  199. this.compilationState.entries
  200. );
  201. // Update snapshot as soon as we know the filedependencies
  202. // this might possibly cause bugs if files were changed inbetween
  203. // compilation start and snapshot creation
  204. compiledEntriesPromise.then((childCompilationResult) => {
  205. return fileWatcherApi.createSnapshot(childCompilationResult.dependencies, mainCompilation, compilationStartTime);
  206. }).then((snapshot) => {
  207. previousFileSystemSnapshot = snapshot;
  208. });
  209. return compiledEntriesPromise;
  210. });
  211. // Add files to compilation which needs to be watched:
  212. mainCompilation.hooks.optimizeTree.tapAsync(
  213. 'PersistentChildCompilerSingletonPlugin',
  214. (chunks, modules, callback) => {
  215. const handleCompilationDonePromise = childCompilationResultPromise.then(
  216. childCompilationResult => {
  217. this.watchFiles(
  218. mainCompilation,
  219. childCompilationResult.dependencies
  220. );
  221. });
  222. handleCompilationDonePromise.then(() => callback(null, chunks, modules), callback);
  223. }
  224. );
  225. // Store the final compilation once the main compilation hash is known
  226. mainCompilation.hooks.additionalAssets.tapAsync(
  227. 'PersistentChildCompilerSingletonPlugin',
  228. (callback) => {
  229. const didRecompilePromise = Promise.all([childCompilationResultPromise, cachedResult]).then(
  230. ([childCompilationResult, cachedResult]) => {
  231. // Update if childCompilation changed
  232. return (cachedResult !== childCompilationResult);
  233. }
  234. );
  235. const handleCompilationDonePromise = Promise.all([childCompilationResultPromise, didRecompilePromise]).then(
  236. ([childCompilationResult, didRecompile]) => {
  237. // Update hash and snapshot if childCompilation changed
  238. if (didRecompile) {
  239. mainCompilationHashOfLastChildRecompile = mainCompilation.hash;
  240. }
  241. this.compilationState = {
  242. isCompiling: false,
  243. isVerifyingCache: false,
  244. entries: this.compilationState.entries,
  245. compiledEntries: this.compilationState.entries,
  246. compilationResult: childCompilationResult,
  247. mainCompilationHash: mainCompilationHashOfLastChildRecompile
  248. };
  249. });
  250. handleCompilationDonePromise.then(() => callback(null), callback);
  251. }
  252. );
  253. // Continue compilation:
  254. callback(null);
  255. }
  256. );
  257. }
  258. /**
  259. * Add a new entry to the next compile run
  260. * @param {string} entry
  261. */
  262. addEntry (entry) {
  263. if (this.compilationState.isCompiling || this.compilationState.isVerifyingCache) {
  264. throw new Error(
  265. 'The child compiler has already started to compile. ' +
  266. "Please add entries before the main compiler 'make' phase has started or " +
  267. 'after the compilation is done.'
  268. );
  269. }
  270. if (this.compilationState.entries.indexOf(entry) === -1) {
  271. this.compilationState.entries = [...this.compilationState.entries, entry];
  272. }
  273. }
  274. getLatestResult () {
  275. if (this.compilationState.isCompiling || this.compilationState.isVerifyingCache) {
  276. throw new Error(
  277. 'The child compiler is not done compiling. ' +
  278. "Please access the result after the compiler 'make' phase has started or " +
  279. 'after the compilation is done.'
  280. );
  281. }
  282. return {
  283. mainCompilationHash: this.compilationState.mainCompilationHash,
  284. compilationResult: this.compilationState.compilationResult
  285. };
  286. }
  287. /**
  288. * Verify that the cache is still valid
  289. * @private
  290. * @param {Snapshot | undefined} snapshot
  291. * @param {WebpackCompilation} mainCompilation
  292. * @returns {Promise<boolean>}
  293. */
  294. isCacheValid (snapshot, mainCompilation) {
  295. if (!this.compilationState.isVerifyingCache) {
  296. return Promise.reject(new Error('Cache validation can only be done right before the compilation starts'));
  297. }
  298. // If there are no entries we don't need a new child compilation
  299. if (this.compilationState.entries.length === 0) {
  300. return Promise.resolve(true);
  301. }
  302. // If there are new entries the cache is invalid
  303. if (this.compilationState.entries !== this.compilationState.previousEntries) {
  304. return Promise.resolve(false);
  305. }
  306. // Mark the cache as invalid if there is no snapshot
  307. if (!snapshot) {
  308. return Promise.resolve(false);
  309. }
  310. return fileWatcherApi.isSnapShotValid(snapshot, mainCompilation);
  311. }
  312. /**
  313. * Start to compile all templates
  314. *
  315. * @private
  316. * @param {WebpackCompilation} mainCompilation
  317. * @param {string[]} entries
  318. * @returns {Promise<ChildCompilationResult>}
  319. */
  320. compileEntries (mainCompilation, entries) {
  321. const compiler = new HtmlWebpackChildCompiler(entries);
  322. return compiler.compileTemplates(mainCompilation).then((result) => {
  323. return {
  324. // The compiled sources to render the content
  325. compiledEntries: result,
  326. // The file dependencies to find out if a
  327. // recompilation is required
  328. dependencies: compiler.fileDependencies,
  329. // The main compilation hash can be used to find out
  330. // if this compilation was done during the current compilation
  331. mainCompilationHash: mainCompilation.hash
  332. };
  333. }, error => ({
  334. // The compiled sources to render the content
  335. error,
  336. // The file dependencies to find out if a
  337. // recompilation is required
  338. dependencies: compiler.fileDependencies,
  339. // The main compilation hash can be used to find out
  340. // if this compilation was done during the current compilation
  341. mainCompilationHash: mainCompilation.hash
  342. }));
  343. }
  344. /**
  345. * @private
  346. * @param {WebpackCompilation} mainCompilation
  347. * @param {FileDependencies} files
  348. */
  349. watchFiles (mainCompilation, files) {
  350. fileWatcherApi.watchFiles(mainCompilation, files);
  351. }
  352. }
  353. module.exports = {
  354. CachedChildCompilation
  355. };