Skip to content

Commit bf525e4

Browse files
authored
Merge pull request #1007 from davidmehren/webpack-4
Webpack 4 refactor & docs
2 parents c7478c1 + 7eed584 commit bf525e4

File tree

9 files changed

+99
-118
lines changed

9 files changed

+99
-118
lines changed

docs/dev/webpack.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Webpack Docs
2+
## `webpack.common.js`
3+
This file contains all common definition for chunks and plugins, that are needed by the whole app.
4+
5+
**TODO:** Document which entry points are used for what.
6+
7+
## `webpack.htmlexport.js`
8+
Separate config for the "save as html" feature.
9+
Packs all CSS from `public/js/htmlExport.js` to `build/html.min.css`.
10+
This file is then downloaded by client-side JS and used to create the HTML.
11+
See `exportToHTML()` in `public/js/extra.js`.
12+
13+
14+
## `webpack.dev.js`
15+
The development config uses both common configs, enables development mode and enables "cheap" source maps (lines only).
16+
If you need more detailed source maps while developing, you might want to use the `source-maps` option.
17+
See https://webpack.js.org/configuration/devtool/ for details.
18+
19+
## `webpack.prod.js`
20+
The production config uses both common configs and enables production mode.
21+
This automatically enables various optimizations (e.g. UglifyJS). See https://webpack.js.org/concepts/mode/ for details.
22+
23+
For the global app config, the name of the emitted chunks is changed to include the content hash.
24+
See https://webpack.js.org/guides/caching/ on why this is a good idea.
25+
26+
For the HTML export config, CSS minification is enabled.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"test": "npm run-script standard && npm run-script jsonlint",
99
"jsonlint": "find . -not -path './node_modules/*' -type f -name '*.json' -o -type f -name '*.json.example' | while read json; do echo $json ; jq . $json; done",
1010
"standard": "node ./node_modules/standard/bin/cmd.js",
11-
"dev": "webpack --config webpack.config.js --mode=production --progress --colors --watch",
12-
"build": "webpack --config webpack.production.js --progress --colors --bail",
11+
"dev": "webpack --config webpack.dev.js --progress --colors --watch",
12+
"build": "webpack --config webpack.prod.js --progress --colors --bail",
1313
"postinstall": "bin/heroku",
1414
"start": "node app.js",
1515
"doctoc": "doctoc --title='# Table of Contents' README.md"
@@ -188,6 +188,7 @@
188188
"url-loader": "^1.0.1",
189189
"webpack": "^4.14.0",
190190
"webpack-cli": "^3.1.0",
191+
"webpack-merge": "^4.1.4",
191192
"webpack-parallel-uglify-plugin": "^1.1.0"
192193
},
193194
"standard": {

webpackBaseConfig.js renamed to webpack.common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var gracefulFs = require('graceful-fs')
1010
gracefulFs.gracefulify(fs)
1111

1212
module.exports = {
13+
name: 'app',
1314
plugins: [
1415
new webpack.ProvidePlugin({
1516
Visibility: 'visibilityjs',

webpack.config.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

webpack.dev.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const common = require('./webpack.common.js')
2+
const htmlexport = require('./webpack.htmlexport')
3+
const merge = require('webpack-merge')
4+
5+
module.exports = [
6+
// merge common config
7+
merge(common, {
8+
mode: 'development',
9+
devtool: 'cheap-module-eval-source-map'
10+
}),
11+
merge(htmlexport, {
12+
mode: 'development',
13+
devtool: 'cheap-module-eval-source-map'
14+
})]

webpack.htmlexport.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
2+
const path = require('path')
3+
4+
module.exports = {
5+
name: 'save-as-html',
6+
entry: {
7+
htmlExport: path.join(__dirname, 'public/js/htmlExport.js')
8+
},
9+
module: {
10+
rules: [{
11+
test: /\.css$/,
12+
use: [MiniCssExtractPlugin.loader, 'css-loader']
13+
}]
14+
},
15+
output: {
16+
path: path.join(__dirname, 'public/build'),
17+
publicPath: '/build/',
18+
filename: '[name].js'
19+
},
20+
plugins: [
21+
new MiniCssExtractPlugin({
22+
filename: 'html.min.css'
23+
})
24+
]
25+
}

webpack.prod.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const common = require('./webpack.common.js')
2+
const htmlexport = require('./webpack.htmlexport')
3+
const merge = require('webpack-merge')
4+
const path = require('path')
5+
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
6+
7+
module.exports = [
8+
merge(common, {
9+
mode: 'production',
10+
output: {
11+
path: path.join(__dirname, 'public/build'),
12+
publicPath: '/build/',
13+
filename: '[name].[contenthash].js'
14+
}
15+
}),
16+
merge(htmlexport, {
17+
mode: 'production',
18+
optimization: {
19+
minimizer: [
20+
new OptimizeCSSAssetsPlugin({})
21+
]
22+
}
23+
})]

webpack.production.js

Lines changed: 0 additions & 75 deletions
This file was deleted.

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11168,6 +11168,13 @@ webpack-cli@^3.1.0:
1116811168
v8-compile-cache "^2.0.0"
1116911169
yargs "^12.0.1"
1117011170

11171+
webpack-merge@^4.1.4:
11172+
version "4.1.4"
11173+
resolved "https://registry.yarnpkg.com/webpack-merge/-/webpack-merge-4.1.4.tgz#0fde38eabf2d5fd85251c24a5a8c48f8a3f4eb7b"
11174+
integrity sha512-TmSe1HZKeOPey3oy1Ov2iS3guIZjWvMT2BBJDzzT5jScHTjVC3mpjJofgueEzaEd6ibhxRDD6MIblDr8tzh8iQ==
11175+
dependencies:
11176+
lodash "^4.17.5"
11177+
1117111178
webpack-parallel-uglify-plugin@^1.1.0:
1117211179
version "1.1.0"
1117311180
resolved "https://registry.yarnpkg.com/webpack-parallel-uglify-plugin/-/webpack-parallel-uglify-plugin-1.1.0.tgz#252a6c796bf79a8047b00de2cf08c23aa9861441"

0 commit comments

Comments
 (0)