-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
151 lines (137 loc) · 4.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* @copyright 2021-present Kriasoft (https://git.io/JtoKE)
*
* @typedef {import("webpack").Compiler} Compiler
* @typedef {import("webpack").compilation.Compilation} Compilation
* @typedef {import("webpack").Configuration} Configuration
* @typedef {import("webpack").javascript.JavascriptParser} JavascriptParser
*/
const path = require("path");
const webpack = require("webpack");
/**
* Creates an additional application bundle for the selected execution
* environment (Webpack `target`).
*/
class BundleWebpackPlugin {
/**
* Creates a new instance of the plugin.
*
* @param {Configuration} config Webpack configuration to use for the bundle.
* @param {string} [config.entry] The entry object (file path).
*/
constructor(config = {}) {
this.config = config;
}
/**
* @param {Compiler} compiler
*/
apply(compiler) {
this.name = this.constructor.name;
this.compiler = compiler;
compiler.hooks.make.tapAsync(this.name, this.handleMake.bind(this));
compiler.hooks.compilation.tap(
this.name,
(compilation, { normalModuleFactory }) => {
normalModuleFactory.hooks.parser
.for("javascript/auto")
.tap(this.name, this.handleParser.bind(this));
}
);
}
/**
* @param {Compilation} compilation
*/
handleMake(compilation, cb) {
const name = this.config.name;
const srcPath = path.resolve(this.config.entry);
compilation.fileDependencies.add(srcPath);
const baseOptions = webpack.config.getNormalizedWebpackOptions(this.config);
webpack.config.applyWebpackOptionsDefaults(baseOptions);
/** @type {Compiler} */
const childCompiler = compilation.createChildCompiler(
name,
webpack.util.cleverMerge(
{
path: this.compiler.options.output.path,
filename: `${name}.js`,
environment: baseOptions.output.environment,
iife: false,
},
this.config.output
)
);
childCompiler.name = name;
childCompiler.context = this.compiler.context;
childCompiler.inputFileSystem = this.compiler.inputFileSystem;
childCompiler.outputFileSystem = this.compiler.outputFileSystem;
childCompiler.options.mode = this.compiler.options.mode;
childCompiler.options.externalsPresets = baseOptions.externalsPresets;
delete childCompiler.options.devServer;
// Apply the known Webpack options to the child compiler configuration.
[
"bail",
"target",
"devtool",
"experiments",
"externalPresets",
"mode",
"module",
"name",
"node",
"optimization",
"parallelism",
"performance",
"profile",
"recordsInputPath",
"recordsOutputPath",
"target",
"watch",
"stats",
]
.filter((key) => this.config[key] !== undefined)
.forEach((key) => {
childCompiler.options[key] = webpack.util.cleverMerge(
childCompiler.options[key],
this.config[key]
);
});
new webpack.EntryPlugin(this.compiler.context, srcPath, name).apply(
childCompiler
);
childCompiler.runAsChild((error, entries, childCompilation) => {
if (error) {
cb(error);
} else {
compilation.warnings = [
...compilation.warnings,
...childCompilation.warnings,
];
compilation.errors = [
...compilation.errors,
...childCompilation.errors,
];
for (const [key, value] of childCompilation.entrypoints) {
if (!compilation.entrypoints.has(key)) {
compilation.entrypoints.set(key, value);
}
}
cb();
}
});
}
/**
* @param {JavascriptParser} parser
*/
handleParser(parser, parserOptions) {
if (parserOptions.import !== undefined && !parserOptions.import) {
return;
}
// Exclude dynamically imported dependencies.
parser.hooks.importCall.tap(this.name, () => {
if (parser.state.compilation.name === this.config.name) {
return false;
}
});
}
}
module.exports = { BundleWebpackPlugin };