diff --git a/lib/filter.js b/lib/filter.js index ac09d71..f4cc97d 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -2,7 +2,7 @@ const { minify: htmlMinify } = require('html-minifier') const CleanCSS = require('clean-css') -const { minify: terserMinify } = require('terser') +const { minify: jsMinify } = require('@swc/core') const { optimize: svgOptimize } = require('svgo') const zlib = require('zlib') const { promisify } = require('util') @@ -109,9 +109,10 @@ async function minifyJs (str, data) { if (isMatch(path, exclude, globOptions)) return str - // Terser doesn't like unsupported options + // Terser/SWC doesn't like unsupported options const jsOptions = Object.assign({}, options) delete jsOptions.enable + delete jsOptions.output delete jsOptions.priority delete jsOptions.verbose // Old option, retained to avoid crash when upgrading to v4 @@ -120,7 +121,7 @@ async function minifyJs (str, data) { delete jsOptions.globOptions try { - const { code } = await terserMinify(str, jsOptions) + const { code } = await jsMinify(str, jsOptions) if (verbose) logFn.call(this, str, code, path, 'js') return code } catch (err) { diff --git a/package.json b/package.json index 28e794e..248f531 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "micromatch": "^4.0.2", "minify-xml": "^3.2.0", "svgo": "^3.0.0", - "terser": "^5.3.0" + "@swc/core": "^1.3.14" }, "devDependencies": { "hexo": "^6.1.0", @@ -53,6 +53,7 @@ "clearMocks": true, "collectCoverage": true, "coverageDirectory": "./coverage/", + "testTimeout": 10000, "testEnvironment": "node" } } diff --git a/test/js.test.js b/test/js.test.js index 706072c..e778f92 100644 --- a/test/js.test.js +++ b/test/js.test.js @@ -2,7 +2,7 @@ 'use strict' const Hexo = require('hexo') -const { minify: terserMinify } = require('terser') +const { minify: jsMinify } = require('@swc/core') describe('js', () => { const hexo = new Hexo(__dirname) @@ -12,7 +12,7 @@ describe('js', () => { let expected = '' beforeAll(async () => { - const { code } = await terserMinify(input, { mangle: true }) + const { code } = await jsMinify(input, { mangle: true }) expected = code }) @@ -55,13 +55,13 @@ describe('js', () => { test('option', async () => { const customOpt = { mangle: { - properties: true + properties: {} } } hexo.config.minify.js = customOpt const result = await j(input, { path }) - const { code: expected } = await terserMinify(input, customOpt) + const { code: expected } = await jsMinify(input, customOpt) expect(result).toBe(expected) }) @@ -84,7 +84,7 @@ describe('js', () => { let expected try { - await terserMinify(input, customOpt) + await jsMinify(input, customOpt) } catch (err) { expected = err } @@ -118,6 +118,14 @@ describe('js', () => { test('invalid string', async () => { const invalid = 'console.log("\\");' - await expect(j(invalid, { path })).rejects.toThrow(`Path: ${path}\nSyntaxError`) + let expected + try { + await jsMinify(invalid) + } catch (err) { + expected = err + } + + expect(expected).toBeDefined() + await expect(j(invalid, { path })).rejects.toThrow(`Path: ${path}\n${expected}`) }) })