UNPKG

2.78 kBJavaScriptView Raw
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
13class EntryOptionPlugin {
14 /**
15 * @param {Compiler} compiler the compiler instance one is tapping into
16 * @returns {void}
17 */
18 apply(compiler) {
19 compiler.hooks.entryOption.tap("EntryOptionPlugin", (context, entry) => {
20 EntryOptionPlugin.applyEntryOption(compiler, context, entry);
21 return true;
22 });
23 }
24
25 /**
26 * @param {Compiler} compiler the compiler
27 * @param {string} context context directory
28 * @param {Entry} entry request
29 * @returns {void}
30 */
31 static applyEntryOption(compiler, context, entry) {
32 if (typeof entry === "function") {
33 const DynamicEntryPlugin = require("./DynamicEntryPlugin");
34 new DynamicEntryPlugin(context, entry).apply(compiler);
35 } else {
36 const EntryPlugin = require("./EntryPlugin");
37 for (const name of Object.keys(entry)) {
38 const desc = entry[name];
39 const options = EntryOptionPlugin.entryDescriptionToOptions(
40 compiler,
41 name,
42 desc
43 );
44 for (const entry of desc.import) {
45 new EntryPlugin(context, entry, options).apply(compiler);
46 }
47 }
48 }
49 }
50
51 /**
52 * @param {Compiler} compiler the compiler
53 * @param {string} name entry name
54 * @param {EntryDescription} desc entry description
55 * @returns {EntryOptions} options for the entry
56 */
57 static entryDescriptionToOptions(compiler, name, desc) {
58 /** @type {EntryOptions} */
59 const options = {
60 name,
61 filename: desc.filename,
62 runtime: desc.runtime,
63 layer: desc.layer,
64 dependOn: desc.dependOn,
65 publicPath: desc.publicPath,
66 chunkLoading: desc.chunkLoading,
67 wasmLoading: desc.wasmLoading,
68 library: desc.library
69 };
70 if (desc.layer !== undefined && !compiler.options.experiments.layers) {
71 throw new Error(
72 "'entryOptions.layer' is only allowed when 'experiments.layers' is enabled"
73 );
74 }
75 if (desc.chunkLoading) {
76 const EnableChunkLoadingPlugin = require("./javascript/EnableChunkLoadingPlugin");
77 EnableChunkLoadingPlugin.checkEnabled(compiler, desc.chunkLoading);
78 }
79 if (desc.wasmLoading) {
80 const EnableWasmLoadingPlugin = require("./wasm/EnableWasmLoadingPlugin");
81 EnableWasmLoadingPlugin.checkEnabled(compiler, desc.wasmLoading);
82 }
83 if (desc.library) {
84 const EnableLibraryPlugin = require("./library/EnableLibraryPlugin");
85 EnableLibraryPlugin.checkEnabled(compiler, desc.library.type);
86 }
87 return options;
88 }
89}
90
91module.exports = EntryOptionPlugin;