feat: add tests of minifyCssWithMap and minifyJsWithMap

This commit is contained in:
where where 2025-11-03 21:37:33 +08:00
parent da397b4504
commit 7907bfa6a4
3 changed files with 49 additions and 1 deletions

View File

@ -40,7 +40,10 @@ function minifyCssWithMap() {
/** @type {{ exclude: string[] }} */
const { exclude, globOptions, verbose } = options
const include = ['*.css', ...exclude.map(x => `!${x}`)]
const cleanCSS = new CleanCSS(options)
const cleanCSS = new CleanCSS({
...options,
sourceMap: true
})
return Promise.all((match(routeList, include, globOptions)).map(path => {
return new Promise((/** @type {(value: void) => void} */ resolve, reject) => {

View File

@ -7,6 +7,7 @@ const CleanCSS = require('clean-css')
describe('css', () => {
const hexo = new Hexo(__dirname)
const c = require('../lib/css').minifyCss.bind(hexo)
const cm = require('../lib/css').minifyCssWithMap.bind(hexo)
const input = 'foo { bar: baz; } foo { aaa: bbb; }'
const path = 'foo.css'
@ -20,6 +21,7 @@ describe('css', () => {
globOptions: { basename: true }
}
}
hexo.route.set(path, input)
})
test('default', async () => {
@ -29,12 +31,32 @@ describe('css', () => {
expect(result).toBe(styles)
})
test('default with map', async () => {
await cm()
const { styles } = await new CleanCSS(hexo.config.minify.css).minify(input)
const output = hexo.route.get(path)
let result = ''
output.on('data', (chunk) => (result += chunk))
output.on('end', () => {
expect(result).toBe(styles + '\n/*# sourceMappingURL=foo.css.map */')
})
})
test('empty file', async () => {
const result = await c('', { path })
expect(result).toBe('')
})
test('empty file with map', async () => {
hexo.route.set(path, '')
const result = await cm()
expect(result).toBeDefined()
expect(result[0]).toBeUndefined()
})
test('option', async () => {
const customOpt = {
level: {

View File

@ -7,6 +7,7 @@ const { minify: terserMinify } = require('terser')
describe('js', () => {
const hexo = new Hexo(__dirname)
const j = require('../lib/js').minifyJs.bind(hexo)
const jm = require('../lib/js').minifyJsWithMap.bind(hexo)
const input = 'var o = { "foo": 1, bar: 3 };'
const path = 'foo.js'
let expected = ''
@ -28,6 +29,7 @@ describe('js', () => {
globOptions: { basename: true }
}
}
hexo.route.set(path, input)
})
test('default', async () => {
@ -38,12 +40,33 @@ describe('js', () => {
expect(result).toBe(expected)
})
test('default with map', async () => {
await jm()
const output = hexo.route.get(path)
let result = ''
output.on('data', (chunk) => (result += chunk))
output.on('end', () => {
expect(result).toBeDefined()
expect(expected).toBeDefined()
expect(result).toBe(expected + '\n//# sourceMappingURL=foo.js.map')
})
})
test('empty file', async () => {
const result = await j('', { path })
expect(result).toBe('')
})
test('empty file with map', async () => {
hexo.route.set(path, '')
const result = await jm()
expect(result).toBeDefined()
expect(result[0]).toBeUndefined()
})
test('option', async () => {
const customOpt = {
mangle: {