fix(js): compatibility with terser v5

- https://github.com/terser/terser/blob/master/CHANGELOG.md#v500-beta0
This commit is contained in:
MDLeom 2020-09-05 10:56:33 +00:00
parent 3de777904f
commit 16cdf467d9
No known key found for this signature in database
GPG Key ID: 06C236E63CBC68AA
2 changed files with 45 additions and 37 deletions

View File

@ -2,7 +2,7 @@
const Htmlminifier = require('html-minifier').minify const Htmlminifier = require('html-minifier').minify
const CleanCSS = require('clean-css') const CleanCSS = require('clean-css')
const Terser = require('terser') const { minify: terserMinify } = require('terser')
const Svgo = require('svgo') const Svgo = require('svgo')
const zlib = require('zlib') const zlib = require('zlib')
const { promisify } = require('util') 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 hexo = this
const options = hexo.config.minify.js const options = hexo.config.minify.js
if (options.enable === false || !str) return if (options.enable === false || !str) return
@ -119,11 +119,13 @@ function minifyJs (str, data) {
delete jsOptions.exclude delete jsOptions.exclude
delete jsOptions.globOptions delete jsOptions.globOptions
const { code, error } = Terser.minify(str, jsOptions) try {
if (error) throw new Error(error) const { code } = await terserMinify(str, jsOptions)
if (verbose) logFn.call(this, str, code, path, 'js') if (verbose) logFn.call(this, str, code, path, 'js')
return code return code
} catch (err) {
throw new Error(err)
}
} }
function minifySvg () { function minifySvg () {

View File

@ -6,39 +6,44 @@ const hexo = new Hexo(__dirname)
global.hexo = hexo global.hexo = hexo
const { jsDefault } = require('../index') const { jsDefault } = require('../index')
const j = require('../lib/filter').minifyJs.bind(hexo) 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 input = 'var o = { "foo": 1, bar: 3 };'
const path = 'foo.js' const path = 'foo.js'
const expected = Terser.minify(input, { mangle: jsDefault.mangle }).code let expected = ''
describe('js', () => { describe('js', () => {
beforeEach(() => { beforeAll(async () => {
const { code } = await terserMinify(input, { mangle: jsDefault.mangle })
expected = code
})
beforeEach(async () => {
hexo.config.minify.js = Object.assign({}, jsDefault) hexo.config.minify.js = Object.assign({}, jsDefault)
}) })
test('default', () => { test('default', async () => {
const result = j(input, { path }) const result = await j(input, { path })
expect(result).toBeDefined() expect(result).toBeDefined()
expect(expected).toBeDefined() expect(expected).toBeDefined()
expect(result).toBe(expected) expect(result).toBe(expected)
}) })
test('disable', () => { test('disable', async () => {
hexo.config.minify.js.enable = false hexo.config.minify.js.enable = false
const result = j(input, { path }) const result = await j(input, { path })
expect(result).toBeUndefined() expect(result).toBeUndefined()
}) })
test('empty file', () => { test('empty file', async () => {
const result = j('', { path }) const result = await j('', { path })
expect(result).toBeUndefined() expect(result).toBeUndefined()
}) })
test('option', () => { test('option', async () => {
const customOpt = { const customOpt = {
mangle: { mangle: {
properties: true properties: true
@ -46,21 +51,21 @@ describe('js', () => {
} }
hexo.config.minify.js = customOpt hexo.config.minify.js = customOpt
const result = j(input, { path }) const result = await j(input, { path })
const expected = Terser.minify(input, customOpt).code const { code: expected } = await terserMinify(input, customOpt)
expect(result).toBe(expected) expect(result).toBe(expected)
}) })
test('option - verbose', () => { test('option - verbose', async () => {
hexo.config.minify.js.verbose = true hexo.config.minify.js.verbose = true
hexo.log.log = jest.fn() hexo.log.log = jest.fn()
j(input, { path }) await j(input, { path })
expect(hexo.log.log.mock.calls[0][0]).toContain(`js: ${path}`) expect(hexo.log.log.mock.calls[0][0]).toContain(`js: ${path}`)
}) })
test('option - invalid', () => { test('option - invalid', async () => {
const customOpt = { const customOpt = {
mangle: { mangle: {
foo: 'bar' foo: 'bar'
@ -68,41 +73,42 @@ describe('js', () => {
} }
hexo.config.minify.js = customOpt 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(error).toBeDefined()
expect(() => { await expect(j(input, { path })).rejects.toThrow(error)
j(input, { path })
}).toThrow(error.message)
}) })
test('exclude - *.min.js', () => { test('exclude - *.min.js', async () => {
const result = j(input, { path: 'foo/bar.min.js' }) const result = await j(input, { path: 'foo/bar.min.js' })
expect(result).toBe(input) expect(result).toBe(input)
}) })
test('exclude - basename', () => { test('exclude - basename', async () => {
const exclude = '*baz.js' const exclude = '*baz.js'
hexo.config.minify.js.exclude = exclude 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) expect(result).toBe(input)
}) })
test('exclude - slash in pattern', () => { test('exclude - slash in pattern', async () => {
const exclude = '**/lectus/**/*.js' const exclude = '**/lectus/**/*.js'
hexo.config.minify.js.exclude = exclude 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) expect(result).toBe(input)
}) })
test('invalid string', () => { test('invalid string', async () => {
const invalid = 'console.log("\\");' const invalid = 'console.log("\\");'
expect(() => { await expect(j(invalid, { path })).rejects.toThrow('SyntaxError')
j(invalid, { path })
}).toThrow('SyntaxError')
}) })
}) })