-
-
Notifications
You must be signed in to change notification settings - Fork 377
/
run-webpack.js
184 lines (144 loc) · 5.19 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
import ip from 'ip';
import { resolve } from 'path';
import { writeFile } from 'fs.promised';
import webpack from 'webpack';
import chalk from 'chalk';
import getPort from 'get-port';
import clearConsole from 'console-clear';
import DevServer from 'webpack-dev-server';
import clientConfig from './webpack-client-config';
import serverConfig from './webpack-server-config';
import transformConfig from './transform-config';
import { error, isDir, warn } from '../../util';
export default function (watch=false, env, onprogress) {
env.isProd = env.production; // shorthand
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
let fn = watch ? devBuild : prodBuild;
return fn(env, onprogress); // AsyncFunctioon
}
async function devBuild(env, onprogress) {
let config = clientConfig(env);
await transformConfig(env, config);
let userPort = parseInt(process.env.PORT || config.devServer.port, 10) || 8080;
let port = await getPort(userPort);
let compiler = webpack(config);
return await new Promise((_, rej) => {
compiler.plugin('emit', (compilation, callback) => {
var missingDeps = compilation.missingDependencies;
var nodeModulesPath = resolve(__dirname, '../../../node_modules');
// ...tell webpack to watch node_modules recursively until they appear.
if (missingDeps.some(file => file.indexOf(nodeModulesPath) !== -1)) {
compilation.contextDependencies.push(nodeModulesPath);
}
callback();
});
compiler.plugin('done', 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') host = 'localhost';
let serverAddr = `${protocol}://${host}:${chalk.bold(port)}`;
let localIpAddr = `${protocol}://${ip.address()}:${chalk.bold(port)}`;
clearConsole();
if (stats.hasErrors()) {
process.stdout.write(chalk.red('\Build failed!\n\n'));
} else {
process.stdout.write(chalk.green('Compiled successfully!\n\n'));
if (userPort !== port) {
process.stdout.write(`Port ${chalk.bold(userPort)} is in use, using ${chalk.bold(port)} instead\n\n`);
}
process.stdout.write('You can view the application in browser.\n\n');
process.stdout.write(`${chalk.bold('Local:')} ${serverAddr}\n`);
process.stdout.write(`${chalk.bold('On Your Network:')} ${localIpAddr}\n`);
}
if (onprogress) onprogress(stats);
});
compiler.plugin('failed', rej);
new DevServer(compiler, config.devServer).listen(port);
});
}
async function prodBuild(env) {
let config = clientConfig(env);
await transformConfig(env, config);
let serverCompiler, clientCompiler=webpack(config);
if (env.prerender) {
let ssrConfig = serverConfig(env);
await transformConfig(env, ssrConfig, true);
serverCompiler = webpack(ssrConfig);
await runCompiler(serverCompiler);
}
let stats = await runCompiler(clientCompiler);
// Timeout for plugins that work on `after-emit` event of webpack
await new Promise(r => setTimeout(r, 20));
return stats;
}
const runCompiler = compiler => new Promise((res, rej) => {
compiler.run((err, stats) => {
if (err || stats.hasErrors()) {
showStats(stats);
rej(chalk.red('Build failed!'));
}
res(stats);
});
});
export function showStats(stats) {
let info = stats.toJson('errors-only');
if (stats.hasErrors()) {
info.errors.map(stripLoaderPrefix).forEach(msg => error(msg));
}
if (stats.hasWarnings()) {
info.warnings.map(stripLoaderPrefix).forEach(msg => warn(msg));
}
return stats;
}
export function writeJsonStats(stats) {
let outputPath = resolve(process.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.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');
});
}
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') {
return str.replace(/(^|\b|@)(\.\/~|\.{0,2}\/[^\s]+\/node_modules)\/\w+-loader(\/[^?!]+)?(\?\?[\w_.-]+|\?({[\s\S]*?})?)?!/g, '');
}
return str;
}
function stripLoaderFromModuleNames(m) {
for (let key in m) {
if (m.hasOwnProperty(key) && m[key]!=null && ~keysToNormalize.indexOf(key)) {
m[key] = stripLoaderPrefix(m[key]);
}
}
if (m.reasons) {
m.reasons.forEach(stripLoaderFromModuleNames);
}
return m;
}