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 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 () {

View File

@ -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')
})
})