Skip to content

Commit 0cc3648

Browse files
nschonniSEWeiTung
authored andcommitted
feat: minify HTML on build (#2537)
1. Improve html-minifier's options. 2. Skip html-minifier on dev.
1 parent 02a62cf commit 0cc3648

File tree

3 files changed

+134
-5
lines changed

3 files changed

+134
-5
lines changed

build.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const markdown = require('metalsmith-markdown')
1616
const prism = require('metalsmith-prism')
1717
const permalinks = require('metalsmith-permalinks')
1818
const pagination = require('metalsmith-yearly-pagination')
19+
const htmlMinifier = require('metalsmith-html-minifier')
1920
const defaultsDeep = require('lodash.defaultsdeep')
2021
const autoprefixer = require('autoprefixer')
2122
const marked = require('marked')
@@ -46,6 +47,33 @@ const markedOptions = {
4647
renderer
4748
}
4849

50+
const htmlMinifierOpts = {
51+
collapseBooleanAttributes: true,
52+
collapseWhitespace: true,
53+
conservativeCollapse: true, // This is needed as things are now
54+
decodeEntities: true,
55+
minifyCSS: {
56+
level: {
57+
1: {
58+
specialComments: 0
59+
}
60+
}
61+
},
62+
minifyJS: false, // we don't have a lot inline JS and this slows down things
63+
minifyURLs: false,
64+
processConditionalComments: true,
65+
removeAttributeQuotes: true,
66+
removeComments: true,
67+
removeOptionalAttributes: true,
68+
removeOptionalTags: true,
69+
removeRedundantAttributes: true,
70+
removeScriptTypeAttributes: true,
71+
removeStyleLinkTypeAttributes: true,
72+
removeTagWhitespace: false,
73+
sortAttributes: true,
74+
sortClassName: true
75+
}
76+
4977
// This function imports a given language file and uses the default language set
5078
// in DEFAULT_LANG as a fallback to prevent any strings that aren't filled out
5179
// from appearing as blank.
@@ -179,6 +207,8 @@ function buildLocale (source, locale, opts) {
179207
pattern: /\.js$/
180208
}))
181209
.use(layouts())
210+
// Use the default options
211+
.use(process.env.NODE_ENV !== 'development' ? htmlMinifier({ minifierOptions: htmlMinifierOpts }) : '')
182212
// Pipes the generated files into their respective subdirectory in the build
183213
// directory.
184214
.destination(path.join(__dirname, 'build', locale))

package-lock.json

Lines changed: 103 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"metalsmith-discover-helpers": "^0.1.1",
5151
"metalsmith-discover-partials": "^0.1.2",
5252
"metalsmith-feed": "^1.0.0",
53+
"metalsmith-html-minifier": "^4.0.0",
5354
"metalsmith-layouts": "^2.3.1",
5455
"metalsmith-markdown": "^1.3.0",
5556
"metalsmith-metadata": "0.0.4",

0 commit comments

Comments
 (0)