Skip to content

Commit

Permalink
fix: skip empty file
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Jan 2, 2020
1 parent 8d0fdae commit d75b10b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -145,6 +145,7 @@ function minifySvg () {
reject(new Error(err))
}
}
resolve()
})
})
}))
Expand Down Expand Up @@ -176,6 +177,7 @@ function gzipFn () {
reject(new Error(err))
}
}
resolve()
})
})
}))
Expand Down Expand Up @@ -207,6 +209,7 @@ function brotliFn () {
reject(new Error(err))
}
}
resolve()
})
})
}))
Expand Down
51 changes: 51 additions & 0 deletions test/filter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -225,6 +237,12 @@ describe('js', () => {
expect(result).toBeUndefined()
})

test('empty file', () => {
const result = j('', { path })

expect(result).toBeUndefined()
})

test('option', () => {
const customOpt = {
mangle: {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d75b10b

Please sign in to comment.