Skip to content

Commit

Permalink
feat: replace terser with swc
Browse files Browse the repository at this point in the history
- closes #136
  • Loading branch information
curbengh committed Nov 6, 2022
1 parent b0575e6 commit 2e2258f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
7 changes: 4 additions & 3 deletions lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -53,6 +53,7 @@
"clearMocks": true,
"collectCoverage": true,
"coverageDirectory": "./coverage/",
"testTimeout": 10000,
"testEnvironment": "node"
}
}
20 changes: 14 additions & 6 deletions test/js.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
})

Expand Down Expand Up @@ -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)
})
Expand All @@ -84,7 +84,7 @@ describe('js', () => {

let expected
try {
await terserMinify(input, customOpt)
await jsMinify(input, customOpt)
} catch (err) {
expected = err
}
Expand Down Expand Up @@ -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}`)
})
})

0 comments on commit 2e2258f

Please sign in to comment.