-
Notifications
You must be signed in to change notification settings - Fork 33
/
webpack.js
118 lines (100 loc) · 2.65 KB
/
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
// @flow weak
/* eslint-disable no-console */
import minimist from 'minimist';
import path from 'path';
import WebpackDevServer from 'webpack-dev-server';
import webpack from 'webpack';
import fse from 'fs-extra';
import ProgressPlugin from 'webpack/lib/ProgressPlugin';
import webpackConfig from './webpack.config';
const PORT_DEV_WEBPACK = 8000;
const PORT_DEV_EXPRESS = 8080;
const argv = minimist(process.argv.slice(2));
let config;
try {
// eslint-disable-next-line import/no-dynamic-require, global-require
config = require(`./config/${argv.config}`);
} catch (err) {
config = {
platform: '',
};
}
if (argv.dev === true) {
const compiler = webpack(
webpackConfig({
configName: argv.config,
config,
port: PORT_DEV_WEBPACK,
outputPath: __dirname,
}),
);
compiler.apply(
new ProgressPlugin((percentage, msg) => {
console.log(`${Math.floor(percentage * 100)}%`, msg);
}),
);
const server = new WebpackDevServer(compiler, {
// webpack-dev-server options
hot: true,
historyApiFallback: true,
// webpack-dev-middleware options
stats: {
// Remove built modules information.
modules: false,
// Remove built modules information to chunk information.
chunkModules: false,
colors: true,
},
disableHostCheck: true, // For security checks, no need here.
contentBase: './server/public',
proxy: {
'**': `http://local.splitme.net:${PORT_DEV_EXPRESS}`,
},
});
server.listen(PORT_DEV_WEBPACK, undefined, () => {});
} else {
let outputPath;
if (config.platform === 'android' || config.platform === 'ios') {
outputPath = path.join(__dirname, 'cordova/www');
} else if (config.platform === 'browser') {
outputPath = path.join(__dirname, 'server/static');
} else {
outputPath = path.join(__dirname, 'server/local');
}
fse.emptyDirSync(`${outputPath}`);
const compiler = webpack(
webpackConfig({
configName: argv.config,
config,
outputPath,
}),
);
compiler.apply(
new ProgressPlugin((percentage, msg) => {
console.log(`${Math.floor(percentage * 100)}%`, msg);
}),
);
compiler.run((err, stats) => {
if (err) {
throw new Error(err);
}
console.log(
stats.toString({
colors: true,
hash: false,
timings: false,
assets: true,
chunks: false,
chunkModules: false,
modules: false,
children: true,
}),
);
if (stats.hasErrors()) {
throw new Error('Webpack failed');
}
if (config.platform === 'browser') {
fse.copySync(`${outputPath}/sw.js`, 'server/public/sw.js');
}
});
}