1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const EntryOptionPlugin = require("./EntryOptionPlugin");
|
9 | const EntryPlugin = require("./EntryPlugin");
|
10 | const EntryDependency = require("./dependencies/EntryDependency");
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | class DynamicEntryPlugin {
|
18 | |
19 |
|
20 |
|
21 |
|
22 | constructor(context, entry) {
|
23 | this.context = context;
|
24 | this.entry = entry;
|
25 | }
|
26 |
|
27 | |
28 |
|
29 |
|
30 |
|
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;
|