EnableChunkLoadingPlugin.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @import { ChunkLoadingType } from "../../declarations/WebpackOptions" */
  7. /** @import Compiler from "../Compiler" */
  8. /** @typedef {Set<ChunkLoadingType>} ChunkLoadingTypes */
  9. /** @type {WeakMap<Compiler, ChunkLoadingTypes>} */
  10. const enabledTypes = new WeakMap();
  11. /**
  12. * Returns enabled types.
  13. * @param {Compiler} compiler compiler
  14. * @returns {ChunkLoadingTypes} enabled types
  15. */
  16. const getEnabledTypes = (compiler) => {
  17. let set = enabledTypes.get(compiler);
  18. if (set === undefined) {
  19. /** @type {ChunkLoadingTypes} */
  20. set = new Set();
  21. enabledTypes.set(compiler, set);
  22. }
  23. return set;
  24. };
  25. class EnableChunkLoadingPlugin {
  26. /**
  27. * Creates an instance of EnableChunkLoadingPlugin.
  28. * @param {ChunkLoadingType} type library type that should be available
  29. */
  30. constructor(type) {
  31. /** @type {string} */
  32. this.type = type;
  33. }
  34. /**
  35. * Updates enabled using the provided compiler.
  36. * @param {Compiler} compiler the compiler instance
  37. * @param {ChunkLoadingType} type type of library
  38. * @returns {void}
  39. */
  40. static setEnabled(compiler, type) {
  41. getEnabledTypes(compiler).add(type);
  42. }
  43. /**
  44. * Checks enabled.
  45. * @param {Compiler} compiler the compiler instance
  46. * @param {ChunkLoadingType} type type of library
  47. * @returns {void}
  48. */
  49. static checkEnabled(compiler, type) {
  50. if (!getEnabledTypes(compiler).has(type)) {
  51. throw new Error(
  52. `Chunk loading type "${type}" is not enabled. ` +
  53. "EnableChunkLoadingPlugin need to be used to enable this type of chunk loading. " +
  54. 'This usually happens through the "output.enabledChunkLoadingTypes" option. ' +
  55. 'If you are using a function as entry which sets "chunkLoading", you need to add all potential chunk loading types to "output.enabledChunkLoadingTypes". ' +
  56. `These types are enabled: ${[...getEnabledTypes(compiler)].join(", ")}`
  57. );
  58. }
  59. }
  60. /**
  61. * Applies the plugin by registering its hooks on the compiler.
  62. * @param {Compiler} compiler the compiler instance
  63. * @returns {void}
  64. */
  65. apply(compiler) {
  66. const { type } = this;
  67. // Only enable once
  68. const enabled = getEnabledTypes(compiler);
  69. if (enabled.has(type)) return;
  70. enabled.add(type);
  71. if (typeof type === "string") {
  72. switch (type) {
  73. case "jsonp": {
  74. const JsonpChunkLoadingPlugin = require("../web/JsonpChunkLoadingPlugin");
  75. new JsonpChunkLoadingPlugin().apply(compiler);
  76. break;
  77. }
  78. case "import-scripts": {
  79. const ImportScriptsChunkLoadingPlugin = require("../webworker/ImportScriptsChunkLoadingPlugin");
  80. new ImportScriptsChunkLoadingPlugin().apply(compiler);
  81. break;
  82. }
  83. case "require": {
  84. // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  85. const CommonJsChunkLoadingPlugin = require("../node/CommonJsChunkLoadingPlugin");
  86. new CommonJsChunkLoadingPlugin({
  87. asyncChunkLoading: false
  88. }).apply(compiler);
  89. break;
  90. }
  91. case "async-node": {
  92. // @ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
  93. const CommonJsChunkLoadingPlugin = require("../node/CommonJsChunkLoadingPlugin");
  94. new CommonJsChunkLoadingPlugin({
  95. asyncChunkLoading: true
  96. }).apply(compiler);
  97. break;
  98. }
  99. case "import": {
  100. const ModuleChunkLoadingPlugin = require("../esm/ModuleChunkLoadingPlugin");
  101. new ModuleChunkLoadingPlugin().apply(compiler);
  102. break;
  103. }
  104. default:
  105. throw new Error(`Unsupported chunk loading type ${type}.
  106. Plugins which provide custom chunk loading types must call EnableChunkLoadingPlugin.setEnabled(compiler, type) to disable this error.`);
  107. }
  108. } else {
  109. // TODO support plugin instances here
  110. // apply them to the compiler
  111. }
  112. }
  113. }
  114. module.exports = EnableChunkLoadingPlugin;