/
webpack.config.js
95 lines (87 loc) · 2.36 KB
/
webpack.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
const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const webpack = require('webpack');
const OfflinePlugin = require('offline-plugin');
module.exports = env => {
let htmlWebpackPluginOpts = {
inject: true,
template: path.join(__dirname, 'src', 'index.html')
};
if (env.option === 'production') {
htmlWebpackPluginOpts = Object.assign(htmlWebpackPluginOpts, {
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
}
});
}
let plugins = [
new HtmlWebpackPlugin(htmlWebpackPluginOpts),
new webpack.DefinePlugin({
'process.env': { NODE_ENV: JSON.stringify(env.option) }
})
];
if (env.option === 'production') {
plugins = plugins.concat([
new webpack.optimize.UglifyJsPlugin(),
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor', 'manifest']
}),
new OfflinePlugin()
]);
}
let entry = { app: [path.join(__dirname, 'src', 'index.js')] };
if (env.option === 'production') {
entry = Object.assign(entry, {
vendor: [
'immutable',
'offline-plugin/runtime',
'react-dom',
'react-redux',
'react',
'redux-actions',
'redux-thunk',
'redux',
'styled-components'
]
});
}
let webpackConfig = {
entry,
output: {
path: path.join(__dirname, `build`),
filename: `[name].[chunkhash:8].js`,
pathinfo: env.option === 'development'
},
module: {
rules: [
{ test: /\.js$/, exclude: /node_modules/, use: 'babel-loader' },
{ test: /\.css$/, use: ['style-loader', 'css-loader'] },
{ test: /\.(jpe?g|png|gif)$/i, use: 'base64-image-loader' },
{ test: /\.svg$/i, loaders: ['file-loader'] }
]
},
plugins
};
if (env.option === 'development') {
webpackConfig = Object.assign(webpackConfig, {
devServer: {
contentBase: 'public/',
inline: true,
open: true,
noInfo: false,
quiet: false,
overlay: { errors: true, warnings: true }
}
});
}
return webpackConfig;
};