From 16cdf467d90c53e9d222aa9395f4c780db618d0e Mon Sep 17 00:00:00 2001 From: MDLeom <43627182+curbengh@users.noreply.github.com> Date: Sat, 5 Sep 2020 10:56:33 +0000 Subject: [PATCH] fix(js): compatibility with terser v5 - https://github.com/terser/terser/blob/master/CHANGELOG.md#v500-beta0 --- lib/filter.js | 16 ++++++------ test/js.test.js | 66 +++++++++++++++++++++++++++---------------------- 2 files changed, 45 insertions(+), 37 deletions(-) diff --git a/lib/filter.js b/lib/filter.js index 6eb86a7..f8e738c 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -2,7 +2,7 @@ const Htmlminifier = require('html-minifier').minify const CleanCSS = require('clean-css') -const Terser = require('terser') +const { minify: terserMinify } = require('terser') const Svgo = require('svgo') const zlib = require('zlib') const { promisify } = require('util') @@ -99,7 +99,7 @@ async function minifyCss (str, data) { } } -function minifyJs (str, data) { +async function minifyJs (str, data) { const hexo = this const options = hexo.config.minify.js if (options.enable === false || !str) return @@ -119,11 +119,13 @@ function minifyJs (str, data) { delete jsOptions.exclude delete jsOptions.globOptions - const { code, error } = Terser.minify(str, jsOptions) - if (error) throw new Error(error) - if (verbose) logFn.call(this, str, code, path, 'js') - - return code + try { + const { code } = await terserMinify(str, jsOptions) + if (verbose) logFn.call(this, str, code, path, 'js') + return code + } catch (err) { + throw new Error(err) + } } function minifySvg () { diff --git a/test/js.test.js b/test/js.test.js index 20bef6e..857593e 100644 --- a/test/js.test.js +++ b/test/js.test.js @@ -6,39 +6,44 @@ const hexo = new Hexo(__dirname) global.hexo = hexo const { jsDefault } = require('../index') const j = require('../lib/filter').minifyJs.bind(hexo) -const Terser = require('terser') +const { minify: terserMinify } = require('terser') const input = 'var o = { "foo": 1, bar: 3 };' const path = 'foo.js' -const expected = Terser.minify(input, { mangle: jsDefault.mangle }).code +let expected = '' describe('js', () => { - beforeEach(() => { + beforeAll(async () => { + const { code } = await terserMinify(input, { mangle: jsDefault.mangle }) + expected = code + }) + + beforeEach(async () => { hexo.config.minify.js = Object.assign({}, jsDefault) }) - test('default', () => { - const result = j(input, { path }) + test('default', async () => { + const result = await j(input, { path }) expect(result).toBeDefined() expect(expected).toBeDefined() expect(result).toBe(expected) }) - test('disable', () => { + test('disable', async () => { hexo.config.minify.js.enable = false - const result = j(input, { path }) + const result = await j(input, { path }) expect(result).toBeUndefined() }) - test('empty file', () => { - const result = j('', { path }) + test('empty file', async () => { + const result = await j('', { path }) expect(result).toBeUndefined() }) - test('option', () => { + test('option', async () => { const customOpt = { mangle: { properties: true @@ -46,21 +51,21 @@ describe('js', () => { } hexo.config.minify.js = customOpt - const result = j(input, { path }) - const expected = Terser.minify(input, customOpt).code + const result = await j(input, { path }) + const { code: expected } = await terserMinify(input, customOpt) expect(result).toBe(expected) }) - test('option - verbose', () => { + test('option - verbose', async () => { hexo.config.minify.js.verbose = true hexo.log.log = jest.fn() - j(input, { path }) + await j(input, { path }) expect(hexo.log.log.mock.calls[0][0]).toContain(`js: ${path}`) }) - test('option - invalid', () => { + test('option - invalid', async () => { const customOpt = { mangle: { foo: 'bar' @@ -68,41 +73,42 @@ describe('js', () => { } hexo.config.minify.js = customOpt - const { error } = Terser.minify(input, customOpt) + let error + try { + await terserMinify(input, customOpt) + } catch (err) { + error = err.message + } - expect(error.message).toBeDefined() - expect(() => { - j(input, { path }) - }).toThrow(error.message) + expect(error).toBeDefined() + await expect(j(input, { path })).rejects.toThrow(error) }) - test('exclude - *.min.js', () => { - const result = j(input, { path: 'foo/bar.min.js' }) + test('exclude - *.min.js', async () => { + const result = await j(input, { path: 'foo/bar.min.js' }) expect(result).toBe(input) }) - test('exclude - basename', () => { + test('exclude - basename', async () => { const exclude = '*baz.js' hexo.config.minify.js.exclude = exclude - const result = j(input, { path: 'foo/barbaz.js' }) + const result = await j(input, { path: 'foo/barbaz.js' }) expect(result).toBe(input) }) - test('exclude - slash in pattern', () => { + test('exclude - slash in pattern', async () => { const exclude = '**/lectus/**/*.js' hexo.config.minify.js.exclude = exclude - const result = j(input, { path: 'eleifend/lectus/nullam/dapibus/netus.js' }) + const result = await j(input, { path: 'eleifend/lectus/nullam/dapibus/netus.js' }) expect(result).toBe(input) }) - test('invalid string', () => { + test('invalid string', async () => { const invalid = 'console.log("\\");' - expect(() => { - j(invalid, { path }) - }).toThrow('SyntaxError') + await expect(j(invalid, { path })).rejects.toThrow('SyntaxError') }) })