fix: Terser should throw error if there is invalid option

- Other functions also throw error to abort
This commit is contained in:
curbengh 2019-12-26 15:06:32 +00:00
parent b046d2a09e
commit 631503aa1a
No known key found for this signature in database
GPG Key ID: 21EA847C35D6E034
2 changed files with 23 additions and 10 deletions

View File

@ -34,11 +34,6 @@ function verbose (original, minified, path, ext) {
log.log(`${ext}: ${path} [${saved}% saved]`) log.log(`${ext}: ${path} [${saved}% saved]`)
} }
function error (msg) {
const log = this.log || console
log.error(msg)
}
function minifyHtml (str, data) { function minifyHtml (str, data) {
const hexo = this const hexo = this
const options = hexo.config.minify.html const options = hexo.config.minify.html
@ -71,7 +66,7 @@ async function minifyCss (str, data) {
if (options.logger) verbose.call(this, str, styles, path, 'css') if (options.logger) verbose.call(this, str, styles, path, 'css')
return styles return styles
} catch (err) { } catch (err) {
error.call(this, err) throw new Error(err)
} }
} }
@ -93,7 +88,8 @@ function minifyJs (str, data) {
delete jsOptions.exclude delete jsOptions.exclude
delete jsOptions.globOptions delete jsOptions.globOptions
const { code } = Terser.minify(str, jsOptions) const { code, error } = Terser.minify(str, jsOptions)
if (error) throw new Error(error)
if (options.logger) verbose.call(this, str, code, path, 'js') if (options.logger) verbose.call(this, str, code, path, 'js')
return code return code
@ -124,8 +120,8 @@ function minifySvg () {
if (options.logger) verbose.call(this, assetTxt.join().toString(), result.data, path, 'svg') if (options.logger) verbose.call(this, assetTxt.join().toString(), result.data, path, 'svg')
resolve(route.set(path, result.data)) resolve(route.set(path, result.data))
} catch (err) { } catch (err) {
error.call(this, err)
reject(err) reject(err)
throw new Error(err)
} }
} }
}) })
@ -160,8 +156,8 @@ function gzipFn () {
if (options.logger) verbose.call(this, input, result.toString(), path, 'gzip') if (options.logger) verbose.call(this, input, result.toString(), path, 'gzip')
resolve(route.set(path + '.gz', result)) resolve(route.set(path + '.gz', result))
} catch (err) { } catch (err) {
error.call(this, err)
reject(err) reject(err)
throw new Error(err)
} }
} }
}) })
@ -195,8 +191,8 @@ function brotliFn () {
if (options.logger) verbose.call(this, input, result.toString(), path, 'brotli') if (options.logger) verbose.call(this, input, result.toString(), path, 'brotli')
resolve(route.set(path + '.br', result)) resolve(route.set(path + '.br', result))
} catch (err) { } catch (err) {
error.call(this, err)
reject(err) reject(err)
throw new Error(err)
} }
} }
}) })

View File

@ -176,6 +176,23 @@ describe('js', () => {
expect(result).toBe(code) expect(result).toBe(code)
}) })
test('option - invalid', () => {
const customOpt = {
mangle: {
foo: 'bar'
}
}
hexo.config.minify.js = customOpt
const input = 'var o = { "foo": 1, bar: 3 };'
const { error } = Terser.minify(input, customOpt)
try {
j(input, { path: '' })
} catch (err) {
expect(err.message).toContain(error.message)
}
})
test('exclude - *.min.js', () => { test('exclude - *.min.js', () => {
const input = 'var o = { "foo": 1, bar: 3 };' const input = 'var o = { "foo": 1, bar: 3 };'
const result = j(input, { path: 'foo/bar.min.js' }) const result = j(input, { path: 'foo/bar.min.js' })