EntryOptionPlugin.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. for (const entry of desc.import) {
  41. new EntryPlugin(context, entry, options).apply(compiler);
  42. }
  43. }
  44. }
  45. }
  46. /**
  47. * @param {Compiler} compiler the compiler
  48. * @param {string} name entry name
  49. * @param {EntryDescription} desc entry description
  50. * @returns {EntryOptions} options for the entry
  51. */
  52. static entryDescriptionToOptions(compiler, name, desc) {
  53. /** @type {EntryOptions} */
  54. const options = {
  55. name,
  56. filename: desc.filename,
  57. runtime: desc.runtime,
  58. layer: desc.layer,
  59. dependOn: desc.dependOn,
  60. baseUri: desc.baseUri,
  61. publicPath: desc.publicPath,
  62. chunkLoading: desc.chunkLoading,
  63. asyncChunks: desc.asyncChunks,
  64. wasmLoading: desc.wasmLoading,
  65. library: desc.library
  66. };
  67. if (desc.layer !== undefined && !compiler.options.experiments.layers) {
  68. throw new Error(
  69. "'entryOptions.layer' is only allowed when 'experiments.layers' is enabled"
  70. );
  71. }
  72. if (desc.chunkLoading) {
  73. const EnableChunkLoadingPlugin = require("./javascript/EnableChunkLoadingPlugin");
  74. EnableChunkLoadingPlugin.checkEnabled(compiler, desc.chunkLoading);
  75. }
  76. if (desc.wasmLoading) {
  77. const EnableWasmLoadingPlugin = require("./wasm/EnableWasmLoadingPlugin");
  78. EnableWasmLoadingPlugin.checkEnabled(compiler, desc.wasmLoading);
  79. }
  80. if (desc.library) {
  81. const EnableLibraryPlugin = require("./library/EnableLibraryPlugin");
  82. EnableLibraryPlugin.checkEnabled(compiler, desc.library.type);
  83. }
  84. return options;
  85. }
  86. }
  87. module.exports = EntryOptionPlugin;