-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
129 lines (125 loc) · 3.26 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
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
const {resolve} = require('path');
const {
DefinePlugin,
NamedModulesPlugin,
HashedModuleIdsPlugin,
HotModuleReplacementPlugin,
NoEmitOnErrorsPlugin,
optimize: {
CommonsChunkPlugin,
ModuleConcatenationPlugin,
},
} = require('webpack');
const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer');
const CleanPlugin = require('clean-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const HTMLPlugin = require('html-webpack-plugin');
const BabelMinifyPlugin = require('babel-minify-webpack-plugin');
const OptimizeJSPlugin = require('optimize-js-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const OfflinePlugin = require('offline-plugin');
const {extract} = ExtractTextPlugin;
const src = 'src';
const dest = 'app';
const clean = [dest];
const copy = [
{
from: 'static',
},
];
module.exports = ({dev} = {}) => {
const env = dev ? 'development' : 'production';
return {
devtool: dev ? 'eval-source-map' : 'hidden-source-map',
entry: [
...dev ? ['react-hot-loader/patch'] : [],
`./${src}/index.jsx`,
],
output: {
path: resolve(__dirname, dest),
filename: `[name]${dev ? '' : '.[chunkhash]'}.js`,
},
module: {
rules: [
{
test: /\.jsx?$/,
include: resolve(__dirname, src),
loader: 'babel-loader',
options: {
cacheDirectory: dev,
forceEnv: env,
},
},
{
test: /\.css$/,
include: resolve(__dirname, src),
use: extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
sourceMap: true,
modules: true,
localIdentName: '[path][name]__[local]--[hash:base64:5]',
},
},
],
}),
},
],
},
plugins: [
new CleanPlugin(clean),
new CopyPlugin(copy),
new HTMLPlugin({
template: `${src}/template.ejs`,
inject: false,
minify: {
removeComments: true,
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
},
}),
new ExtractTextPlugin({
filename: '[name].[contenthash].css',
allChunks: true,
disable: dev,
}),
new DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(env),
}),
new CommonsChunkPlugin({
name: 'vendor',
minChunks(module) {
return module.context && module.context.includes('node_modules');
},
}),
new CommonsChunkPlugin({
name: 'manifest',
minChunks: Infinity,
}),
...dev
? [
new NamedModulesPlugin(),
new HotModuleReplacementPlugin(),
new BundleAnalyzerPlugin({
analyzerHost: '0.0.0.0',
openAnalyzer: false,
}),
]
: [
new HashedModuleIdsPlugin(),
new NoEmitOnErrorsPlugin(),
new ModuleConcatenationPlugin(),
new BabelMinifyPlugin(),
new OptimizeJSPlugin(),
new OfflinePlugin(),
],
],
resolve: {
extensions: ['.js', '.jsx', '.json'],
},
};
};