webpack/lib/EntryOptionPlugin.js

84 lines
2.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
/** @typedef {import("../declarations/WebpackOptions").Entry} Entry */
/** @typedef {import("../declarations/WebpackOptions").EntryDescription} EntryDescription */
2018-09-19 19:05:22 +08:00
/** @typedef {import("../declarations/WebpackOptions").EntryItem} EntryItem */
/** @typedef {import("../declarations/WebpackOptions").EntryStatic} EntryStatic */
/** @typedef {import("./Compilation").EntryOptions} EntryOptions */
/** @typedef {import("./Compiler")} Compiler */
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) => {
if (typeof entry === "function") {
const DynamicEntryPlugin = require("./DynamicEntryPlugin");
new DynamicEntryPlugin(context, entry).apply(compiler);
} else {
const EntryPlugin = require("./EntryPlugin");
EntryOptionPlugin.processEntryStatic(entry, (entry, options) => {
new EntryPlugin(context, entry, options).apply(compiler);
});
}
return true;
});
}
/**
* @param {EntryStatic} entry entry array or single path
* @param {function(string, EntryOptions): void} onEntry callback for each entry
* @returns {void}
*/
static processEntryStatic(entry, onEntry) {
/**
* @param {EntryItem} entry entry array or single path
* @param {EntryOptions} options entry options
* @returns {void}
*/
const applyEntryItemPlugins = (entry, options) => {
if (typeof entry === "string") {
onEntry(entry, options);
} else if (Array.isArray(entry)) {
for (const item of entry) {
applyEntryItemPlugins(item, options);
}
}
};
/**
* @param {EntryDescription} entry entry array or single path
* @param {EntryOptions} options entry options
* @returns {void}
*/
const applyEntryDescriptionPlugins = (entry, options) => {
2020-02-07 18:00:25 +08:00
applyEntryItemPlugins(entry.import, {
...options,
filename: entry.filename
});
};
if (typeof entry === "string" || Array.isArray(entry)) {
applyEntryItemPlugins(entry, { name: "main" });
} else if (entry && typeof entry === "object") {
for (const name of Object.keys(entry)) {
const value = entry[name];
if (typeof value === "string" || Array.isArray(value)) {
applyEntryItemPlugins(value, { name });
} else {
applyEntryDescriptionPlugins(value, { name });
}
}
}
}
}
module.exports = EntryOptionPlugin;