-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
344 lines (306 loc) · 10.4 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
/*
* Copyright (c) 2016-2018 Martin Donath <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
const fs = require("fs")
const cssmin = require("cssmin")
const path = require("path")
const html = require("html-minifier")
const uglify = require("uglify-js")
const webpack = require("webpack")
/* ----------------------------------------------------------------------------
* Plugins
* ------------------------------------------------------------------------- */
const CopyPlugin = require("copy-webpack-plugin")
const EventHooksPlugin = require("event-hooks-webpack-plugin")
const ExtractTextPlugin = require("extract-text-webpack-plugin")
const ImageminPlugin = require("imagemin-webpack-plugin").default
const ManifestPlugin = require("webpack-manifest-plugin")
/* ----------------------------------------------------------------------------
* Configuration
* ------------------------------------------------------------------------- */
module.exports = env => { // eslint-disable-line complexity
const config = {
/* Entrypoints */
entry: {
/* Custom Modernizr build */
"assets/javascripts/modernizr": path.resolve(
__dirname, "src/assets/javascripts/modernizr.js"
),
/* Application */
"assets/javascripts/application": path.resolve(
__dirname, "src/assets/javascripts/application.js"
)
},
/* Loaders */
module: {
rules: [
/* Babel ES6 transformations */
{
test: /\.jsx?$/,
use: "babel-loader",
exclude: /\/node_modules\//
},
/* Custom Modernizr build */
{
test: /\.modernizr-autorc$/,
use: "modernizr-auto-loader"
},
/* Cache busting for SVGs */
{
test: /\.svg$/,
use: `file-loader?name=[path][name]${
env && env.prod ? ".[md5:hash:hex:8]" : ""
}.[ext]&context=./src`
}
]
},
/* Output */
output: {
path: path.resolve(__dirname, "material"),
filename: `[name]${env && env.prod ? ".[chunkhash]" : ""}.js`,
hashDigestLength: 8,
libraryTarget: "window"
},
/* Plugins */
plugins: [
/* Combine all dependencies into a single file */
new webpack.optimize.CommonsChunkPlugin({
name: "src/assets/javascripts/modernizr.js",
chunks: [".modernizr-autorc"]
}),
/* Provide JSX helper */
new webpack.ProvidePlugin({
JSX: path.resolve(__dirname, "src/assets/javascripts/providers/jsx.js")
}),
/* Copy and transform static assets */
new CopyPlugin([
/* Copy search language support files - we could define the languages
package as entrypoints, but this leads to a lot of problems because
the files have the structure lunr.[language].js, which some Webpack
plugins will complain about. For this reason we only minify */
{
context: path.resolve(__dirname, "node_modules/lunr-languages"),
to: "assets/javascripts/lunr",
from: "*.js",
transform: content => {
return uglify.minify(content.toString()).code
}
},
/* Copy web font files */
{
context: "src",
from: "assets/fonts/**/*",
ignore: "**/*.css"
},
/* Copy and minify web font stylesheets */
{
context: "src",
from: "assets/fonts/*.css",
transform: content => cssmin(content.toString())
},
/* Copy images without cache busting */
{
context: "src",
from: "assets/images/*.{ico,png}"
},
/* Copy configuration */
{
context: "src",
from: "**/*.{py,yml}"
},
/* Copy and minify HTML */
{
context: "src",
from: "**/*.html",
transform: content => {
const metadata = require(path.resolve(__dirname, "package.json"))
return html.minify(content.toString(), {
collapseBooleanAttributes: true,
includeAutoGeneratedTags: false,
minifyCSS: true,
minifyJS: true,
removeComments: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true
})
/* Remove empty lines without collapsing everything */
.replace(/^\s*[\r\n]/gm, "")
/* Write theme version into template */
.replace("$md-name$", metadata.name)
.replace("$md-version$", metadata.version)
/* Write available search languages into template */
.replace("$md-lunr-languages$",
fs.readdirSync(
path.resolve(__dirname, "node_modules/lunr-languages")
).reduce((files, file) => {
const matches = file.match(/lunr.(\w{2}).js$/)
if (matches) {
const [, language] = matches
files.push(`"${language}"`)
}
return files
}, [])
.join(", "))
}
}
]),
/* Hack: The webpack development middleware sometimes goes into a loop on
macOS when starting for the first time. This is a quick fix until
this issue is resolved. See: http://bit.ly/2AsizEn */
new EventHooksPlugin({
"watch-run": (compiler, cb) => {
compiler.startTime += 10000
cb()
},
"done": stats => {
stats.startTime -= 10000
}
})
],
/* Module resolver */
resolve: {
modules: [
path.resolve(__dirname, "node_modules")
],
extensions: [".js", ".jsx", ".scss"],
alias: {
modernizr$: path.resolve(__dirname, ".modernizr-autorc")
}
},
/* Sourcemaps */
devtool: !env || env.prod ? "inline-source-map" : ""
}
/* Compile stylesheets */
for (const stylesheet of [
"application.scss",
"application-palette.scss"
]) {
const plugin = new ExtractTextPlugin(
`assets/stylesheets/${
stylesheet.replace(".scss",
env && env.prod ? ".[md5:contenthash:hex:8]" : ""
)}.css`)
/* Register plugin */
config.plugins.push(plugin)
config.module.rules.push({
test: new RegExp(`${stylesheet}$`),
use: plugin.extract({
use: [
{
loader: "css-loader",
options: {
minimize: env && env.prod,
sourceMap: !(env && env.prod)
}
},
{
loader: "postcss-loader",
options: {
ident: "postcss",
plugins: () => [
require("autoprefixer")(),
require("css-mqpacker")
],
sourceMap: !(env && env.prod)
}
},
{
loader: "sass-loader",
options: {
includePaths: [
"node_modules/modularscale-sass/stylesheets",
"node_modules/material-design-color",
"node_modules/material-shadows"
],
sourceMap: !(env && env.prod),
sourceMapContents: true
}
}
]
})
})
}
/* Production compilation */
if (env && env.prod) {
config.plugins.push(
/* Uglify sources */
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
screw_ie8: true, // eslint-disable-line camelcase
conditionals: true,
unused: true,
comparisons: true,
sequences: true,
dead_code: true, // eslint-disable-line camelcase
evaluate: true,
if_return: true, // eslint-disable-line camelcase
join_vars: true // eslint-disable-line camelcase
},
output: {
comments: false
}
}),
/* Minify images */
new ImageminPlugin({
test: /\.(ico|png|svg)$/i,
svgo: null
// Hack: Temporarily disabled, as SVGO removes the viewbox property
// and setting the plugin to false doesn't have any effect.
// {
// plugins: [
// {
// cleanupIDs: false,
// removeViewBox: false
// }
// ]
// }
}),
/* Write manifest */
new ManifestPlugin({
/* This is an ugly workaround for the fact that the manifest plugin
doesn't handle multiple chunks. See http://bit.ly/2BbfER9 */
map(file) {
file.name = file.path.replace(/\.[a-z0-9].+\.(css|js|svg)/i, ".$1")
return file
}
}),
/* Apply manifest */
new EventHooksPlugin({
"after-emit": (compilation, cb) => {
const manifest = require(path.resolve("material/manifest.json"))
Object.keys(compilation.assets).forEach(name => {
if (name.match(/\.html/)) {
const asset = compilation.assets[name]
const replaced = Object.keys(manifest).reduce((source, key) => {
return source.replace(key, manifest[key])
}, asset.source())
fs.writeFileSync(asset.existsAt, replaced)
}
})
cb()
}
})
)
}
/* Oh my god, that was a hell of a setup */
return config
}