UNPKG

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