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").EntryDynamicNormalized} EntryDynamic */
|
---|
13 | /** @typedef {import("../declarations/WebpackOptions").EntryItem} EntryItem */
|
---|
14 | /** @typedef {import("../declarations/WebpackOptions").EntryStaticNormalized} EntryStatic */
|
---|
15 | /** @typedef {import("./Compiler")} Compiler */
|
---|
16 |
|
---|
17 | class DynamicEntryPlugin {
|
---|
18 | /**
|
---|
19 | * @param {string} context the context path
|
---|
20 | * @param {EntryDynamic} entry the entry value
|
---|
21 | */
|
---|
22 | constructor(context, entry) {
|
---|
23 | this.context = context;
|
---|
24 | this.entry = entry;
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Apply the plugin
|
---|
29 | * @param {Compiler} compiler the compiler instance
|
---|
30 | * @returns {void}
|
---|
31 | */
|
---|
32 | apply(compiler) {
|
---|
33 | compiler.hooks.compilation.tap(
|
---|
34 | "DynamicEntryPlugin",
|
---|
35 | (compilation, { normalModuleFactory }) => {
|
---|
36 | compilation.dependencyFactories.set(
|
---|
37 | EntryDependency,
|
---|
38 | normalModuleFactory
|
---|
39 | );
|
---|
40 | }
|
---|
41 | );
|
---|
42 |
|
---|
43 | compiler.hooks.make.tapPromise(
|
---|
44 | "DynamicEntryPlugin",
|
---|
45 | (compilation, callback) =>
|
---|
46 | Promise.resolve(this.entry())
|
---|
47 | .then(entry => {
|
---|
48 | const promises = [];
|
---|
49 | for (const name of Object.keys(entry)) {
|
---|
50 | const desc = entry[name];
|
---|
51 | const options = EntryOptionPlugin.entryDescriptionToOptions(
|
---|
52 | compiler,
|
---|
53 | name,
|
---|
54 | desc
|
---|
55 | );
|
---|
56 | for (const entry of desc.import) {
|
---|
57 | promises.push(
|
---|
58 | new Promise((resolve, reject) => {
|
---|
59 | compilation.addEntry(
|
---|
60 | this.context,
|
---|
61 | EntryPlugin.createDependency(entry, options),
|
---|
62 | options,
|
---|
63 | err => {
|
---|
64 | if (err) return reject(err);
|
---|
65 | resolve();
|
---|
66 | }
|
---|
67 | );
|
---|
68 | })
|
---|
69 | );
|
---|
70 | }
|
---|
71 | }
|
---|
72 | return Promise.all(promises);
|
---|
73 | })
|
---|
74 | .then(x => {})
|
---|
75 | );
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | module.exports = DynamicEntryPlugin;
|
---|