EnableChunkLoadingPlugin.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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: ${Array.from(
  50. getEnabledTypes(compiler)
  51. ).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;