-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.babel.js
executable file
·277 lines (262 loc) · 6.73 KB
/
webpack.config.babel.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import webpack from 'webpack';
import path from 'path';
import Merge from 'webpack-merge';
import StatsPlugin from 'stats-webpack-plugin';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import HTMLWebpackPlugin from 'html-webpack-plugin';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import CompressionPlugin from 'compression-webpack-plugin';
import './postcss.config';
// define environment constants
const ENV = (process.env.ENV || 'development');
const NODE_ENV = (process.env.NODE_ENV || 'development');
// Optimize for production?
const IS_PRODUCTION = (NODE_ENV === 'production');
console.log(`Running webpack optimized for ${NODE_ENV} using the ${ENV} configs.`);
// Static vendor assets for which one can expect
// minimal and a slow rate of change:
const VENDOR_LIBS = [
'react',
'react-dom',
'react-router-dom',
'react-redux',
'react-document-meta',
];
const CLIENT_PATH = path.resolve(__dirname, 'client/app');
// Webpack config for both production and development environments
// ====================
const BASE_CONFIG = {
entry:
{
bundle: ['babel-polyfill', CLIENT_PATH],
vendor: VENDOR_LIBS,
},
output:
{
path: path.join(__dirname, 'public'),
publicPath: '/',
},
module:
{
rules: [
{
test: /\.jsx?$/i,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback:
{
loader: 'style-loader',
},
use: [
{
loader: 'css-loader',
options:
{
modules: true,
sourceMap: false,
minimize: true,
importLoaders: 1,
localIdentName: '[name]--[local]--[hash:base64:8]',
},
},
{
loader: 'postcss-loader',
},
],
}),
},
{
test: /\.pdf$/i,
use: 'file-loader?name=/docs/[name].[ext]',
},
{
test: /\.(gif|png|jpg|jpe?g|webp)$/i,
include: path.resolve(__dirname, 'client/assets/images'),
loaders: [
'file-loader',
{
loader: 'image-webpack-loader',
options:
{
bypassOnDebug: true,
gifsicle:
{
interlaced: false,
},
optipng:
{
optimizationLevel: 7,
},
pngquant:
{
quality: '65-80',
speed: 4,
},
mozjpeg:
{
progressive: true,
quality: 65,
},
// Specifying webp here will create a WEBP version of your JPG/PNG images
webp:
{
quality: 75,
},
},
},
],
},
{
test: /\.(ttf|otf|eot|woff(2)?)(\?[a-z0-9]+)?$/,
loader: 'url-loader',
},
{
test: /\.svg$/,
exclude: /node_modules/,
loader: 'svg-react-loader',
query:
{
classIdPrefix: '[name]-[hash:8]__',
},
}],
},
plugins: [
new CopyWebpackPlugin([
{
context: 'client',
from: 'assets/images',
to: 'images',
},
{
context: 'client',
from: 'assets/uploads',
to: 'uploads',
},
{
context: 'client',
from: 'assets/pdfs',
to: 'pdfs',
},
{
context: 'client',
from: 'assets/fonts',
to: 'fonts',
},
]),
new webpack.DefinePlugin({
// Dynamically access local environment variables based on the environment
ENV_CONFIG: JSON.stringify(require('./config/' + ENV + '.config')),
}),
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor', 'manifest'],
minChunks: Infinity,
}),
new HTMLWebpackPlugin({
template: 'client/index.html',
}),
new webpack.NamedModulesPlugin(),
new webpack.LoaderOptionsPlugin({
debug: true,
}),
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/), // reduce moment package size
],
devtool: `${IS_PRODUCTION ? 'inline' : 'cheap-eval'}-source-map`,
resolve:
{
alias:
{
assets: path.resolve(__dirname, 'client/assets/'),
images: path.resolve(__dirname, 'client/images/'),
client: path.resolve(__dirname, 'client/'),
components: path.resolve(__dirname, 'client/components/'),
containers: path.resolve(__dirname, 'client/containers/'),
layout: path.resolve(__dirname, 'client/main/layout/'),
routes: path.resolve(__dirname, 'client/main/routes/'),
store: path.resolve(__dirname, 'client/store/'),
styles: path.resolve(__dirname, 'client/styles/'),
utils: path.resolve(__dirname, 'client/utils/'),
lib: path.resolve(__dirname, 'client/lib/'),
shop: path.resolve(__dirname, 'client/containers/shop/'),
},
extensions: ['.js', '.jsx', '.css'],
}
};
// Webpack plugins unique to the production build:
const PROD_PLUGINS = [
new webpack.DefinePlugin({
'process.env':
{
NODE_ENV: JSON.stringify('production'),
},
}),
new ExtractTextPlugin('[name].min.[contenthash].css'),
new webpack.optimize.UglifyJsPlugin({
compress:
{
screw_ie8: true,
warnings: false,
},
output:
{
comments: false,
},
}),
new webpack.optimize.AggressiveMergingPlugin(),
new CompressionPlugin({
asset: '[path].gz[query]',
algorithm: 'gzip',
test: /\.js$|\.css$|\.html$/,
threshold: 10240,
minRatio: 0.8,
}),
new StatsPlugin('stats.prod.json', {
chunkModules: true,
}),
];
// Webpack field-value pairs re: webpack-dev-server:
const PROD_CONFIG = {
output:
{
filename: '[name].min.[hash].js',
},
};
// Webpack plugins unique to the development build:
const DEV_PLUGINS = [
new ExtractTextPlugin('[name].[contenthash].css'),
new webpack.HotModuleReplacementPlugin(),
new StatsPlugin('stats.dev.json', {
chunkModules: true,
}),
];
// Webpack field-value pairs re: webpack-dev-server:
const DEV_CONFIG = {
output:
{
filename: '[name].[hash].js',
},
devServer:
{
contentBase: path.join(__dirname, 'public'),
open: true,
host: 'localhost',
hot: true,
inline: true,
noInfo: false,
port: 3000,
historyApiFallback: true,
},
};
// Final Webpack configuration object constructed
// conditionally according to the NODE_ENV value:
const AGGREGATE_CONFIG = IS_PRODUCTION ?
Merge(BASE_CONFIG, PROD_CONFIG, {
plugins: PROD_PLUGINS,
}) :
Merge(BASE_CONFIG, DEV_CONFIG, {
plugins: DEV_PLUGINS,
});
export default AGGREGATE_CONFIG;