-
-
Notifications
You must be signed in to change notification settings - Fork 376
/
run-webpack.js
250 lines (214 loc) · 6.54 KB
/
run-webpack.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const ip = require('ip');
const webpack = require('webpack');
const { resolve } = require('path');
const clear = require('console-clear');
const { writeFile } = require('fs').promises;
const { bold, red, green, magenta } = require('kleur');
const DevServer = require('webpack-dev-server');
const clientConfig = require('./webpack-client-config');
const serverConfig = require('./webpack-server-config');
const transformConfig = require('./transform-config');
const { error, isDir, warn } = require('../../util');
async function devBuild(env) {
let config = await clientConfig(env);
await transformConfig(env, config);
let compiler = webpack(config);
return new Promise((res, rej) => {
compiler.hooks.beforeCompile.tap('CliDevPlugin', () => {
if (env['clear']) clear(true);
});
compiler.hooks.done.tap('CliDevPlugin', stats => {
let devServer = config.devServer;
let protocol = process.env.HTTPS || devServer.https ? 'https' : 'http';
let host = process.env.HOST || devServer.host || 'localhost';
if (host === '0.0.0.0' && process.platform === 'win32') {
host = 'localhost';
}
let serverAddr = `${protocol}://${host}:${bold(env.port)}`;
let localIpAddr = `${protocol}://${ip.address()}:${bold(env.port)}`;
if (stats.hasErrors()) {
process.stdout.write(red('Build failed!\n\n'));
} else {
process.stdout.write(green('Compiled successfully!\n\n'));
process.stdout.write('You can view the application in browser.\n\n');
process.stdout.write(`${bold('Local:')} ${serverAddr}\n`);
process.stdout.write(`${bold('On Your Network:')} ${localIpAddr}\n`);
}
showStats(stats, false);
});
compiler.hooks.failed.tap('CliDevPlugin', rej);
let server = new DevServer(config.devServer, compiler);
server.start();
res(server);
});
}
async function prodBuild(env) {
let config = await clientConfig(env);
await transformConfig(env, config);
if (env.prerender) {
let ssrConfig = serverConfig(env);
await transformConfig(env, ssrConfig, true);
let serverCompiler = webpack(ssrConfig);
await runCompiler(serverCompiler);
}
let clientCompiler = webpack(config);
try {
let stats = await runCompiler(clientCompiler);
// Timeout for plugins that work on `after-emit` event of webpack
await new Promise(r => setTimeout(r, 20));
return showStats(stats, true);
} catch (err) {
// eslint-disable-next-line
console.log(err);
}
}
function runCompiler(compiler) {
return new Promise((res, rej) => {
compiler.run((err, stats) => {
showStats(stats, true);
if (err || (stats && stats.hasErrors())) {
rej(`${red('\n\nBuild failed! \n\n')} ${err || ''}`);
}
res(stats);
});
});
}
function showStats(stats, isProd) {
if (stats) {
if (stats.hasErrors()) {
allFields(stats, 'errors')
.map(stripLoaderPrefix)
.forEach(msg => error(msg, isProd ? 1 : 0));
}
if (stats.hasWarnings()) {
allFields(stats, 'warnings')
.map(stripLoaderPrefix)
.forEach(msg => {
if (
msg.match(
/Conflict: Multiple assets emit different content to the same filename .*\.(css|map)/
)
) {
/**
* This particular warning is expected due to `babel-esm-plugin`.
* This can be removed when upgrading to webpack5 with https://webpack.js.org/configuration/output/#outputcomparebeforeemit
*/
return;
}
warn(msg);
});
}
}
return stats;
}
function writeJsonStats(cwd, stats) {
let outputPath = resolve(cwd, 'stats.json');
let jsonStats = stats.toJson({
json: true,
chunkModules: true,
source: false,
});
function strip(stats) {
stats.modules.forEach(stripLoaderFromModuleNames);
stats.chunks.forEach(c => {
(
c.modules ||
(c.mapModules != null ? c.mapModules(Object) : c.getModules())
).forEach(stripLoaderFromModuleNames);
});
if (stats.children) stats.children.forEach(strip);
}
strip(jsonStats);
return writeFile(outputPath, JSON.stringify(jsonStats)).then(() => {
process.stdout.write('\nWebpack output stats generated.\n\n');
process.stdout.write('You can upload your stats.json to:\n');
process.stdout.write(
'- https://chrisbateman.github.io/webpack-visualizer/\n'
);
process.stdout.write('- https://webpack.github.io/analyse/\n');
});
}
function allFields(stats, field, fields = [], name = null) {
const info = stats.toJson({
errors: true,
warnings: false,
errorDetails: false,
});
const addCompilerPrefix = msg =>
name ? bold(magenta(name + ': ')) + msg : msg;
if (field === 'errors' && stats.hasErrors()) {
fields = fields.concat(info.errors.map(addCompilerPrefix));
}
if (field === 'warnings' && stats.hasWarnings()) {
fields = fields.concat(info.warnings.map(addCompilerPrefix));
}
if (stats.compilation.children) {
stats.compilation.children.forEach((child, index) => {
const name = child.name || `Child Compiler ${index + 1}`;
const stats = child.getStats();
fields = allFields(stats, field, fields, name);
});
}
return fields;
}
const keysToNormalize = [
'issuer',
'issuerName',
'identifier',
'name',
'module',
'moduleName',
'moduleIdentifier',
];
/** Removes all loaders from any resource identifiers found in a string */
function stripLoaderPrefix(str) {
if (typeof str === 'string') {
str = str.replace(
/(?:(\()|(^|\b|@))(\.\/~|\.{0,2}\/(?:[^\s]+\/)?node_modules)\/\w+-loader(\/[^?!]+)?(\?\?[\w_.-]+|\?({[\s\S]*?})?)?!/g,
'$1'
);
str = str.replace(/(\.?\.?(?:\/[^/ ]+)+)\s+\(\1\)/g, '$1');
str = replaceAll(str, process.cwd(), '.');
return str;
}
return str;
}
// https://gist.github.com/developit/1a40a6fee65361d1182aaa22ab8c334c
function replaceAll(str, find, replace) {
let s = '',
index,
next;
while (~(next = str.indexOf(find, index))) {
s += str.substring(index, next) + replace;
index = next + find.length;
}
return s + str.substring(index);
}
function stripLoaderFromModuleNames(m) {
for (let key in m) {
if (
Object.prototype.hasOwnProperty.call(m, key) &&
m[key] != null &&
~keysToNormalize.indexOf(key)
) {
m[key] = stripLoaderPrefix(m[key]);
}
}
if (m.reasons) {
m.reasons.forEach(stripLoaderFromModuleNames);
}
return m;
}
module.exports = function (env, watch = false) {
env.isProd = env.production; // shorthand
env.isWatch = !!watch; // use HMR?
env.cwd = resolve(env.cwd || process.cwd());
// env.src='src' via `build` default
let src = resolve(env.cwd, env.src);
env.src = isDir(src) ? src : env.cwd;
// attach sourcing helper
env.source = dir => resolve(env.src, dir);
// determine build-type to run
return (watch ? devBuild : prodBuild)(env);
};
module.exports.writeJsonStats = writeJsonStats;