1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | const MultiEntryDependency = require("./dependencies/MultiEntryDependency");
|
8 | const SingleEntryDependency = require("./dependencies/SingleEntryDependency");
|
9 | const MultiModuleFactory = require("./MultiModuleFactory");
|
10 | const MultiEntryPlugin = require("./MultiEntryPlugin");
|
11 | const SingleEntryPlugin = require("./SingleEntryPlugin");
|
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 | apply(compiler) {
|
32 | compiler.hooks.compilation.tap(
|
33 | "DynamicEntryPlugin",
|
34 | (compilation, { normalModuleFactory }) => {
|
35 | const multiModuleFactory = new MultiModuleFactory();
|
36 |
|
37 | compilation.dependencyFactories.set(
|
38 | MultiEntryDependency,
|
39 | multiModuleFactory
|
40 | );
|
41 | compilation.dependencyFactories.set(
|
42 | SingleEntryDependency,
|
43 | normalModuleFactory
|
44 | );
|
45 | }
|
46 | );
|
47 |
|
48 | compiler.hooks.make.tapAsync(
|
49 | "DynamicEntryPlugin",
|
50 | (compilation, callback) => {
|
51 | |
52 |
|
53 |
|
54 |
|
55 |
|
56 | const addEntry = (entry, name) => {
|
57 | const dep = DynamicEntryPlugin.createDependency(entry, name);
|
58 | return new Promise((resolve, reject) => {
|
59 | compilation.addEntry(this.context, dep, name, err => {
|
60 | if (err) return reject(err);
|
61 | resolve();
|
62 | });
|
63 | });
|
64 | };
|
65 |
|
66 | Promise.resolve(this.entry()).then(entry => {
|
67 | if (typeof entry === "string" || Array.isArray(entry)) {
|
68 | addEntry(entry, "main").then(() => callback(), callback);
|
69 | } else if (typeof entry === "object") {
|
70 | Promise.all(
|
71 | Object.keys(entry).map(name => {
|
72 | return addEntry(entry[name], name);
|
73 | })
|
74 | ).then(() => callback(), callback);
|
75 | }
|
76 | });
|
77 | }
|
78 | );
|
79 | }
|
80 | }
|
81 |
|
82 | module.exports = DynamicEntryPlugin;
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | DynamicEntryPlugin.createDependency = (entry, name) => {
|
89 | if (Array.isArray(entry)) {
|
90 | return MultiEntryPlugin.createDependency(entry, name);
|
91 | } else {
|
92 | return SingleEntryPlugin.createDependency(entry, name);
|
93 | }
|
94 | };
|