webpack/lib/EntryOptionPlugin.js

46 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-05-17 00:27:59 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
2017-08-09 08:50:10 +08:00
*/
"use strict";
2015-05-17 00:27:59 +08:00
const SingleEntryPlugin = require("./SingleEntryPlugin");
const MultiEntryPlugin = require("./MultiEntryPlugin");
const DynamicEntryPlugin = require("./DynamicEntryPlugin");
2015-05-17 00:27:59 +08:00
/** @typedef {import("./Compiler")} Compiler */
/**
* @param {string} context context path
* @param {string | string[]} item entry array or single path
* @param {string} name entry key name
* @returns {SingleEntryPlugin | MultiEntryPlugin} returns either a single or multi entry plugin
*/
2017-11-08 18:32:05 +08:00
const itemToPlugin = (context, item, name) => {
2018-02-25 09:00:20 +08:00
if (Array.isArray(item)) {
return new MultiEntryPlugin(context, item, name);
}
2017-08-09 08:50:10 +08:00
return new SingleEntryPlugin(context, item, name);
2017-11-08 18:32:05 +08:00
};
module.exports = class EntryOptionPlugin {
/**
* @param {Compiler} compiler the compiler instance one is tapping into
* @returns {void}
*/
apply(compiler) {
2017-12-08 15:00:32 +08:00
compiler.hooks.entryOption.tap("EntryOptionPlugin", (context, entry) => {
2018-02-25 09:00:20 +08:00
if (typeof entry === "string" || Array.isArray(entry)) {
itemToPlugin(context, entry, "main").apply(compiler);
2018-02-25 09:00:20 +08:00
} else if (typeof entry === "object") {
for (const name of Object.keys(entry)) {
2018-01-22 20:52:43 +08:00
itemToPlugin(context, entry[name], name).apply(compiler);
}
2018-02-25 09:00:20 +08:00
} else if (typeof entry === "function") {
new DynamicEntryPlugin(context, entry).apply(compiler);
}
return true;
});
}
2017-01-11 17:51:58 +08:00
};