From 5a3a30ccdb2e61bc5e70e994e63fad2b9d09684d Mon Sep 17 00:00:00 2001 From: Zhicheng Wang Date: Sun, 23 Sep 2018 22:42:13 +0800 Subject: [PATCH] chore: use html-minifier instead of manual minify --- aio/package.json | 1 + .../translator/assets/aio-shell-index.html | 12 +- .../translator/assets/aio-shell-template.html | 765 +++++++++++++++++- aio/tools/translator/bin/ssr.ts | 20 +- aio/yarn.lock | 31 +- 5 files changed, 819 insertions(+), 10 deletions(-) diff --git a/aio/package.json b/aio/package.json index 0c0bfcec4f..4512ed9c33 100644 --- a/aio/package.json +++ b/aio/package.json @@ -87,6 +87,7 @@ "chai": "^4.1.2", "classlist.js": "^1.1.20150312", "core-js": "^2.4.1", + "html-minifier": "^3.5.20", "jasmine": "^2.6.0", "klaw-sync": "^5.0.0", "mkdirp": "^0.5.1", diff --git a/aio/tools/translator/assets/aio-shell-index.html b/aio/tools/translator/assets/aio-shell-index.html index 5d94927cdc..7aecd705e6 100644 --- a/aio/tools/translator/assets/aio-shell-index.html +++ b/aio/tools/translator/assets/aio-shell-index.html @@ -1 +1,11 @@ - + + + diff --git a/aio/tools/translator/assets/aio-shell-template.html b/aio/tools/translator/assets/aio-shell-template.html index 31ddd4b7d8..fccdc98b58 100644 --- a/aio/tools/translator/assets/aio-shell-template.html +++ b/aio/tools/translator/assets/aio-shell-template.html @@ -1 +1,764 @@ -
Version 6 of Angular Now Available! Learn More Home
\ No newline at end of file + +
+ + + + + Version 6 of Angular Now Available! Learn More + + + + + + Home + + + + + +
+
+
+ +
+
+ + + + + + + + + + + + + + + + + +
+
+
+ + + + + + +
+
+ +
+
+
+
+
+
+ + + +
+
+ + + + +
+ +
diff --git a/aio/tools/translator/bin/ssr.ts b/aio/tools/translator/bin/ssr.ts index e1d6c1e3ac..d1452d5198 100644 --- a/aio/tools/translator/bin/ssr.ts +++ b/aio/tools/translator/bin/ssr.ts @@ -2,6 +2,7 @@ import * as fs from 'fs'; import * as mkdirp from 'mkdirp'; import * as path from 'path'; import * as klawSync from 'klaw-sync'; +import { minify } from 'html-minifier'; const rootElementPattern = //; @@ -9,10 +10,15 @@ const indexTemplate = fs.readFileSync('./dist/index.html', 'utf-8'); const aioShellTemplate = fs.readFileSync(__dirname + '/../assets/aio-shell-template.html'); const pageTemplate = indexTemplate.replace(rootElementPattern, `${aioShellTemplate}`); -function purge(text: string): string { - return text.replace(//g, '') - .replace(//g, ''); -} +const minifyOptions = { + collapseWhitespace: true, + ignoreCustomFragments: [/[\s\S]*?<\/code>/], + minifyCSS: true, + minifyJS: true, + removeComments: true, + removeScriptTypeAttributes: true, + removeStyleLinkTypeAttributes: true, +}; function composePage(url) { const { title, contents } = JSON.parse(fs.readFileSync(`./dist/generated/docs/${url}.json`, 'utf-8')); @@ -25,7 +31,7 @@ function composePage(url) { .replace('Angular Docs', `${title} - Angular 官方文档`) .replace('', ssrContent); mkdirp.sync(path.dirname(`./dist/${url}`)); - fs.writeFileSync(`./dist/${url}.html`, purge(pageContent), 'utf-8'); + fs.writeFileSync(`./dist/${url}.html`, minify(pageContent, minifyOptions), 'utf-8'); } function buildGuidePages(): void { @@ -45,14 +51,14 @@ function buildApiPages(): void { const apiListContent = fs.readFileSync('./dist/api.html', 'utf-8') .replace(rootElementPattern, `

