-
-
Notifications
You must be signed in to change notification settings - Fork 375
/
webpack-client-config.js
202 lines (187 loc) · 4.55 KB
/
webpack-client-config.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
import webpack from 'webpack';
import { resolve } from 'path';
import { existsSync } from 'fs';
import merge from 'webpack-merge';
import { filter } from 'minimatch';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import SWPrecacheWebpackPlugin from 'sw-precache-webpack-plugin';
import RenderHTMLPlugin from './render-html-plugin';
import PushManifestPlugin from './push-manifest';
import baseConfig from './webpack-base-config';
function clientConfig(env) {
const { isProd, source, src /*, port? */ } = env;
let entry = {
bundle: resolve(__dirname, './../entry'),
polyfills: resolve(__dirname, './polyfills')
};
if (!isProd) {
entry.bundle = [
entry.bundle,
'webpack-dev-server/client',
'webpack/hot/dev-server'
];
}
return {
entry: entry,
output: {
path: env.dest,
publicPath: '/',
filename: isProd ? '[name].[chunkhash:5].js' : '[name].js',
chunkFilename: '[name].chunk.[chunkhash:5].js'
},
resolveLoader: {
alias: {
async: resolve(__dirname, './async-component-loader')
}
},
// automatic async components :)
module: {
loaders: [
{
test: /\.jsx?$/,
include: [
filter(source('routes')+'/{*.js,*/index.js}'),
filter(source('components')+'/{routes,async}/{*.js,*/index.js}')
],
loader: resolve(__dirname, './async-component-loader'),
options: {
name(filename) {
let relative = filename.replace(src, '');
let isRoute = filename.indexOf('/routes/') >= 0;
return isRoute ? 'route-' + relative.replace(/(^\/(routes|components\/(routes|async))\/|(\/index)?\.js$)/g, '') : false;
},
formatName(filename) {
let relative = filename.replace(source('.'), '');
// strip out context dir & any file/ext suffix
return relative.replace(/(^\/(routes|components\/(routes|async))\/|(\/index)?\.js$)/g, '');
}
}
}
]
},
plugins: [
...RenderHTMLPlugin(env),
new PushManifestPlugin(),
new CopyWebpackPlugin([
...(
existsSync(source('manifest.json'))
? [{ from:'manifest.json' }]
: [{
from: resolve(__dirname, '../../resources/manifest.json'),
to: 'manifest.json'
}, {
from: resolve(__dirname, '../../resources/icon.png'),
to: 'assets/icon.png'
}]
),
// copy any static files
existsSync(source('assets')) && { from:'assets', to:'assets' }
].filter(Boolean))
]
};
}
function isProd(config) {
let limit = 200 * 1000; // 200kb
return {
performance: {
hints: 'warning',
maxAssetSize: limit,
maxEntrypointSize: limit,
...config.pkg.performance
},
plugins: [
new webpack.optimize.UglifyJsPlugin({
output: { comments:false },
mangle: true,
sourceMap: true,
compress: {
properties: true,
keep_fargs: false,
pure_getters: true,
collapse_vars: true,
warnings: false,
screw_ie8: true,
sequences: true,
dead_code: true,
drop_debugger: true,
comparisons: true,
conditionals: true,
evaluate: true,
booleans: true,
loops: true,
unused: true,
hoist_funs: true,
if_return: true,
join_vars: true,
cascade: true,
drop_console: false,
pure_funcs: [
'classCallCheck',
'_classCallCheck',
'_possibleConstructorReturn',
'Object.freeze',
'invariant',
'warning'
]
}
}),
new SWPrecacheWebpackPlugin({
filename: 'sw.js',
navigateFallback: 'index.html',
navigateFallbackWhitelist: [/^(?!\/__).*/],
minify: true,
stripPrefix: config.cwd,
staticFileGlobsIgnorePatterns: [
/polyfills(\..*)?\.js$/,
/\.map$/,
/push-manifest\.json$/,
/.DS_Store/
]
}),
new webpack.DefinePlugin({
'process.env.ADD_SW': config.serviceWorker
}),
]
};
}
function isDev(config) {
const { cwd, src } = config;
return {
plugins: [
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin()
],
devServer: {
inline: true,
hot:true,
compress: true,
publicPath: '/',
contentBase: src,
https: config.https,
port: process.env.PORT || config.port || 8080,
host: process.env.HOST || config.host || '0.0.0.0',
// setup(app) {
// app.use(middleware);
// },
disableHostCheck: true,
historyApiFallback: true,
quiet: true,
clientLogLevel: 'none',
overlay: false,
stats: 'minimal',
watchOptions: {
ignored: [
resolve(cwd, 'build'),
resolve(cwd, 'node_modules')
]
}
}
};
}
export default function (env) {
return merge(
baseConfig(env),
clientConfig(env),
(env.isProd ? isProd : isDev)(env)
);
}