-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
190 lines (165 loc) · 6.14 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const deepcopy = require('deepcopy');
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
const MultiEntryPlugin = require('webpack/lib/MultiEntryPlugin');
const JsonpTemplatePlugin = require('webpack/lib/web/JsonpTemplatePlugin');
const SplitChunksPlugin = require('webpack/lib/optimize/SplitChunksPlugin');
const RuntimeChunkPlugin = require('webpack/lib/optimize/RuntimeChunkPlugin');
const {
makeESMPresetOptions,
getBabelLoaderOptions,
getBabelLoader,
} = require('./babel-utils');
const PLUGIN_NAME = 'BabelEsmPlugin';
const FILENAME = '[name].es6.js';
const CHUNK_FILENAME = '[id].es6.js';
class BabelEsmPlugin {
constructor(options) {
this.options_ = Object.assign(
{
filename: FILENAME,
chunkFilename: CHUNK_FILENAME,
excludedPlugins: [PLUGIN_NAME],
additionalPlugins: [],
},
options,
);
}
apply(compiler) {
compiler.hooks.make.tapAsync(PLUGIN_NAME, async (compilation, callback) => {
const outputOptions = deepcopy(compiler.options);
this.babelLoaderConfigOptions_ = getBabelLoaderOptions(outputOptions);
this.newConfigOptions_ = makeESMPresetOptions(
this.babelLoaderConfigOptions_,
);
outputOptions.output.filename = this.options_.filename;
outputOptions.output.chunkFilename = this.options_.chunkFilename;
let plugins = (compiler.options.plugins || []).filter(
c => this.options_.excludedPlugins.indexOf(c.constructor.name) < 0,
);
// Add the additionalPlugins
plugins = plugins.concat(this.options_.additionalPlugins);
/**
* We are deliberatly not passing plugins in createChildCompiler.
* All webpack does with plugins is to call `apply` method on them
* with the childCompiler.
* But by then we haven't given childCompiler a fileSystem or other options
* which a few plugins might expect while execution the apply method.
* We do call the `apply` method of all plugins by ourselves later in the code
*/
const childCompiler = compilation.createChildCompiler(
PLUGIN_NAME,
outputOptions.output,
);
childCompiler.context = compiler.context;
childCompiler.inputFileSystem = compiler.inputFileSystem;
childCompiler.outputFileSystem = compiler.outputFileSystem;
// Call the `apply` method of all plugins by ourselves.
if (Array.isArray(plugins)) {
for (const plugin of plugins) {
plugin.apply(childCompiler);
}
}
// All plugin work is done, call the lifecycle hook.
childCompiler.hooks.afterPlugins.call(childCompiler);
let entries = compiler.options.entry;
if (typeof entries === 'function') {
entries = await entries();
}
if (typeof entries === 'string') {
entries = {
index: entries,
};
}
Object.keys(entries).forEach(entry => {
const entryFiles = entries[entry];
if (Array.isArray(entryFiles)) {
new MultiEntryPlugin(compiler.context, entryFiles, entry).apply(
childCompiler,
);
} else {
new SingleEntryPlugin(compiler.context, entryFiles, entry).apply(
childCompiler,
);
}
});
// Convert entry chunk to entry file
new JsonpTemplatePlugin().apply(childCompiler);
if (compiler.options.optimization) {
if (compiler.options.optimization.splitChunks) {
new SplitChunksPlugin(
Object.assign({}, compiler.options.optimization.splitChunks),
).apply(childCompiler);
}
if (compiler.options.optimization.runtimeChunk) {
new RuntimeChunkPlugin(
Object.assign({}, compiler.options.optimization.runtimeChunk),
).apply(childCompiler);
}
}
compilation.hooks.additionalAssets.tapAsync(
PLUGIN_NAME,
childProcessDone => {
let babelLoader;
childCompiler.options.module.rules.forEach((rule, index) => {
babelLoader = getBabelLoader(childCompiler.options);
babelLoader.options = this.newConfigOptions_;
});
this.options_.beforeStartExecution &&
this.options_.beforeStartExecution(
plugins,
(babelLoader || {}).options,
);
/*
* Copy over the parent compilation hash, see issue#15.
*/
childCompiler.hooks.make.tapAsync(
PLUGIN_NAME,
(childCompilation, callback) => {
childCompilation.hooks.afterHash.tap(PLUGIN_NAME, () => {
childCompilation.hash = compilation.hash;
childCompilation.fullHash = compilation.fullHash;
});
callback();
},
);
childCompiler.runAsChild((err, entries, childCompilation) => {
if (err) {
return childProcessDone(err);
}
if (childCompilation.errors.length > 0) {
return childProcessDone(childCompilation.errors[0]);
}
compilation.hooks.afterOptimizeAssets.tap(PLUGIN_NAME, () => {
compilation.assets = Object.assign(
childCompilation.assets,
compilation.assets,
);
compilation.namedChunkGroups = Object.assign(
childCompilation.namedChunkGroups,
compilation.namedChunkGroups,
);
const childChunkFileMap = childCompilation.chunks.reduce(
(chunkMap, chunk) => {
chunkMap[chunk.name] = chunk.files;
return chunkMap;
},
{},
);
compilation.chunks.forEach(chunk => {
const childChunkFiles = childChunkFileMap[chunk.name];
if (childChunkFiles) {
chunk.files.push(
...childChunkFiles.filter(v => !chunk.files.includes(v)),
);
}
});
});
childProcessDone();
});
},
);
callback();
});
}
}
module.exports = BabelEsmPlugin;