fix: skip empty file

This commit is contained in:
curbengh 2020-01-02 12:49:29 +00:00
parent 8d0fdaeb80
commit d75b10b64b
No known key found for this signature in database
GPG Key ID: 21EA847C35D6E034
2 changed files with 57 additions and 3 deletions

View File

@ -61,7 +61,7 @@ function logFn (original, minified, path, ext) {
function minifyHtml (str, data) {
const hexo = this
const options = hexo.config.minify.html
if (options.enable === false) return
if (options.enable === false || !str) return
const { path } = data
const { exclude, globOptions, verbose } = options
@ -78,7 +78,7 @@ function minifyHtml (str, data) {
async function minifyCss (str, data) {
const hexo = this
const options = hexo.config.minify.css
if (options.enable === false) return
if (options.enable === false || !str) return
const { path } = data
const { exclude, globOptions, verbose } = options
@ -97,7 +97,7 @@ async function minifyCss (str, data) {
function minifyJs (str, data) {
const hexo = this
const options = hexo.config.minify.js
if (options.enable === false) return
if (options.enable === false || !str) return
const { path } = data
const { exclude, globOptions, verbose } = options
@ -145,6 +145,7 @@ function minifySvg () {
reject(new Error(err))
}
}
resolve()
})
})
}))
@ -176,6 +177,7 @@ function gzipFn () {
reject(new Error(err))
}
}
resolve()
})
})
}))
@ -207,6 +209,7 @@ function brotliFn () {
reject(new Error(err))
}
}
resolve()
})
})
}))

View File

@ -32,6 +32,12 @@ describe('html', () => {
expect(result).toBeUndefined()
})
test('empty file', () => {
const result = h('', { path })
expect(result).toBeUndefined()
})
test('option', () => {
const customOpt = { removeEmptyAttributes: false }
hexo.config.minify.html = customOpt
@ -128,6 +134,12 @@ describe('css', () => {
expect(result).toBeUndefined()
})
test('empty file', async () => {
const result = await c('', { path })
expect(result).toBeUndefined()
})
test('option', async () => {
const customOpt = {
level: {
@ -225,6 +237,12 @@ describe('js', () => {
expect(result).toBeUndefined()
})
test('empty file', () => {
const result = j('', { path })
expect(result).toBeUndefined()
})
test('option', () => {
const customOpt = {
mangle: {
@ -322,6 +340,15 @@ describe('svg', () => {
expect(result).toBeUndefined()
})
test('empty file', async () => {
hexo.route.set(path, '')
const result = await s()
// empty file resolves to an array of undefined
expect(result).toBeDefined()
expect(result[0]).toBeUndefined()
})
test('option', async () => {
const customOpt = [{ cleanupIDs: false }]
hexo.config.minify.svg.plugins = customOpt
@ -530,6 +557,18 @@ describe('gzip', () => {
expect(result).toBeUndefined()
})
test('empty file', async () => {
hexo.route.set(path, '')
const routeList = hexo.route.list()
expect(routeList).not.toContain(path.concat('.gz'))
const result = await g()
// empty file resolves to an array of undefined
expect(result).toBeDefined()
expect(result[0]).toBeUndefined()
})
test('option', async () => {
const customOpt = {
level: 1
@ -747,6 +786,18 @@ describe('brotli', () => {
expect(result).toBeUndefined()
})
test('empty file', async () => {
hexo.route.set(path, '')
const routeList = hexo.route.list()
expect(routeList).not.toContain(path.concat('.br'))
const result = await b()
// empty file resolves to an array of undefined
expect(result).toBeDefined()
expect(result[0]).toBeUndefined()
})
test('option', async () => {
const level = 1
hexo.config.minify.brotli.level = level