This repository has been archived by the owner on Jul 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.config.js
348 lines (324 loc) · 8.61 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
const webpack = require('webpack');
const path = require('path');
const merge = require('webpack-merge');
const md5 = require('md5');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HTMLWebpackPlugin = require('html-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const NameAllModulesPlugin = require('name-all-modules-plugin');
const OfflinePlugin = require('offline-plugin');
const WebpackPwaManifest = require('webpack-pwa-manifest');
const DashboardPlugin = require('webpack-dashboard/plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
var isProd = process.env.NODE_ENV === 'production';
// -- Offline Plugin --
let offlinePlugin = new OfflinePlugin({
safeToUseOptionalCaches: true,
caches: {
main: [':rest:'],
additional: [':externals:', '*-chunk-*.js']
},
// externals: ['https://fonts.googleapis.com/css?family=Pacifico'],
ServiceWorker: {
navigateFallbackURL: '/',
events: true,
minify: true
}
});
// -- PWA Manifest --
let pwaPlugin = new WebpackPwaManifest({
name: 'Ephemeral',
short_name: 'Ephemeral',
description: 'Save words and translations when you see them!',
background_color: '#A5DBF7',
theme_color: '#A5DBF7',
icons: [
{
src: path.resolve('src/assets/icon.png'),
sizes: [96, 128, 192, 256, 384, 512] // multiple sizes
}
]
});
// Babel plugins
let babelPluginsProd = [
'syntax-dynamic-import',
['transform-remove-console', { exclude: ['error', 'warn', 'info'] }]
];
let babelPluginsDev = ['syntax-dynamic-import'];
// Bundle analyzer config
let bundlePlugin = new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
reportFilename: 'bundle-analysis.html'
});
// Extract text
const extractSass = new ExtractTextPlugin({
filename: '[name].[contenthash].css',
disable: !isProd
});
// -- Common Config --
var common = {
entry: {
main: './src/index.ts',
vendor: ['pouchdb-browser', 'pouchdb-authentication', 'leaflet', 'xstream']
// styles: './src/assets/css/styles.scss'
},
output: {
path: path.join(__dirname, 'dist'),
// Hash as appropriate for production; based on chunks etc.
filename: isProd ? '[name]-[chunkhash].js' : '[name]-[hash].js'
},
plugins: [
new CleanWebpackPlugin(['dist']),
// Give modules a deterministic name for better long-term caching:
// https://github.com/webpack/webpack.js.org/issues/652#issuecomment-273023082
new webpack.NamedModulesPlugin(),
// Give dynamically `import()`-ed scripts a deterministic name for better
// long-term caching.
// Also append '.chunk' to the name, such that offline-plugin caches it
new webpack.NamedChunksPlugin(
chunk =>
chunk.name
? chunk.name
: md5(chunk.mapModules(m => m.identifier()).join()).slice(0, 10) +
'-chunk'
),
new HTMLWebpackPlugin({
// using .ejs prevents other loaders causing errors
template: './src/index.ejs',
minify: isProd
? { collapseWhitespace: true, collapseInlineTagWhitespace: true }
: false,
// inject details of output file at end of body
inject: 'body'
}),
new webpack.optimize.ModuleConcatenationPlugin(),
new webpack.optimize.CommonsChunkPlugin({
name: ['vendor'],
minChunks: Infinity
// (with more entries, this ensures that no other module
// goes into the vendor chunk)
// TODO: add a common entry point if needed
}),
//// Extract runtime code so updates don't affect app-code caching:
// https://webpack.js.org/guides/caching
new webpack.optimize.CommonsChunkPlugin({
name: 'runtime'
}),
// Give deterministic names to all webpacks non-"normal" modules
// https://medium.com/webpack/predictable-long-term-caching-with-webpack-d3eee1d3fa31
new NameAllModulesPlugin(),
pwaPlugin,
extractSass
],
resolve: {
extensions: ['.ts', '.js', '.scss'],
alias: [
{
name: 'ephemeral/elm',
alias: path.resolve(__dirname, 'src', 'Main.elm')
},
{
name: 'ephemeral',
alias: path.resolve(__dirname, 'src')
}
]
},
module: {
rules: [
{
test: /\.html$/,
exclude: /node_modules/,
loader: 'file-loader?name=[name].[ext]'
},
{
test: /\.ts$/,
exclude: /node_modules/,
loader: 'ts-loader'
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true,
presets: [
[
'env',
{
debug: true,
modules: false,
useBuiltIns: true,
targets: {
browsers: ['> 1%', 'last 2 versions', 'Firefox ESR']
}
}
]
],
plugins: isProd ? babelPluginsProd : babelPluginsDev
}
}
},
{
// Transpile and extract scss
test: /\.scss$/,
exclude: [/elm-stuff/, /node_modules/],
use: extractSass.extract({
use: [
{
loader: 'css-loader',
options: {
minimize: isProd,
sourceMap: !isProd
}
},
{
loader: 'resolve-url-loader'
},
{
loader: 'sass-loader',
options: {
sourceMap: !isProd
}
}
],
fallback: 'style-loader'
})
},
{
test: /\.css$/,
exclude: [/elm-stuff/, /node_modules/],
loaders: ['style-loader', 'css-loader']
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
exclude: [/elm-stuff/, /node_modules/],
loader: 'url-loader',
options: {
limit: 10000,
mimetype: 'application/font-woff'
}
},
{
test: /\.svg$/,
loader: 'svg-url-loader'
},
{
test: /\.(ttf|eot)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
exclude: [/elm-stuff/, /node_modules/],
loader: 'file-loader'
},
{
test: /\.(jpe?g|png|gif)$/i,
loader: 'file-loader'
}
]
}
};
if (!isProd) {
console.log('Building for dev...');
module.exports = merge(common, {
devtool: 'cheap-module-eval-source-map',
plugins: [
// Prevents compilation errors causing the hot loader to lose state
new webpack.NoEmitOnErrorsPlugin(),
new DashboardPlugin()
],
resolve: {
alias: [
{
name: 'config',
alias: path.join(__dirname, 'config/development.ts')
}
]
},
module: {
rules: [
{
test: /\.elm$/,
exclude: [/elm-stuff/, /node_modules/],
use: [
{
loader: 'elm-hot-loader'
},
{
loader: 'elm-webpack-loader',
// add Elm's debug overlay to output
options: {
debug: true,
pathToMake: './bin/unbuffered-elm-make'
}
}
]
}
]
},
devServer: {
inline: true,
stats: 'errors-only',
contentBase: path.join(__dirname, 'src/assets')
}
});
}
if (isProd) {
console.log('Building for prod...');
module.exports = merge(common, {
devtool: 'source-map',
plugins: [
new CopyWebpackPlugin([
{
from: 'src/assets',
ignore: ['*.scss']
}
]),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
new UglifyJsPlugin({
sourceMap: true,
uglifyOptions: {
compress: {
warnings: false
},
mangle: {
safari10: true
},
output: {
comments: false
}
}
}),
offlinePlugin,
bundlePlugin
],
resolve: {
alias: [
{
name: 'config',
alias: path.join(__dirname, 'config/production.ts')
}
]
},
module: {
rules: [
{
test: /\.elm$/,
exclude: [/elm-stuff/, /node_modules/],
use: [
{
loader: 'elm-webpack-loader',
options: {
pathToMake: './bin/unbuffered-elm-make'
}
}
]
}
]
}
});
}