EntryOptionPlugin.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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("./Compiler")} Compiler */
  10. /**
  11. * @param {string} context context path
  12. * @param {string | string[]} item entry array or single path
  13. * @param {string} name entry key name
  14. * @returns {SingleEntryPlugin | MultiEntryPlugin} returns either a single or multi entry plugin
  15. */
  16. const itemToPlugin = (context, item, name) => {
  17. if (Array.isArray(item)) {
  18. return new MultiEntryPlugin(context, item, name);
  19. }
  20. return new SingleEntryPlugin(context, item, name);
  21. };
  22. module.exports = class EntryOptionPlugin {
  23. /**
  24. * @param {Compiler} compiler the compiler instance one is tapping into
  25. * @returns {void}
  26. */
  27. apply(compiler) {
  28. compiler.hooks.entryOption.tap("EntryOptionPlugin", (context, entry) => {
  29. if (typeof entry === "string" || Array.isArray(entry)) {
  30. itemToPlugin(context, entry, "main").apply(compiler);
  31. } else if (typeof entry === "object") {
  32. for (const name of Object.keys(entry)) {
  33. itemToPlugin(context, entry[name], name).apply(compiler);
  34. }
  35. } else if (typeof entry === "function") {
  36. new DynamicEntryPlugin(context, entry).apply(compiler);
  37. }
  38. return true;
  39. });
  40. }
  41. };