Apply module bundling
[platform/framework/web/wrtjs.git] / node_modules / webpack / lib / EntryOptionPlugin.js
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 class 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                         baseUri: desc.baseUri,
66                         publicPath: desc.publicPath,
67                         chunkLoading: desc.chunkLoading,
68                         asyncChunks: desc.asyncChunks,
69                         wasmLoading: desc.wasmLoading,
70                         library: desc.library
71                 };
72                 if (desc.layer !== undefined && !compiler.options.experiments.layers) {
73                         throw new Error(
74                                 "'entryOptions.layer' is only allowed when 'experiments.layers' is enabled"
75                         );
76                 }
77                 if (desc.chunkLoading) {
78                         const EnableChunkLoadingPlugin = require("./javascript/EnableChunkLoadingPlugin");
79                         EnableChunkLoadingPlugin.checkEnabled(compiler, desc.chunkLoading);
80                 }
81                 if (desc.wasmLoading) {
82                         const EnableWasmLoadingPlugin = require("./wasm/EnableWasmLoadingPlugin");
83                         EnableWasmLoadingPlugin.checkEnabled(compiler, desc.wasmLoading);
84                 }
85                 if (desc.library) {
86                         const EnableLibraryPlugin = require("./library/EnableLibraryPlugin");
87                         EnableLibraryPlugin.checkEnabled(compiler, desc.library.type);
88                 }
89                 return options;
90         }
91 }
92
93 module.exports = EntryOptionPlugin;