This repository was archived by the owner on Jan 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathwebpackConfigFactory.js
More file actions
307 lines (275 loc) · 8.84 KB
/
webpackConfigFactory.js
File metadata and controls
307 lines (275 loc) · 8.84 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
const path = require('path');
const autoprefixer = require('autoprefixer');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const NameAllModulesPlugin = require('name-all-modules-plugin');
const assert = require('assert');
const ServiceWorkerPreCachePlugin = require('sw-precache-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const RollbarSourceMapPlugin = require('rollbar-sourcemap-webpack-plugin');
const _ = require('lodash');
const paths = require('./paths');
const config = require('../src/config/default');
const manup = require('manup');
const manifest = require('../src/manifest.json');
const pkg = require('../package.json');
module.exports = ({
entryPath,
name,
htmlWebpackPlugin,
production,
filename,
serviceWorker,
longTermCache,
...extra
}) => {
assert(entryPath, 'entryPath should be defined in webpack factory');
assert(name, 'name should be defined in webpack factory');
let publicPath = '/';
if (production && !process.env.LOCAL) {
publicPath = process.env.TRAVIS_TAG
? 'https://gw2armory.com/'
: process.env.PUBLIC_PATH || publicPath;
}
const cssRulesUse = production
? ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
modules: true,
minimize: true,
importLoaders: '1',
localIdentName: '[hash:base64:5]',
sourceMap: true,
},
},
{
loader: 'postcss-loader',
options: {
plugins: () => [
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
flexbox: 'no-2009',
}),
],
},
},
'less-loader',
],
})
: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: true,
localIdentName: '[path][name]__[local]--[hash:base64:5]',
importLoaders: '1',
sourceMap: true,
},
},
{
loader: 'postcss-loader',
options: {
plugins: () => [
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
flexbox: 'no-2009',
}),
],
},
},
'less-loader',
];
return _.merge(
{},
{
bail: production,
context: __dirname,
devtool: production ? 'source-map' : 'eval',
entry: {
[name]: production
? path.join(entryPath, 'index')
: [
require.resolve('webpack-dev-server/client'),
require.resolve('webpack/hot/dev-server'),
path.join(entryPath, 'index'),
],
},
output: {
// Next line is not used in dev but WebpackDevServer crashes without it:
path: paths.appBuild,
pathinfo: true,
filename: filename || (production ? '[name].[chunkhash:8].js' : '[name].js'),
chunkFilename: production ? '[name]-chunk.[chunkhash:8].js' : '[name]-chunk.js',
publicPath,
},
resolve: {
modules: [path.resolve('./src'), 'node_modules'],
extensions: ['.js', '.json'],
},
module: {
strictExportPresence: true,
rules: [
{
test: /\.js$/,
include: paths.appSrc,
loader: 'babel-loader',
},
{
test: /\.(css|less)$/,
include: [paths.appSrc, paths.appNodeModules],
use: cssRulesUse,
},
{
test: [
/\.gif$/,
/\.jpe?g$/,
/\.png$/,
/\.ico$/,
/\.svg$/,
/\.eot$/,
/\.ttf$/,
/\.woff$/,
/\.woff2$/,
],
include: [paths.appSrc, paths.appNodeModules],
loader: 'file-loader',
options: {
name: production ? 'assets/[name].[hash:8].[ext]' : 'assets/[name].[ext]',
},
},
],
},
plugins: [
new webpack.optimize.ModuleConcatenationPlugin(),
new webpack.ProvidePlugin({
React: 'react',
}),
new ManifestPlugin({
fileName: 'asset-manifest.json',
}),
new HtmlWebpackPlugin({
...config,
...htmlWebpackPlugin,
env: production ? 'production' : 'development',
pwaMeta: manup(manifest),
version: pkg.version,
minify: production && {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
},
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(production ? 'production' : 'development'),
__DEVELOPMENT__: !production,
}),
new webpack.optimize.CommonsChunkPlugin({
minChunks: 2,
async: true,
}),
!production && new webpack.HotModuleReplacementPlugin(),
production &&
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
screw_ie8: true,
warnings: false,
reduce_vars: false,
},
mangle: {
screw_ie8: true,
},
output: {
comments: false,
screw_ie8: true,
},
parallel: true,
}),
production &&
new ExtractTextPlugin({
filename: 'assets/[name].[contenthash:8].css',
allChunks: true,
}),
// Note that ServiceWorker plugin doesn't work with webpack dev server,
// So we only run it in production mode.
serviceWorker &&
new ServiceWorkerPreCachePlugin({
cacheId: 'GW2Armory',
filename: 'service-worker.js',
minify: production,
dontCacheBustUrlsMatching: /\.\w{8}\./,
navigateFallback: 'index.html',
// Ignore any source map files and the asset manifest.
staticFileGlobsIgnorePatterns: [/\.map$/, /asset-manifest\.json$/, /gw2aEmbeds\.js$/],
}),
production &&
process.env.ROLLBAR_POST &&
new RollbarSourceMapPlugin({
accessToken: process.env.ROLLBAR_POST,
version: pkg.version,
// Rollbar can't have a trailing slash. Ends up creating
// URLS like: //gw2armory.com//3-chunk.9a82d951.js
publicPath: publicPath.slice(0, publicPath.length - 1),
}),
// Moment.js is an extremely popular library that bundles large locale files
// by default due to how Webpack interprets its code. This is a practical
// solution that requires the user to opt into importing specific locales.
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
// You can remove this if you don't use Moment.js:
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
// See: https://medium.com/webpack/predictable-long-term-caching-with-webpack-d3eee1d3fa31
// >> Start longterm caching strategy.
longTermCache && new webpack.NamedModulesPlugin(),
longTermCache &&
new webpack.NamedChunksPlugin(chunk => {
return chunk.name
? chunk.name
: chunk.modules.map(m => path.relative(m.context, m.request)).join('_');
}),
longTermCache &&
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: Infinity,
}),
longTermCache &&
new webpack.optimize.CommonsChunkPlugin({
name: 'runtime',
}),
longTermCache && new NameAllModulesPlugin(),
// >> End longterm caching strategy.
// >> Perf plugins
production &&
new BundleAnalyzerPlugin({
analyzerMode: 'static',
}),
].filter(Boolean),
performance: {
hints: production ? 'warning' : false,
},
},
extra
);
};