-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
executable file
·197 lines (190 loc) · 5.9 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
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
const path = require('path');
const fs = require('fs');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const JsDocPlugin = require('jsdoc-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
// const BundleAnalyzer = require('webpack-bundle-analyzer');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const webpack = require('webpack');
const homePageTemplate = '../assets/templates/index.html';
const exclude = /node_modules/;
const JSDocPath = path.resolve(__dirname, 'jsdoc.conf.json');
// const ElevatePortalAssetsPath = path.resolve(__dirname, 'assets');
const PostcssLocalIdentName = '[name]---[local]---[hash:base64:5]';
const lessToJs = require('less-vars-to-js');
const themeVariables = lessToJs(fs.readFileSync(path.join(__dirname, './assets/_ant-theme-vars.less'), 'utf8'));
const commonPlugins = (environment) => {
const isDev = environment === 'development';
// const isProd = environment === 'production';
return [new CleanWebpackPlugin(['dist', 'assets/messages'], { verbose: true }),
new HtmlWebpackPlugin({
hash: isDev,
template: homePageTemplate,
filename: 'index.html',
alwaysWriteToDisk: true,
}),
new CopyWebpackPlugin([
'../assets/images/common/favicon.ico',
]),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(environment),
__ENV__: JSON.stringify(environment),
}),
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
];
};
const devPlugins = environment => commonPlugins(environment).concat([
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin(),
new JsDocPlugin({
conf: JSDocPath,
}),
new MiniCssExtractPlugin({
filename: 'style.css',
allChunks: true,
}),
// new BundleAnalyzer.BundleAnalyzerPlugin(),
new HtmlWebpackHarddiskPlugin(),
]);
const prodPlugins = environment => commonPlugins(environment).concat([
new MiniCssExtractPlugin({
filename: 'style.[chunkhash].css',
allChunks: true,
}),
new webpack.optimize.AggressiveMergingPlugin(),
]);
const getPlugins = environment => (environment === 'development' ? devPlugins(environment) : prodPlugins(environment));
module.exports = (environment) => {
const isDev = environment === 'development';
const isProd = environment === 'production';
return {
context: path.resolve(__dirname, 'src'),
node: false,
entry: {
development: ['./client/index.js', 'webpack-hot-middleware/client'],
production: ['./client/index.js'],
}[environment],
output: {
filename: isDev ? '[name].bundle.js' : '[name].[chunkhash].bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
},
devtool: isDev ? 'eval-source-map' : 'false',
resolve: {
alias: {
Bundles: path.resolve(__dirname, 'src/bundles/'),
Component: path.resolve(__dirname, 'src/component/'),
Libs: path.resolve(__dirname, 'src/libs/'),
Middleware: path.resolve(__dirname, 'src/middleware/'),
AppRedux: path.resolve(__dirname, 'src/redux/'),
APIRequestHelper: path.resolve(__dirname, 'src/libs/APIRequestHelper'),
CSS: path.resolve(__dirname, 'assets/css'),
},
extensions: ['.js', '.jsx', '.less'],
},
plugins: getPlugins(environment),
mode: environment,
optimization: {
minimize: isProd,
namedModules: true,
runtimeChunk: true,
splitChunks: {
name: false,
chunks: 'all',
cacheGroups: {
vendor: {
test: /node_modules/,
chunks: 'all',
name: 'vendor',
priority: -10,
},
},
},
},
module: {
rules: [
{
test: /(\.js$|\.jsx$)/,
exclude,
use: {
loader: 'babel-loader?cacheDirectory',
},
},
{
test: /\.(woff|woff2|eot|eot\?iefix|ttf|otf)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]',
},
},
{
test: /\.(jpe?g|svg|gif|png)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[path][name].[ext]?[hash]',
},
},
{
loader: 'img-loader',
options: {
enabled: process.env.NODE_ENV === 'production',
gifsicle: {
interlaced: false,
},
mozjpeg: {
progressive: true,
arithmetic: false,
},
optipng: false, // disabled
pngquant: {
floyd: 0.5,
speed: 2,
},
svgo: {
plugins: [
{ removeTitle: true },
{ convertPathData: false },
],
},
},
},
],
}, {
test: /\.css$/,
// include: [srcPath],
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: 1,
modules: true,
localIdentName: PostcssLocalIdentName,
},
}, {
loader: 'postcss-loader',
},
],
}, {
test: /\.less$/,
// include: [srcPath, antdLibPath],
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
}, {
loader: 'less-loader',
options: { javascriptEnabled: true, modifyVars: themeVariables },
}],
}, {
test: /\.html$/,
loader: 'raw-loader',
},
],
},
};
};