EnableChunkLoadingPlugin.js 3.6 KB

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