1 |
3a515b92
|
cagy
|
/*
|
2 |
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
3 |
|
|
Author Naoyuki Kanezawa @nkzawa
|
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 |
|
|
/** @typedef {import("../declarations/WebpackOptions").EntryDynamic} EntryDynamic */
|
14 |
|
|
/** @typedef {import("../declarations/WebpackOptions").EntryStatic} 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 |
|
|
* @param {Compiler} compiler the compiler instance
|
29 |
|
|
* @returns {void}
|
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 |
|
|
* @param {string|string[]} entry entry value or array of entry values
|
53 |
|
|
* @param {string} name name of entry
|
54 |
|
|
* @returns {Promise<EntryStatic>} returns the promise resolving the Compilation#addEntry function
|
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 |
|
|
* @param {string|string[]} entry entry value or array of entry paths
|
85 |
|
|
* @param {string} name name of entry
|
86 |
|
|
* @returns {SingleEntryDependency|MultiEntryDependency} returns dep
|
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 |
|
|
};
|