Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: minify HTML on build #2537

Merged
merged 4 commits into from
Nov 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const markdown = require('metalsmith-markdown')
const prism = require('metalsmith-prism')
const permalinks = require('metalsmith-permalinks')
const pagination = require('metalsmith-yearly-pagination')
const htmlMinifier = require('metalsmith-html-minifier')
const defaultsDeep = require('lodash.defaultsdeep')
const autoprefixer = require('autoprefixer')
const marked = require('marked')
Expand Down Expand Up @@ -46,6 +47,33 @@ const markedOptions = {
renderer
}

const htmlMinifierOpts = {
collapseBooleanAttributes: true,
collapseWhitespace: true,
conservativeCollapse: true, // This is needed as things are now
decodeEntities: true,
minifyCSS: {
level: {
1: {
specialComments: 0
}
}
},
minifyJS: false, // we don't have a lot inline JS and this slows down things
minifyURLs: false,
processConditionalComments: true,
removeAttributeQuotes: true,
removeComments: true,
removeOptionalAttributes: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeTagWhitespace: false,
sortAttributes: true,
sortClassName: true
}

// This function imports a given language file and uses the default language set
// in DEFAULT_LANG as a fallback to prevent any strings that aren't filled out
// from appearing as blank.
Expand Down Expand Up @@ -179,6 +207,8 @@ function buildLocale (source, locale, opts) {
pattern: /\.js$/
}))
.use(layouts())
// Use the default options
.use(process.env.NODE_ENV !== 'development' ? htmlMinifier({ minifierOptions: htmlMinifierOpts }) : '')
// Pipes the generated files into their respective subdirectory in the build
// directory.
.destination(path.join(__dirname, 'build', locale))
Expand Down
108 changes: 103 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"metalsmith-discover-helpers": "^0.1.1",
"metalsmith-discover-partials": "^0.1.2",
"metalsmith-feed": "^1.0.0",
"metalsmith-html-minifier": "^4.0.0",
"metalsmith-layouts": "^2.3.1",
"metalsmith-markdown": "^1.3.0",
"metalsmith-metadata": "0.0.4",
Expand Down