EntryOptionPlugin.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const SingleEntryPlugin = require("./SingleEntryPlugin");
  7. const MultiEntryPlugin = require("./MultiEntryPlugin");
  8. const DynamicEntryPlugin = require("./DynamicEntryPlugin");
  9. /** @typedef {import("../declarations/WebpackOptions").EntryItem} EntryItem */
  10. /** @typedef {import("./Compiler")} Compiler */
  11. /**
  12. * @param {string} context context path
  13. * @param {EntryItem} item entry array or single path
  14. * @param {string} name entry key name
  15. * @returns {SingleEntryPlugin | MultiEntryPlugin} returns either a single or multi entry plugin
  16. */
  17. const itemToPlugin = (context, item, name) => {
  18. if (Array.isArray(item)) {
  19. return new MultiEntryPlugin(context, item, name);
  20. }
  21. return new SingleEntryPlugin(context, item, name);
  22. };
  23. module.exports = class EntryOptionPlugin {
  24. /**
  25. * @param {Compiler} compiler the compiler instance one is tapping into
  26. * @returns {void}
  27. */
  28. apply(compiler) {
  29. compiler.hooks.entryOption.tap("EntryOptionPlugin", (context, entry) => {
  30. if (typeof entry === "string" || Array.isArray(entry)) {
  31. itemToPlugin(context, entry, "main").apply(compiler);
  32. } else if (typeof entry === "object") {
  33. for (const name of Object.keys(entry)) {
  34. itemToPlugin(context, entry[name], name).apply(compiler);
  35. }
  36. } else if (typeof entry === "function") {
  37. new DynamicEntryPlugin(context, entry).apply(compiler);
  38. }
  39. return true;
  40. });
  41. }
  42. };