From 43bf6e90f8ac770ae5eb9768249ae8c18cdfb53a Mon Sep 17 00:00:00 2001 From: endiliey Date: Wed, 11 Dec 2019 16:56:36 +0700 Subject: [PATCH 1/3] wip: minify html with terser --- packages/docusaurus/package.json | 1 + packages/docusaurus/src/client/serverEntry.js | 16 +++++++++++++++- yarn.lock | 2 +- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/docusaurus/package.json b/packages/docusaurus/package.json index 3de3f9d79e71..ec9d8fd75752 100644 --- a/packages/docusaurus/package.json +++ b/packages/docusaurus/package.json @@ -55,6 +55,7 @@ "express": "^4.17.1", "fs-extra": "^8.1.0", "globby": "^10.0.1", + "html-minifier-terser": "^5.0.2", "html-tags": "^3.1.0", "html-webpack-plugin": "^4.0.0-beta.11", "import-fresh": "^3.2.1", diff --git a/packages/docusaurus/src/client/serverEntry.js b/packages/docusaurus/src/client/serverEntry.js index 0e936541f981..ce88d56f698e 100644 --- a/packages/docusaurus/src/client/serverEntry.js +++ b/packages/docusaurus/src/client/serverEntry.js @@ -13,6 +13,7 @@ import {Helmet} from 'react-helmet'; import {getBundles} from 'react-loadable-ssr-addon'; import Loadable from 'react-loadable'; +import {minify} from 'html-minifier-terser'; import path from 'path'; import fs from 'fs-extra'; import routes from '@generated/routes'; @@ -68,7 +69,7 @@ export default async function render(locals) { const scripts = (bundles.js || []).map(b => b.file); const {baseUrl} = locals; - return ejs.render( + const renderedHtml = ejs.render( ssrTemplate.trim(), { appHtml, @@ -87,4 +88,17 @@ export default async function render(locals) { rmWhitespace: true, }, ); + + // minify html + return minify(renderedHtml, { + // https://github.com/DanielRuf/html-minifier-terser + collapseWhitespace: true, + removeComments: true, + removeRedundantAttributes: true, + removeScriptTypeAttributes: true, + removeStyleLinkTypeAttributes: true, + useShortDoctype: true, + minifyJS: true, + minifyCSS: true, + }); } diff --git a/yarn.lock b/yarn.lock index af61e867d749..16396e6f9fbb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7553,7 +7553,7 @@ html-entities@^1.2.1: resolved "https://registry.yarnpkg.com/html-entities/-/html-entities-1.2.1.tgz#0df29351f0721163515dfb9e5543e5f6eed5162f" integrity sha1-DfKTUfByEWNRXfueVUPl9u7VFi8= -html-minifier-terser@^5.0.1: +html-minifier-terser@^5.0.1, html-minifier-terser@^5.0.2: version "5.0.2" resolved "https://registry.yarnpkg.com/html-minifier-terser/-/html-minifier-terser-5.0.2.tgz#0e67a0b062ae1dd0719fc73199479298f807ae16" integrity sha512-VAaitmbBuHaPKv9bj47XKypRhgDxT/cDLvsPiiF7w+omrN3K0eQhpigV9Z1ilrmHa9e0rOYcD6R/+LCDADGcnQ== From aee4562739f2d8770b2ce471734cf3720dceba64 Mon Sep 17 00:00:00 2001 From: endiliey Date: Wed, 11 Dec 2019 18:41:53 +0700 Subject: [PATCH 2/3] tweak --- packages/docusaurus/src/client/serverEntry.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/docusaurus/src/client/serverEntry.js b/packages/docusaurus/src/client/serverEntry.js index ce88d56f698e..21aa4ef7f398 100644 --- a/packages/docusaurus/src/client/serverEntry.js +++ b/packages/docusaurus/src/client/serverEntry.js @@ -89,9 +89,8 @@ export default async function render(locals) { }, ); - // minify html + // minify html with https://github.com/DanielRuf/html-minifier-terser return minify(renderedHtml, { - // https://github.com/DanielRuf/html-minifier-terser collapseWhitespace: true, removeComments: true, removeRedundantAttributes: true, From 036fd90f227844e1c916b739eb2bf3b4d9fc3464 Mon Sep 17 00:00:00 2001 From: endiliey Date: Thu, 12 Dec 2019 19:11:08 +0700 Subject: [PATCH 3/3] remove empty attr --- packages/docusaurus/src/client/serverEntry.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/docusaurus/src/client/serverEntry.js b/packages/docusaurus/src/client/serverEntry.js index 21aa4ef7f398..e6cbaa328965 100644 --- a/packages/docusaurus/src/client/serverEntry.js +++ b/packages/docusaurus/src/client/serverEntry.js @@ -94,6 +94,7 @@ export default async function render(locals) { collapseWhitespace: true, removeComments: true, removeRedundantAttributes: true, + removeEmptyAttributes: true, removeScriptTypeAttributes: true, removeStyleLinkTypeAttributes: true, useShortDoctype: true,