From 4711759937e59ed44ae0277f5238c8eb9b527620 Mon Sep 17 00:00:00 2001 From: Alex Anderson <191496+alxndrsn@users.noreply.github.com> Date: Thu, 9 Jan 2025 18:24:54 +0300 Subject: [PATCH] fix(docs): minify code.js automatically (#9051) Previously minification seemed to happen "offline", and both source and minified file were tracked in git. This also prevents the unused source file `code.js` from being served at https://pouchdb.com/static/js/code.js --- bin/build-site.js | 21 +++++++++++++++++++++ docs/_config.yml | 2 ++ docs/{static/js => src}/code.js | 0 docs/static/js/code.min.js | 1 - 4 files changed, 23 insertions(+), 1 deletion(-) rename docs/{static/js => src}/code.js (100%) delete mode 100644 docs/static/js/code.min.js diff --git a/bin/build-site.js b/bin/build-site.js index 94f2f0a08e..691e006e22 100755 --- a/bin/build-site.js +++ b/bin/build-site.js @@ -11,6 +11,7 @@ const Path = require('node:path'); var replace = require('replace'); var mkdirp = require('mkdirp'); var cssmin = require('cssmin'); +const terser = require('terser'); const POUCHDB_CSS = resolvePath('docs/static/css/pouchdb.css'); const POUCHDB_LESS = resolvePath('docs/static/less/pouchdb/pouchdb.less'); @@ -43,6 +44,26 @@ function buildJekyll(path) { return highlightEs6(); }).then(function () { console.log('=> Highlighted ES6'); + + const srcPath = resolvePath('docs/src/code.js'); + const targetPath = resolvePath('docs/_site/static/js/code.min.js'); + const src = fs.readFileSync(srcPath, { encoding:'utf8' }); + const mangle = { toplevel: true }; + const output = { ascii_only: true }; + const { code, error } = terser.minify(src, { mangle, output }); + if (error) { + if (process.env.BUILD) { + throw error; + } else { + console.log( + `Javascript minification failed on line ${error.line} col ${error.col}:`, + error.message, + ); + } + } else { + fs.writeFileSync(targetPath, code); + console.log('Minified javascript.'); + } }); } diff --git a/docs/_config.yml b/docs/_config.yml index f6d6709aec..4b1b19275a 100644 --- a/docs/_config.yml +++ b/docs/_config.yml @@ -11,3 +11,5 @@ github: collections: guides: output: true +exclude: + - src diff --git a/docs/static/js/code.js b/docs/src/code.js similarity index 100% rename from docs/static/js/code.js rename to docs/src/code.js diff --git a/docs/static/js/code.min.js b/docs/static/js/code.min.js deleted file mode 100644 index 5bf9fd3313..0000000000 --- a/docs/static/js/code.min.js +++ /dev/null @@ -1 +0,0 @@ -function codeWrap(){"use strict";function a(){var a='
{{tapPanes}}
';l.forEach(function(e){var t=$("[data-code-id='"+e+"']"),c=t.get().map(function(a){return a.outerHTML}).join(""),i=a.replace(/{{tapPanes}}/g,c).replace(/{{codeId}}/g,e);t.first().replaceWith(i),t.remove()}),$("[data-code-hide]").addClass("hide")}function e(a){var e=!localStorage.getItem("codeStyle");if(e&&localStorage.setItem("codeStyle",c),a=a||localStorage.getItem("codeStyle"),"undefined"!=typeof a&&null!==a){var t=$('[data-code-tablist] [href="#'+a+'"]').parent();$("[data-code-tablist] li").removeClass("active"),t.addClass("active"),$(".tab-pane").removeClass("active"),$('.tab-pane[id="'+a+'"]').addClass("active"),localStorage.setItem("codeStyle",a)}}function t(){o.length>0||l.forEach(function(a){var e=$("[data-code-id='"+a+"']"),t=0;e.get().forEach(function(a){var e=a.style.display;a.style.display="block";var c=a.clientHeight;a.style.display=e,c>t&&(t=c)}),e.find("pre").css("height",t),o.push(a)})}var c="promise",i=$("[data-code-id]").get(),l=i.map(function(a){return a.attributes["data-code-id"].value}).filter(function(a,e,t){return t.indexOf(a)==e});a(),e(),$("[data-code-tablist] [href]").on("click",function(a){var c=$(this).attr("href");e(c.replace("#","")),t(),a.preventDefault()});var o=[]}codeWrap();