CommonJsChunkLoadingPlugin.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /**
  11. * @typedef {object} CommonJsChunkLoadingPluginOptions
  12. * @property {boolean} [asyncChunkLoading] enable async chunk loading
  13. */
  14. class CommonJsChunkLoadingPlugin {
  15. /**
  16. * @param {CommonJsChunkLoadingPluginOptions} [options] options
  17. */
  18. constructor(options = {}) {
  19. this._asyncChunkLoading = options.asyncChunkLoading;
  20. }
  21. /**
  22. * Apply the plugin
  23. * @param {Compiler} compiler the compiler instance
  24. * @returns {void}
  25. */
  26. apply(compiler) {
  27. const ChunkLoadingRuntimeModule = this._asyncChunkLoading
  28. ? require("./ReadFileChunkLoadingRuntimeModule")
  29. : require("./RequireChunkLoadingRuntimeModule");
  30. const chunkLoadingValue = this._asyncChunkLoading
  31. ? "async-node"
  32. : "require";
  33. new StartupChunkDependenciesPlugin({
  34. chunkLoading: chunkLoadingValue,
  35. asyncChunkLoading: this._asyncChunkLoading
  36. }).apply(compiler);
  37. compiler.hooks.thisCompilation.tap(
  38. "CommonJsChunkLoadingPlugin",
  39. compilation => {
  40. const globalChunkLoading = compilation.outputOptions.chunkLoading;
  41. /**
  42. * @param {Chunk} chunk chunk
  43. * @returns {boolean} true, if wasm loading is enabled for the chunk
  44. */
  45. const isEnabledForChunk = chunk => {
  46. const options = chunk.getEntryOptions();
  47. const chunkLoading =
  48. options && options.chunkLoading !== undefined
  49. ? options.chunkLoading
  50. : globalChunkLoading;
  51. return chunkLoading === chunkLoadingValue;
  52. };
  53. const onceForChunkSet = new WeakSet();
  54. /**
  55. * @param {Chunk} chunk chunk
  56. * @param {Set<string>} set runtime requirements
  57. */
  58. const handler = (chunk, set) => {
  59. if (onceForChunkSet.has(chunk)) return;
  60. onceForChunkSet.add(chunk);
  61. if (!isEnabledForChunk(chunk)) return;
  62. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  63. set.add(RuntimeGlobals.hasOwnProperty);
  64. compilation.addRuntimeModule(
  65. chunk,
  66. new ChunkLoadingRuntimeModule(set)
  67. );
  68. };
  69. compilation.hooks.runtimeRequirementInTree
  70. .for(RuntimeGlobals.ensureChunkHandlers)
  71. .tap("CommonJsChunkLoadingPlugin", handler);
  72. compilation.hooks.runtimeRequirementInTree
  73. .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
  74. .tap("CommonJsChunkLoadingPlugin", handler);
  75. compilation.hooks.runtimeRequirementInTree
  76. .for(RuntimeGlobals.hmrDownloadManifest)
  77. .tap("CommonJsChunkLoadingPlugin", handler);
  78. compilation.hooks.runtimeRequirementInTree
  79. .for(RuntimeGlobals.baseURI)
  80. .tap("CommonJsChunkLoadingPlugin", handler);
  81. compilation.hooks.runtimeRequirementInTree
  82. .for(RuntimeGlobals.externalInstallChunk)
  83. .tap("CommonJsChunkLoadingPlugin", handler);
  84. compilation.hooks.runtimeRequirementInTree
  85. .for(RuntimeGlobals.onChunksLoaded)
  86. .tap("CommonJsChunkLoadingPlugin", handler);
  87. compilation.hooks.runtimeRequirementInTree
  88. .for(RuntimeGlobals.ensureChunkHandlers)
  89. .tap("CommonJsChunkLoadingPlugin", (chunk, set) => {
  90. if (!isEnabledForChunk(chunk)) return;
  91. set.add(RuntimeGlobals.getChunkScriptFilename);
  92. });
  93. compilation.hooks.runtimeRequirementInTree
  94. .for(RuntimeGlobals.hmrDownloadUpdateHandlers)
  95. .tap("CommonJsChunkLoadingPlugin", (chunk, set) => {
  96. if (!isEnabledForChunk(chunk)) return;
  97. set.add(RuntimeGlobals.getChunkUpdateScriptFilename);
  98. set.add(RuntimeGlobals.moduleCache);
  99. set.add(RuntimeGlobals.hmrModuleData);
  100. set.add(RuntimeGlobals.moduleFactoriesAddOnly);
  101. });
  102. compilation.hooks.runtimeRequirementInTree
  103. .for(RuntimeGlobals.hmrDownloadManifest)
  104. .tap("CommonJsChunkLoadingPlugin", (chunk, set) => {
  105. if (!isEnabledForChunk(chunk)) return;
  106. set.add(RuntimeGlobals.getUpdateManifestFilename);
  107. });
  108. }
  109. );
  110. }
  111. }
  112. module.exports = CommonJsChunkLoadingPlugin;