CommonJsChunkLoadingPlugin.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /** @typedef {import("../Chunk")} Chunk */
  9. /** @typedef {import("../Compiler")} Compiler */
  10. /** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
  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. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  69. set.add(RuntimeGlobals.hasOwnProperty);
  70. compilation.addRuntimeModule(chunk, new ChunkLoadingRuntimeModule(set));
  71. };
  72. compilation.hooks.runtimeRequirementInTree
  73. .for(RuntimeGlobals.ensureChunkHandlers)
  74. .tap(PLUGIN_NAME, handler);
  75. compilation.hooks.runtimeRequirementInTree
  76. .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
  77. .tap(PLUGIN_NAME, handler);
  78. compilation.hooks.runtimeRequirementInTree
  79. .for(RuntimeGlobals.hmrDownloadManifest)
  80. .tap(PLUGIN_NAME, handler);
  81. compilation.hooks.runtimeRequirementInTree
  82. .for(RuntimeGlobals.baseURI)
  83. .tap(PLUGIN_NAME, handler);
  84. compilation.hooks.runtimeRequirementInTree
  85. .for(RuntimeGlobals.externalInstallChunk)
  86. .tap(PLUGIN_NAME, handler);
  87. compilation.hooks.runtimeRequirementInTree
  88. .for(RuntimeGlobals.onChunksLoaded)
  89. .tap(PLUGIN_NAME, handler);
  90. compilation.hooks.runtimeRequirementInTree
  91. .for(RuntimeGlobals.ensureChunkHandlers)
  92. .tap(PLUGIN_NAME, (chunk, set) => {
  93. if (!isEnabledForChunk(chunk)) return;
  94. set.add(RuntimeGlobals.getChunkScriptFilename);
  95. });
  96. compilation.hooks.runtimeRequirementInTree
  97. .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
  98. .tap(PLUGIN_NAME, (chunk, set) => {
  99. if (!isEnabledForChunk(chunk)) return;
  100. set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
  101. set.add(RuntimeGlobals.moduleCache);
  102. set.add(RuntimeGlobals.hmrModuleData);
  103. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  104. });
  105. compilation.hooks.runtimeRequirementInTree
  106. .for(RuntimeGlobals.hmrDownloadManifest)
  107. .tap(PLUGIN_NAME, (chunk, set) => {
  108. if (!isEnabledForChunk(chunk)) return;
  109. set.add(RuntimeGlobals.getUpdateManifestFilename);
  110. });
  111. });
  112. }
  113. }
  114. module.exports = CommonJsChunkLoadingPlugin;