From 7907bfa6a44de710509ac19d9e64bd7d6d58a841 Mon Sep 17 00:00:00 2001 From: where where Date: Mon, 3 Nov 2025 21:37:33 +0800 Subject: [PATCH] feat: add tests of minifyCssWithMap and minifyJsWithMap --- lib/css.js | 5 ++++- test/css.test.js | 22 ++++++++++++++++++++++ test/js.test.js | 23 +++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/css.js b/lib/css.js index 6dffe79..affd328 100644 --- a/lib/css.js +++ b/lib/css.js @@ -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) => { diff --git a/test/css.test.js b/test/css.test.js index e277d8a..c4d2dc0 100644 --- a/test/css.test.js +++ b/test/css.test.js @@ -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: { diff --git a/test/js.test.js b/test/js.test.js index adba3ce..59bd329 100644 --- a/test/js.test.js +++ b/test/js.test.js @@ -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: {