EntryOptionPlugin.js 2.9 KB

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