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
|
|
|
|
2017-01-06 13:02:10 +08:00
|
|
|
"use strict";
|
2015-05-17 00:27:59 +08:00
|
|
|
|
2020-02-26 20:08:05 +08:00
|
|
|
/** @typedef {import("../declarations/WebpackOptions").EntryDescriptionNormalized} EntryDescription */
|
2020-10-12 15:45:19 +08:00
|
|
|
/** @typedef {import("../declarations/WebpackOptions").EntryNormalized} Entry */
|
2018-05-04 00:57:02 +08:00
|
|
|
/** @typedef {import("./Compiler")} Compiler */
|
2020-09-08 00:02:14 +08:00
|
|
|
/** @typedef {import("./Entrypoint").EntryOptions} EntryOptions */
|
2018-05-04 00:57:02 +08:00
|
|
|
|
2020-02-05 04:21:42 +08:00
|
|
|
class EntryOptionPlugin {
|
2018-05-04 00:57:02 +08:00
|
|
|
/**
|
|
|
|
* @param {Compiler} compiler the compiler instance one is tapping into
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-01-06 13:02:10 +08:00
|
|
|
apply(compiler) {
|
2020-10-12 15:37:07 +08:00
|
|
|
compiler.hooks.entryOption.tap("EntryOptionPlugin", (context, entry) => {
|
|
|
|
EntryOptionPlugin.applyEntryOption(compiler, context, entry);
|
|
|
|
return true;
|
|
|
|
});
|
2020-10-12 05:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Compiler} compiler the compiler
|
2020-10-12 15:37:07 +08:00
|
|
|
* @param {string} context context directory
|
2020-10-12 15:45:19 +08:00
|
|
|
* @param {Entry} entry request
|
2020-10-12 15:37:07 +08:00
|
|
|
* @returns {void}
|
2020-10-12 05:43:13 +08:00
|
|
|
*/
|
2020-10-12 15:37:07 +08:00
|
|
|
static applyEntryOption(compiler, context, entry) {
|
|
|
|
if (typeof entry === "function") {
|
|
|
|
const DynamicEntryPlugin = require("./DynamicEntryPlugin");
|
|
|
|
new DynamicEntryPlugin(context, entry).apply(compiler);
|
|
|
|
} else {
|
|
|
|
const EntryPlugin = require("./EntryPlugin");
|
|
|
|
for (const name of Object.keys(entry)) {
|
|
|
|
const desc = entry[name];
|
|
|
|
const options = EntryOptionPlugin.entryDescriptionToOptions(
|
|
|
|
compiler,
|
|
|
|
name,
|
|
|
|
desc
|
|
|
|
);
|
|
|
|
for (const entry of desc.import) {
|
|
|
|
new EntryPlugin(context, entry, options).apply(compiler);
|
2020-02-26 20:08:05 +08:00
|
|
|
}
|
2017-01-06 13:02:10 +08:00
|
|
|
}
|
2020-10-12 15:37:07 +08:00
|
|
|
}
|
2017-01-06 13:02:10 +08:00
|
|
|
}
|
2020-02-05 04:21:42 +08:00
|
|
|
|
|
|
|
/**
|
2020-02-26 19:34:57 +08:00
|
|
|
* @param {Compiler} compiler the compiler
|
2020-02-26 20:08:05 +08:00
|
|
|
* @param {string} name entry name
|
|
|
|
* @param {EntryDescription} desc entry description
|
|
|
|
* @returns {EntryOptions} options for the entry
|
2020-02-05 04:21:42 +08:00
|
|
|
*/
|
2020-02-26 19:34:57 +08:00
|
|
|
static entryDescriptionToOptions(compiler, name, desc) {
|
2020-02-26 20:08:05 +08:00
|
|
|
/** @type {EntryOptions} */
|
|
|
|
const options = {
|
|
|
|
name,
|
|
|
|
filename: desc.filename,
|
2020-07-30 17:18:09 +08:00
|
|
|
runtime: desc.runtime,
|
2020-12-22 21:51:09 +08:00
|
|
|
layer: desc.layer,
|
2020-02-26 19:34:57 +08:00
|
|
|
dependOn: desc.dependOn,
|
2021-04-14 22:38:01 +08:00
|
|
|
publicPath: desc.publicPath,
|
2020-08-26 03:45:56 +08:00
|
|
|
chunkLoading: desc.chunkLoading,
|
2021-11-10 21:23:03 +08:00
|
|
|
asyncChunks: desc.asyncChunks,
|
2020-09-09 15:23:01 +08:00
|
|
|
wasmLoading: desc.wasmLoading,
|
2020-02-26 19:34:57 +08:00
|
|
|
library: desc.library
|
2020-02-05 04:21:42 +08:00
|
|
|
};
|
2020-12-22 21:51:09 +08:00
|
|
|
if (desc.layer !== undefined && !compiler.options.experiments.layers) {
|
|
|
|
throw new Error(
|
|
|
|
"'entryOptions.layer' is only allowed when 'experiments.layers' is enabled"
|
|
|
|
);
|
|
|
|
}
|
2020-08-26 03:45:56 +08:00
|
|
|
if (desc.chunkLoading) {
|
|
|
|
const EnableChunkLoadingPlugin = require("./javascript/EnableChunkLoadingPlugin");
|
|
|
|
EnableChunkLoadingPlugin.checkEnabled(compiler, desc.chunkLoading);
|
|
|
|
}
|
2020-09-09 15:23:01 +08:00
|
|
|
if (desc.wasmLoading) {
|
|
|
|
const EnableWasmLoadingPlugin = require("./wasm/EnableWasmLoadingPlugin");
|
|
|
|
EnableWasmLoadingPlugin.checkEnabled(compiler, desc.wasmLoading);
|
|
|
|
}
|
2020-02-26 19:34:57 +08:00
|
|
|
if (desc.library) {
|
|
|
|
const EnableLibraryPlugin = require("./library/EnableLibraryPlugin");
|
|
|
|
EnableLibraryPlugin.checkEnabled(compiler, desc.library.type);
|
|
|
|
}
|
2020-02-26 20:08:05 +08:00
|
|
|
return options;
|
2020-02-05 04:21:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = EntryOptionPlugin;
|