feat: replace terser with swc

- closes #136
This commit is contained in:
MDLeom 2022-11-06 06:35:58 +00:00
parent b0575e6fdc
commit 2e2258f6de
No known key found for this signature in database
GPG Key ID: 06C236E63CBC68AA
3 changed files with 20 additions and 10 deletions

View File

@ -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) {

View File

@ -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"
}
}

View File

@ -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}`)
})
})