| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | /** @typedef {import("../declarations/WebpackOptions").EntryDescriptionNormalized} EntryDescription */
|
|---|
| 9 | /** @typedef {import("../declarations/WebpackOptions").EntryNormalized} Entry */
|
|---|
| 10 | /** @typedef {import("./Compiler")} Compiler */
|
|---|
| 11 | /** @typedef {import("./Entrypoint").EntryOptions} EntryOptions */
|
|---|
| 12 |
|
|---|
| 13 | const PLUGIN_NAME = "EntryOptionPlugin";
|
|---|
| 14 |
|
|---|
| 15 | class EntryOptionPlugin {
|
|---|
| 16 | /**
|
|---|
| 17 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 18 | * @param {Compiler} compiler the compiler instance one is tapping into
|
|---|
| 19 | * @returns {void}
|
|---|
| 20 | */
|
|---|
| 21 | apply(compiler) {
|
|---|
| 22 | compiler.hooks.entryOption.tap(PLUGIN_NAME, (context, entry) => {
|
|---|
| 23 | EntryOptionPlugin.applyEntryOption(compiler, context, entry);
|
|---|
| 24 | return true;
|
|---|
| 25 | });
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Apply entry option.
|
|---|
| 30 | * @param {Compiler} compiler the compiler
|
|---|
| 31 | * @param {string} context context directory
|
|---|
| 32 | * @param {Entry} entry request
|
|---|
| 33 | * @returns {void}
|
|---|
| 34 | */
|
|---|
| 35 | static applyEntryOption(compiler, context, entry) {
|
|---|
| 36 | if (typeof entry === "function") {
|
|---|
| 37 | const DynamicEntryPlugin = require("./DynamicEntryPlugin");
|
|---|
| 38 |
|
|---|
| 39 | new DynamicEntryPlugin(context, entry).apply(compiler);
|
|---|
| 40 | } else {
|
|---|
| 41 | const EntryPlugin = require("./EntryPlugin");
|
|---|
| 42 |
|
|---|
| 43 | for (const name of Object.keys(entry)) {
|
|---|
| 44 | const desc = entry[name];
|
|---|
| 45 | const options = EntryOptionPlugin.entryDescriptionToOptions(
|
|---|
| 46 | compiler,
|
|---|
| 47 | name,
|
|---|
| 48 | desc
|
|---|
| 49 | );
|
|---|
| 50 | const descImport =
|
|---|
| 51 | /** @type {Exclude<EntryDescription["import"], undefined>} */
|
|---|
| 52 | (desc.import);
|
|---|
| 53 | for (const entry of descImport) {
|
|---|
| 54 | new EntryPlugin(context, entry, options).apply(compiler);
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /**
|
|---|
| 61 | * Entry description to options.
|
|---|
| 62 | * @param {Compiler} compiler the compiler
|
|---|
| 63 | * @param {string} name entry name
|
|---|
| 64 | * @param {EntryDescription} desc entry description
|
|---|
| 65 | * @returns {EntryOptions} options for the entry
|
|---|
| 66 | */
|
|---|
| 67 | static entryDescriptionToOptions(compiler, name, desc) {
|
|---|
| 68 | /** @type {EntryOptions} */
|
|---|
| 69 | const options = {
|
|---|
| 70 | name,
|
|---|
| 71 | filename: desc.filename,
|
|---|
| 72 | runtime: desc.runtime,
|
|---|
| 73 | layer: desc.layer,
|
|---|
| 74 | dependOn: desc.dependOn,
|
|---|
| 75 | baseUri: desc.baseUri,
|
|---|
| 76 | publicPath: desc.publicPath,
|
|---|
| 77 | chunkLoading: desc.chunkLoading,
|
|---|
| 78 | asyncChunks: desc.asyncChunks,
|
|---|
| 79 | wasmLoading: desc.wasmLoading,
|
|---|
| 80 | library: desc.library
|
|---|
| 81 | };
|
|---|
| 82 | if (desc.chunkLoading) {
|
|---|
| 83 | const EnableChunkLoadingPlugin = require("./javascript/EnableChunkLoadingPlugin");
|
|---|
| 84 |
|
|---|
| 85 | EnableChunkLoadingPlugin.checkEnabled(compiler, desc.chunkLoading);
|
|---|
| 86 | }
|
|---|
| 87 | if (desc.wasmLoading) {
|
|---|
| 88 | const EnableWasmLoadingPlugin = require("./wasm/EnableWasmLoadingPlugin");
|
|---|
| 89 |
|
|---|
| 90 | EnableWasmLoadingPlugin.checkEnabled(compiler, desc.wasmLoading);
|
|---|
| 91 | }
|
|---|
| 92 | if (desc.library) {
|
|---|
| 93 | const EnableLibraryPlugin = require("./library/EnableLibraryPlugin");
|
|---|
| 94 |
|
|---|
| 95 | EnableLibraryPlugin.checkEnabled(compiler, desc.library.type);
|
|---|
| 96 | }
|
|---|
| 97 | return options;
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | module.exports = EntryOptionPlugin;
|
|---|