1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Naoyuki Kanezawa @nkzawa
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const EntryOptionPlugin = require("./EntryOptionPlugin");
|
---|
9 | const EntryPlugin = require("./EntryPlugin");
|
---|
10 | const EntryDependency = require("./dependencies/EntryDependency");
|
---|
11 |
|
---|
12 | /** @typedef {import("../declarations/WebpackOptions").EntryDescriptionNormalized} EntryDescriptionNormalized */
|
---|
13 | /** @typedef {import("../declarations/WebpackOptions").EntryDynamicNormalized} EntryDynamic */
|
---|
14 | /** @typedef {import("../declarations/WebpackOptions").EntryItem} EntryItem */
|
---|
15 | /** @typedef {import("../declarations/WebpackOptions").EntryStaticNormalized} EntryStatic */
|
---|
16 | /** @typedef {import("./Compiler")} Compiler */
|
---|
17 |
|
---|
18 | class DynamicEntryPlugin {
|
---|
19 | /**
|
---|
20 | * @param {string} context the context path
|
---|
21 | * @param {EntryDynamic} entry the entry value
|
---|
22 | */
|
---|
23 | constructor(context, entry) {
|
---|
24 | this.context = context;
|
---|
25 | this.entry = entry;
|
---|
26 | }
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Apply the plugin
|
---|
30 | * @param {Compiler} compiler the compiler instance
|
---|
31 | * @returns {void}
|
---|
32 | */
|
---|
33 | apply(compiler) {
|
---|
34 | compiler.hooks.compilation.tap(
|
---|
35 | "DynamicEntryPlugin",
|
---|
36 | (compilation, { normalModuleFactory }) => {
|
---|
37 | compilation.dependencyFactories.set(
|
---|
38 | EntryDependency,
|
---|
39 | normalModuleFactory
|
---|
40 | );
|
---|
41 | }
|
---|
42 | );
|
---|
43 |
|
---|
44 | compiler.hooks.make.tapPromise("DynamicEntryPlugin", compilation =>
|
---|
45 | Promise.resolve(this.entry())
|
---|
46 | .then(entry => {
|
---|
47 | const promises = [];
|
---|
48 | for (const name of Object.keys(entry)) {
|
---|
49 | const desc = entry[name];
|
---|
50 | const options = EntryOptionPlugin.entryDescriptionToOptions(
|
---|
51 | compiler,
|
---|
52 | name,
|
---|
53 | desc
|
---|
54 | );
|
---|
55 | for (const entry of /** @type {NonNullable<EntryDescriptionNormalized["import"]>} */ (
|
---|
56 | desc.import
|
---|
57 | )) {
|
---|
58 | promises.push(
|
---|
59 | new Promise(
|
---|
60 | /**
|
---|
61 | * @param {(value?: any) => void} resolve resolve
|
---|
62 | * @param {(reason?: Error) => void} reject reject
|
---|
63 | */
|
---|
64 | (resolve, reject) => {
|
---|
65 | compilation.addEntry(
|
---|
66 | this.context,
|
---|
67 | EntryPlugin.createDependency(entry, options),
|
---|
68 | options,
|
---|
69 | err => {
|
---|
70 | if (err) return reject(err);
|
---|
71 | resolve();
|
---|
72 | }
|
---|
73 | );
|
---|
74 | }
|
---|
75 | )
|
---|
76 | );
|
---|
77 | }
|
---|
78 | }
|
---|
79 | return Promise.all(promises);
|
---|
80 | })
|
---|
81 | .then(x => {})
|
---|
82 | );
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | module.exports = DynamicEntryPlugin;
|
---|