Switched uglify for terser + bugfixes

- Uglify was really slow and clunky, so replaced with terser which is fast and simple
- Ensured test cleanup works properly so we can be sure about the state of files during tests
- Changed the output from being the absolute path to just the "dest" value, as that is much more useful as absolute paths include local machine path names
- Fixed async/await issue that got the whole thing working 100%
This commit is contained in:
Hannah Wolfe 2021-10-19 13:05:23 +01:00
parent 03b1e9c3bd
commit 19383f27f7
3 changed files with 9 additions and 10 deletions

View File

@ -1,8 +1,8 @@
const errors = require('@tryghost/errors');
const debug = require('@tryghost/debug')('affsfdfsdfsdfsdffsdsdfsd');
const debug = require('@tryghost/debug')('minifier');
const tpl = require('@tryghost/tpl');
const csso = require('csso');
const uglify = require('uglify-js');
const terser = require('terser');
const glob = require('tiny-glob');
const path = require('path');
const fs = require('fs').promises;
@ -49,7 +49,7 @@ class Minifier {
}
async minifyCSS(contents) {
const result = csso.minify(contents);
const result = await csso.minify(contents);
if (result && result.css) {
return result.css;
}
@ -57,11 +57,10 @@ class Minifier {
}
async minifyJS(contents) {
const result = uglify.minify(contents);
const result = await terser.minify(contents);
if (result && result.code) {
return result.code;
}
return null;
}
@ -112,7 +111,7 @@ class Minifier {
const result = await this.writeFile(minifiedContents, dest);
if (result) {
minifiedFiles.push(result);
minifiedFiles.push(dest);
}
}

View File

@ -29,7 +29,7 @@
"@tryghost/errors": "^0.2.16",
"@tryghost/tpl": "^0.1.7",
"csso": "4.2.0",
"tiny-glob": "^0.2.9",
"uglify-js": "3.14.2"
"terser": "^5.9.0",
"tiny-glob": "^0.2.9"
}
}

View File

@ -15,12 +15,12 @@ describe('Minifier', function () {
minifier = new Minifier({
src: path.join(__dirname, 'fixtures', 'basic-cards'),
dest: path.join(os.tmpdir(), 'minifier-tests')
dest: testDir
});
});
after(async function () {
await fs.rmdir(testDir);
await fs.rmdir(testDir, {recursive: true, force: true});
});
describe('getMatchingFiles expands globs correctly', function () {