EnableChunkLoadingPlugin.js 3.6 KB

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