webpack/lib/EntryOptionPlugin.js

52 lines
1.5 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
*/
2018-07-30 23:08:51 +08:00
"use strict";
2015-05-17 00:27:59 +08:00
const DynamicEntryPlugin = require("./DynamicEntryPlugin");
const EntryPlugin = require("./EntryPlugin");
2015-05-17 00:27:59 +08:00
/** @typedef {import("../declarations/WebpackOptions").EntryDescription} EntryDescription */
2018-09-19 19:05:22 +08:00
/** @typedef {import("../declarations/WebpackOptions").EntryItem} EntryItem */
/** @typedef {import("./Compiler")} Compiler */
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) => {
/**
* @param {EntryItem|EntryDescription} entry entry array or single path
* @param {string} name entry key name
* @returns {void}
*/
const applyEntryPlugins = (entry, name) => {
if (typeof entry === "string") {
new EntryPlugin(context, entry, name).apply(compiler);
} else if (Array.isArray(entry)) {
for (const item of entry) {
applyEntryPlugins(item, name);
}
} else if (entry && typeof entry === "object") {
applyEntryPlugins(entry.import, name);
}
};
2018-02-25 09:00:20 +08:00
if (typeof entry === "string" || Array.isArray(entry)) {
applyEntryPlugins(entry, "main");
2018-02-25 09:00:20 +08:00
} else if (typeof entry === "object") {
for (const name of Object.keys(entry)) {
applyEntryPlugins(entry[name], name);
2018-01-22 20:52:43 +08:00
}
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
};