EnableChunkLoadingPlugin.js 3.8 KB

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