-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
285 lines (270 loc) · 7.29 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
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
278
279
280
281
282
283
284
285
'use strict';
/**
* Load dependencies
*/
const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const SvgSpriteLoaderPlugin = require('svg-sprite-loader/plugin');
const autoprefixer = require('autoprefixer');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const LodashPlugin = require('lodash-webpack-plugin');
const config = require('./lib/config');
/**
* Private vars and fn
* to customise config based on env
*/
const supportedBrowsers = ['last 3 versions', 'Android >= 4.4'];
const isProduction = config.env === 'production';
const extractCSS = new ExtractTextPlugin({
filename: '[name].css',
allChunks: true,
});
/**
* Main webpack config
*/
let webpackCfg = {
context: config.root,
devtool: isProduction ? 'source-map' : 'cheap-module-source-map',
entry: {
// vendor: ['react'],
app: extendEntrySources([path.join(config.root, 'app', 'client')]),
},
resolve: config.webpack.resolve,
stats: {
colors: true,
reasons: true,
children: false,
},
output: {
path: path.join(config.root, 'public', 'assets'),
publicPath: config.client.publicPath + 'assets/',
filename: '[name].js',
chunkFilename: '[id].[hash].js',
},
performance: {
hints: false,
},
node: {
Buffer: false, // prevent axios 0.16.1 from bundling buffer
},
};
/**
* Webpack devServer
*/
webpackCfg.devServer = {
contentBase: webpackCfg.output.path,
publicPath: webpackCfg.output.publicPath,
hot: true,
inline: true,
lazy: false,
logLevel: 'silent',
};
/**
* Webpack modules
*/
webpackCfg.module = {
rules: [
{
// JS/JSX loader + hot reload
test: /\.jsx?$/,
include: path.join(config.root, 'app'),
use: [
{
loader: 'babel-loader',
options: {
cacheDirectory: true,
presets: [
[
'env',
{
targets: { browsers: supportedBrowsers },
loose: true,
modules: false,
},
],
'react',
'stage-2',
],
plugins: isProduction
? [
'lodash',
['transform-react-remove-prop-types', { removeImport: true }],
'transform-react-constant-elements',
'transform-react-inline-elements',
]
: [
[
'react-transform',
{
transforms: [
{
transform: 'react-transform-hmr',
imports: ['react'],
locals: ['module'],
},
],
},
],
],
},
},
],
},
{
// CSS/SASS loader + autoprefixer
test: /\.scss$/,
include: path.join(config.root, 'app'),
use: extendCSSLoaders([
{
loader: 'css-loader',
options: {
minimize: false,
sourceMap: true,
},
},
{
loader: 'postcss-loader',
options: {
plugins: () => [autoprefixer({ browsers: supportedBrowsers })],
sourceMap: true,
},
},
{
loader: 'sass-loader',
options: {
outputStyle: 'expanded',
sourceMap: !isProduction, // breaks build sass-loader#309
},
},
]),
},
{
// Image/SVG loader + base64 encode + optimisation
test: /\.(jpe?g|png|gif|svg)$/i,
exclude: /assets\/icons/,
use: [
{
loader: 'url-loader',
options: {
limit: 8192,
name: '[name].[hash:6].[ext]',
},
},
{
loader: 'image-webpack-loader',
options: {
bypassOnDebug: true,
progressive: true,
},
},
],
},
{
// SVG Icons sprite loader
test: /\.svg$/,
include: [path.join(config.root, 'app', 'assets', 'icons')],
use: [
{
loader: 'svg-sprite-loader',
options: {
symbolId: 'i-[name]',
extract: isProduction,
spriteFilename: 'icons.svg',
},
},
{
loader: 'image-webpack-loader',
},
],
},
{
// Generic file loader
test: /\.(eot|ttf|woff2?|swf|mp[34]|wav)$/i,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[hash:6].[ext]',
},
},
],
},
],
};
/**
* Webpack plugins
*/
webpackCfg.plugins = extendPlugins([
new webpack.LoaderOptionsPlugin({
debug: !isProduction,
}),
// Variable replacement to bridge client/server side globals
new webpack.DefinePlugin(
Object.assign({
'process.env.NODE_ENV': JSON.stringify(config.env),
__CLIENT__: true, // allow detection if clientside rendering
})
),
// Convert lodash-es to lodash, avoiding duplication
new webpack.NormalModuleReplacementPlugin(/^lodash-es(\/|$)/, (res) => {
res.request = res.request.replace(/^lodash-es(\/|$)/, 'lodash$1');
}),
// Disable Moment langs from being auto-required
new webpack.IgnorePlugin(/^\.\/locale$/, [/moment$/]),
new LodashPlugin({
shorthands: true, // Iteratee shorthands for _.property, _.matches, & _.matchesProperty
cloning: true, // Support “clone” methods & cloning source objects
currying: true, // Support “curry” methods
caching: true, // Caches for methods like _.cloneDeep, _.isEqual, & _.uniq
collections: true, // Support objects in “Collection” methods
// deburring: true, // Support deburring letters
memoizing: true, // Support _.memoize & memoization
coercions: true, // Coercion methods like _.toInteger, _.toNumber, & _.toString
flattening: true, // Support “flatten” methods & flattening rest arguments
paths: true, // Deep property path support for methods like _.get, _.has, & _.set
placeholders: true, // Argument placeholder support for “bind”, “curry”, & “partial” methods
}),
// Add any additional provide/define plugin here
]);
function extendEntrySources(sources) {
if (!isProduction) {
sources.unshift('webpack-hot-middleware/client');
}
return sources;
}
function extendCSSLoaders(loaders) {
if (!isProduction) {
loaders.unshift({
loader: 'style-loader',
options: { convertToAbsoluteUrls: true },
});
return loaders;
}
// move css to separate file
return extractCSS.extract({
fallback: 'style-loader',
use: loaders,
publicPath: './', // make css image urls relative
});
}
function extendPlugins(plugins) {
if (!isProduction) {
plugins.unshift(new webpack.HotModuleReplacementPlugin());
} else {
plugins.push(
extractCSS,
new SvgSpriteLoaderPlugin(),
new OptimizeCssAssetsPlugin({
cssProcessorOptions: { discardComments: { removeAll: true } },
canPrint: false,
}),
new webpack.optimize.UglifyJsPlugin({
compress: { warnings: false },
sourceMap: true,
})
);
}
return plugins;
}
module.exports = webpackCfg;