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 { minify: htmlMinify } = require('html-minifier')
const CleanCSS = require('clean-css') const CleanCSS = require('clean-css')
const { minify: terserMinify } = require('terser') const { minify: jsMinify } = require('@swc/core')
const { optimize: svgOptimize } = require('svgo') const { optimize: svgOptimize } = require('svgo')
const zlib = require('zlib') const zlib = require('zlib')
const { promisify } = require('util') const { promisify } = require('util')
@ -109,9 +109,10 @@ async function minifyJs (str, data) {
if (isMatch(path, exclude, globOptions)) return str 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) const jsOptions = Object.assign({}, options)
delete jsOptions.enable delete jsOptions.enable
delete jsOptions.output
delete jsOptions.priority delete jsOptions.priority
delete jsOptions.verbose delete jsOptions.verbose
// Old option, retained to avoid crash when upgrading to v4 // Old option, retained to avoid crash when upgrading to v4
@ -120,7 +121,7 @@ async function minifyJs (str, data) {
delete jsOptions.globOptions delete jsOptions.globOptions
try { try {
const { code } = await terserMinify(str, jsOptions) const { code } = await jsMinify(str, jsOptions)
if (verbose) logFn.call(this, str, code, path, 'js') if (verbose) logFn.call(this, str, code, path, 'js')
return code return code
} catch (err) { } catch (err) {

View File

@ -28,7 +28,7 @@
"micromatch": "^4.0.2", "micromatch": "^4.0.2",
"minify-xml": "^3.2.0", "minify-xml": "^3.2.0",
"svgo": "^3.0.0", "svgo": "^3.0.0",
"terser": "^5.3.0" "@swc/core": "^1.3.14"
}, },
"devDependencies": { "devDependencies": {
"hexo": "^6.1.0", "hexo": "^6.1.0",
@ -53,6 +53,7 @@
"clearMocks": true, "clearMocks": true,
"collectCoverage": true, "collectCoverage": true,
"coverageDirectory": "./coverage/", "coverageDirectory": "./coverage/",
"testTimeout": 10000,
"testEnvironment": "node" "testEnvironment": "node"
} }
} }

View File

@ -2,7 +2,7 @@
'use strict' 'use strict'
const Hexo = require('hexo') const Hexo = require('hexo')
const { minify: terserMinify } = require('terser') const { minify: jsMinify } = require('@swc/core')
describe('js', () => { describe('js', () => {
const hexo = new Hexo(__dirname) const hexo = new Hexo(__dirname)
@ -12,7 +12,7 @@ describe('js', () => {
let expected = '' let expected = ''
beforeAll(async () => { beforeAll(async () => {
const { code } = await terserMinify(input, { mangle: true }) const { code } = await jsMinify(input, { mangle: true })
expected = code expected = code
}) })
@ -55,13 +55,13 @@ describe('js', () => {
test('option', async () => { test('option', async () => {
const customOpt = { const customOpt = {
mangle: { mangle: {
properties: true properties: {}
} }
} }
hexo.config.minify.js = customOpt hexo.config.minify.js = customOpt
const result = await j(input, { path }) const result = await j(input, { path })
const { code: expected } = await terserMinify(input, customOpt) const { code: expected } = await jsMinify(input, customOpt)
expect(result).toBe(expected) expect(result).toBe(expected)
}) })
@ -84,7 +84,7 @@ describe('js', () => {
let expected let expected
try { try {
await terserMinify(input, customOpt) await jsMinify(input, customOpt)
} catch (err) { } catch (err) {
expected = err expected = err
} }
@ -118,6 +118,14 @@ describe('js', () => {
test('invalid string', async () => { test('invalid string', async () => {
const invalid = 'console.log("\\");' 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}`)
}) })
}) })