CommonJsChunkLoadingPlugin.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("../RuntimeGlobals");
  7. const StartupChunkDependenciesPlugin = require("../runtime/StartupChunkDependenciesPlugin");
  8. /** @import Chunk from "../Chunk" */
  9. /** @import Compiler from "../Compiler" */
  10. /** @import { RuntimeRequirements } from "../Module" */
  11. /**
  12. * Defines the common js chunk loading plugin options type used by this module.
  13. * @typedef {object} CommonJsChunkLoadingPluginOptions
  14. * @property {boolean=} asyncChunkLoading enable async chunk loading
  15. */
  16. const PLUGIN_NAME = "CommonJsChunkLoadingPlugin";
  17. class CommonJsChunkLoadingPlugin {
  18. /**
  19. * Creates an instance of CommonJsChunkLoadingPlugin.
  20. * @param {CommonJsChunkLoadingPluginOptions=} options options
  21. */
  22. constructor(options = {}) {
  23. /** @type {CommonJsChunkLoadingPluginOptions} */
  24. this.options = options;
  25. }
  26. /**
  27. * Applies the plugin by registering its hooks on the compiler.
  28. * @param {Compiler} compiler the compiler instance
  29. * @returns {void}
  30. */
  31. apply(compiler) {
  32. const ChunkLoadingRuntimeModule = this.options.asyncChunkLoading
  33. ? require("./ReadFileChunkLoadingRuntimeModule")
  34. : require("./RequireChunkLoadingRuntimeModule");
  35. const chunkLoadingValue = this.options.asyncChunkLoading
  36. ? "async-node"
  37. : "require";
  38. new StartupChunkDependenciesPlugin({
  39. chunkLoading: chunkLoadingValue,
  40. asyncChunkLoading: this.options.asyncChunkLoading
  41. }).apply(compiler);
  42. compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
  43. const globalChunkLoading = compilation.outputOptions.chunkLoading;
  44. /**
  45. * Checks whether this common js chunk loading plugin is enabled for chunk.
  46. * @param {Chunk} chunk chunk
  47. * @returns {boolean} true, if wasm loading is enabled for the chunk
  48. */
  49. const isEnabledForChunk = (chunk) => {
  50. const options = chunk.getEntryOptions();
  51. const chunkLoading =
  52. options && options.chunkLoading !== undefined
  53. ? options.chunkLoading
  54. : globalChunkLoading;
  55. return chunkLoading === chunkLoadingValue;
  56. };
  57. /** @type {WeakSet<Chunk>} */
  58. const onceForChunkSet = new WeakSet();
  59. /**
  60. * Handles the hook callback for this code path.
  61. * @param {Chunk} chunk chunk
  62. * @param {RuntimeRequirements} set runtime requirements
  63. */
  64. const handler = (chunk, set) => {
  65. if (onceForChunkSet.has(chunk)) return;
  66. onceForChunkSet.add(chunk);
  67. if (!isEnabledForChunk(chunk)) return;
  68. compilation.addRuntimeModule(chunk, new ChunkLoadingRuntimeModule(set));
  69. };
  70. /**
  71. * Installing a chunk is the only part that writes the module factories and
  72. * looks a chunk id up; a base uri or `onChunksLoaded` comes without either.
  73. * Kept off `handler`, which runs once per chunk and so cannot see which of the
  74. * requirements below the chunk ends up with.
  75. * @param {Chunk} chunk chunk
  76. * @param {RuntimeRequirements} set runtime requirements
  77. */
  78. const installHandler = (chunk, set) => {
  79. if (!isEnabledForChunk(chunk)) return;
  80. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  81. set.add(RuntimeGlobals.hasOwnProperty);
  82. };
  83. for (const requirement of [
  84. RuntimeGlobals.ensureChunkHandlers,
  85. RuntimeGlobals.hmrDownloadUpdateHandlers,
  86. RuntimeGlobals.hmrDownloadManifest,
  87. RuntimeGlobals.baseURI,
  88. RuntimeGlobals.externalInstallChunk,
  89. RuntimeGlobals.onChunksLoaded
  90. ]) {
  91. compilation.hooks.runtimeRequirementInTree
  92. .for(requirement)
  93. .tap(PLUGIN_NAME, handler);
  94. }
  95. for (const requirement of [
  96. RuntimeGlobals.ensureChunkHandlers,
  97. RuntimeGlobals.externalInstallChunk,
  98. RuntimeGlobals.hmrDownloadUpdateHandlers
  99. ]) {
  100. compilation.hooks.runtimeRequirementInTree
  101. .for(requirement)
  102. .tap(PLUGIN_NAME, installHandler);
  103. }
  104. compilation.hooks.runtimeRequirementInTree
  105. .for(RuntimeGlobals.ensureChunkHandlers)
  106. .tap(PLUGIN_NAME, (chunk, set) => {
  107. if (!isEnabledForChunk(chunk)) return;
  108. set.add(RuntimeGlobals.getChunkScriptFilename);
  109. });
  110. compilation.hooks.runtimeRequirementInTree
  111. .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
  112. .tap(PLUGIN_NAME, (chunk, set) => {
  113. if (!isEnabledForChunk(chunk)) return;
  114. set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
  115. set.add(RuntimeGlobals.moduleCache);
  116. set.add(RuntimeGlobals.hmrModuleData);
  117. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  118. });
  119. compilation.hooks.runtimeRequirementInTree
  120. .for(RuntimeGlobals.hmrDownloadManifest)
  121. .tap(PLUGIN_NAME, (chunk, set) => {
  122. if (!isEnabledForChunk(chunk)) return;
  123. set.add(RuntimeGlobals.getUpdateManifestFilename);
  124. });
  125. });
  126. }
  127. }
  128. module.exports = CommonJsChunkLoadingPlugin;