Skip to content

Commit

Permalink
fix(docs): minify code.js automatically (pouchdb#9051)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alxndrsn authored Jan 9, 2025
1 parent 17a3b3b commit 4711759
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
21 changes: 21 additions & 0 deletions bin/build-site.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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.');
}
});
}

Expand Down
2 changes: 2 additions & 0 deletions docs/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ github:
collections:
guides:
output: true
exclude:
- src
File renamed without changes.
1 change: 0 additions & 1 deletion docs/static/js/code.min.js

This file was deleted.

0 comments on commit 4711759

Please sign in to comment.