fix: disable basename by invidual pattern

This commit is contained in:
curbengh 2019-12-26 14:44:42 +00:00
parent e6381ebed3
commit b046d2a09e
No known key found for this signature in database
GPG Key ID: 21EA847C35D6E034
2 changed files with 55 additions and 17 deletions

View File

@ -10,13 +10,23 @@ const gzip = promisify(zlib.gzip)
const br = require('zlib').brotliCompress ? promisify(require('zlib').brotliCompress) : require('iltorb').compress
const micromatch = require('micromatch')
const isMatch = (path, patterns, options) => {
if (path && patterns && patterns.length) {
return micromatch.isMatch(path, patterns, options)
} else {
return false
const isMatch = (path = '', patterns = [], options = {}) => {
if (path && patterns) {
if (path.length && patterns.length) {
if (typeof patterns === 'string') patterns = [patterns]
for (const pattern of patterns) {
// disable basename if a pattern includes a slash
let { basename } = options
// only disable when basename is enabled
basename = basename && !pattern.includes('/')
if (micromatch.isMatch(path, pattern, { ...options, basename })) {
return true
}
}
}
}
return false
}
function verbose (original, minified, path, ext) {
const saved = ((original.length - minified.length) / original.length * 100).toFixed(2)
@ -37,10 +47,6 @@ function minifyHtml (str, data) {
const { path } = data
const { exclude, globOptions } = options
let excludeString = exclude || ''
if (Array.isArray(exclude)) excludeString = exclude.join('')
if (excludeString.includes('/')) globOptions.basename = false
// Return if a path matches exclusion pattern
if (isMatch(path, exclude, globOptions)) return str
@ -58,10 +64,6 @@ async function minifyCss (str, data) {
const { path } = data
const { exclude, globOptions } = options
let excludeString = exclude || ''
if (exclude && Array.isArray(exclude)) excludeString = exclude.join('')
if (excludeString && excludeString.includes('/')) globOptions.basename = false
if (isMatch(path, exclude, globOptions)) return str
try {
@ -81,10 +83,6 @@ function minifyJs (str, data) {
const { path } = data
const { exclude, globOptions } = options
let excludeString = exclude || ''
if (exclude && Array.isArray(exclude)) excludeString = exclude.join('')
if (excludeString && excludeString.includes('/')) globOptions.basename = false
if (isMatch(path, exclude, globOptions)) return str
// Terser doesn't like unsupported options

View File

@ -18,6 +18,7 @@ describe('html', () => {
const input = '<p id="">foo</p>'
const result = h(input, { path: '' })
const expected = Htmlminifier(input, hexo.config.minify.html)
expect(result).toBe(expected)
})
@ -28,6 +29,7 @@ describe('html', () => {
const input = '<p id="">foo</p>'
const result = h(input, { path: '' })
const expected = Htmlminifier(input, customOpt)
expect(result).toBe(input)
expect(result).toBe(expected)
})
@ -38,6 +40,7 @@ describe('html', () => {
const input = '<p id="">foo</p>'
const result = h(input, { path: 'foo/bar.min.html' })
expect(result).toBe(input)
})
@ -47,8 +50,45 @@ describe('html', () => {
const input = '<p id="">foo</p>'
const result = h(input, { path: 'foo/bar.html' })
expect(result).toBe(input)
})
test('exclude - basename is true + slash', () => {
const exclude = ['**/baz', 'bar.html']
const globOptions = { basename: true }
hexo.config.minify.html.exclude = exclude
hexo.config.minify.html.globOptions = globOptions
const input = '<p id="">foo</p>'
const result = h(input, { path: 'foo/bar.html' })
expect(result).toBe(input)
})
test('exclude - basename is false + slash', () => {
const exclude = ['**/baz', 'bar.html']
const globOptions = { basename: false }
hexo.config.minify.html.exclude = exclude
hexo.config.minify.html.globOptions = globOptions
const input = '<p id="">foo</p>'
const result = h(input, { path: 'foo/bar.html' })
const expected = Htmlminifier(input, hexo.config.minify.html)
expect(result).toBe(expected)
})
test('null', () => {
hexo.config.minify.html.exclude = null
hexo.config.minify.html.globOptions = null
const input = '<p id="">foo</p>'
const result = h(input, { path: null })
const expected = Htmlminifier(input, hexo.config.minify.html)
expect(result).toBe(expected)
})
})
describe('css', () => {