EnableChunkLoadingPlugin.js 3.5 KB

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