API List

${links}
`); - fs.writeFileSync(`./dist/api.html`, purge(apiListContent), 'utf-8'); + fs.writeFileSync(`./dist/api.html`, minify(apiListContent, minifyOptions), 'utf-8'); } function buildIndexPage(): void { const indexTemplate = fs.readFileSync('./dist/index.html', 'utf-8'); const aioShellIndex = fs.readFileSync(__dirname + '/../assets/aio-shell-index.html'); const content = indexTemplate.replace(rootElementPattern, `${aioShellIndex}`); - fs.writeFileSync(`./dist/index.html`, purge(content), 'utf-8'); + fs.writeFileSync(`./dist/index.html`, minify(content, minifyOptions), 'utf-8'); } buildGuidePages(); diff --git a/aio/yarn.lock b/aio/yarn.lock index 844145fb44..d1958db2a8 100644 --- a/aio/yarn.lock +++ b/aio/yarn.lock @@ -2214,6 +2214,12 @@ clean-css@4.1.x, clean-css@^4.1.11: dependencies: source-map "0.5.x" +clean-css@4.2.x: + version "4.2.1" + resolved "https://registry.yarnpkg.com/clean-css/-/clean-css-4.2.1.tgz#2d411ef76b8569b6d0c84068dabe85b0aa5e5c17" + dependencies: + source-map "~0.6.0" + cli-boxes@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/cli-boxes/-/cli-boxes-1.0.0.tgz#4fa917c3e59c94a004cd61f8ee509da651687143" @@ -2409,6 +2415,10 @@ commander@2.15.x, commander@^2.12.1, commander@^2.9.0, commander@~2.15.0: version "2.15.1" resolved "https://registry.yarnpkg.com/commander/-/commander-2.15.1.tgz#df46e867d0fc2aec66a34662b406a9ccafff5b0f" +commander@2.17.x, commander@~2.17.1: + version "2.17.1" + resolved "https://registry.yarnpkg.com/commander/-/commander-2.17.1.tgz#bd77ab7de6de94205ceacc72f1716d29f20a77bf" + commander@2.6.0: version "2.6.0" resolved "https://registry.yarnpkg.com/commander/-/commander-2.6.0.tgz#9df7e52fb2a0cb0fb89058ee80c3104225f37e1d" @@ -5028,6 +5038,18 @@ html-minifier@^3.2.3: relateurl "0.2.x" uglify-js "3.3.x" +html-minifier@^3.5.20: + version "3.5.20" + resolved "https://registry.yarnpkg.com/html-minifier/-/html-minifier-3.5.20.tgz#7b19fd3caa0cb79f7cde5ee5c3abdf8ecaa6bb14" + dependencies: + camel-case "3.0.x" + clean-css "4.2.x" + commander "2.17.x" + he "1.1.x" + param-case "2.1.x" + relateurl "0.2.x" + uglify-js "3.4.x" + html-void-elements@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/html-void-elements/-/html-void-elements-1.0.2.tgz#9d22e0ca32acc95b3f45b8d5b4f6fbdc05affd55" @@ -9487,7 +9509,7 @@ source-map@^0.4.2, source-map@^0.4.4, source-map@~0.4.1: dependencies: amdefine ">=0.0.4" -source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1: +source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.0, source-map@~0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" @@ -10341,6 +10363,13 @@ uglify-js@3.3.x: commander "~2.15.0" source-map "~0.6.1" +uglify-js@3.4.x: + version "3.4.9" + resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.4.9.tgz#af02f180c1207d76432e473ed24a28f4a782bae3" + dependencies: + commander "~2.17.1" + source-map "~0.6.1" + uglify-js@^2.6: version "2.8.29" resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.8.29.tgz#29c5733148057bb4e1f75df35b7a9cb72e6a59dd"