EntryOptionPlugin.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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").EntryDescriptionNormalized} EntryDescription */
  7. /** @typedef {import("../declarations/WebpackOptions").EntryNormalized} Entry */
  8. /** @typedef {import("./Compiler")} Compiler */
  9. /** @typedef {import("./Entrypoint").EntryOptions} EntryOptions */
  10. class EntryOptionPlugin {
  11. /**
  12. * @param {Compiler} compiler the compiler instance one is tapping into
  13. * @returns {void}
  14. */
  15. apply(compiler) {
  16. compiler.hooks.entryOption.tap("EntryOptionPlugin", (context, entry) => {
  17. EntryOptionPlugin.applyEntryOption(compiler, context, entry);
  18. return true;
  19. });
  20. }
  21. /**
  22. * @param {Compiler} compiler the compiler
  23. * @param {string} context context directory
  24. * @param {Entry} entry request
  25. * @returns {void}
  26. */
  27. static applyEntryOption(compiler, context, entry) {
  28. if (typeof entry === "function") {
  29. const DynamicEntryPlugin = require("./DynamicEntryPlugin");
  30. new DynamicEntryPlugin(context, entry).apply(compiler);
  31. } else {
  32. const EntryPlugin = require("./EntryPlugin");
  33. for (const name of Object.keys(entry)) {
  34. const desc = entry[name];
  35. const options = EntryOptionPlugin.entryDescriptionToOptions(
  36. compiler,
  37. name,
  38. desc
  39. );
  40. const descImport =
  41. /** @type {Exclude<EntryDescription["import"], undefined>} */
  42. (desc.import);
  43. for (const entry of descImport) {
  44. new EntryPlugin(context, entry, options).apply(compiler);
  45. }
  46. }
  47. }
  48. }
  49. /**
  50. * @param {Compiler} compiler the compiler
  51. * @param {string} name entry name
  52. * @param {EntryDescription} desc entry description
  53. * @returns {EntryOptions} options for the entry
  54. */
  55. static entryDescriptionToOptions(compiler, name, desc) {
  56. /** @type {EntryOptions} */
  57. const options = {
  58. name,
  59. filename: desc.filename,
  60. runtime: desc.runtime,
  61. layer: desc.layer,
  62. dependOn: desc.dependOn,
  63. baseUri: desc.baseUri,
  64. publicPath: desc.publicPath,
  65. chunkLoading: desc.chunkLoading,
  66. asyncChunks: desc.asyncChunks,
  67. wasmLoading: desc.wasmLoading,
  68. library: desc.library
  69. };
  70. if (desc.layer !== undefined && !compiler.options.experiments.layers) {
  71. throw new Error(
  72. "'entryOptions.layer' is only allowed when 'experiments.layers' is enabled"
  73. );
  74. }
  75. if (desc.chunkLoading) {
  76. const EnableChunkLoadingPlugin = require("./javascript/EnableChunkLoadingPlugin");
  77. EnableChunkLoadingPlugin.checkEnabled(compiler, desc.chunkLoading);
  78. }
  79. if (desc.wasmLoading) {
  80. const EnableWasmLoadingPlugin = require("./wasm/EnableWasmLoadingPlugin");
  81. EnableWasmLoadingPlugin.checkEnabled(compiler, desc.wasmLoading);
  82. }
  83. if (desc.library) {
  84. const EnableLibraryPlugin = require("./library/EnableLibraryPlugin");
  85. EnableLibraryPlugin.checkEnabled(compiler, desc.library.type);
  86. }
  87. return options;
  88. }
  89. }
  90. module.exports = EntryOptionPlugin;