This commit is contained in:
Ming Di Leom 2024-03-24 02:39:23 +08:00 committed by GitHub
commit c6b583c9ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 50 additions and 18 deletions

25
.github/workflows/pkg-mgr.yml vendored Normal file
View File

@ -0,0 +1,25 @@
name: Package Manager
on: [push, pull_request]
jobs:
tester:
runs-on: ubuntu-latest
strategy:
matrix:
pkg-mgr: [yarn, pnpm]
fail-fast: false
steps:
- uses: actions/checkout@v3
- name: Use Node.js 16
uses: actions/setup-node@v3
with:
node-version: 16
- name: Install package manager
run: npm install --global ${{ matrix.pkg-mgr }}
- name: Install Dependencies
run: ${{ matrix.pkg-mgr }} install
- name: Test
run: ${{ matrix.pkg-mgr }} run test
env:
CI: true

View File

@ -8,7 +8,6 @@
Yet Another Minifier for Hexo. Minify and compress HTML, JS, CSS, SVG, XML and JSON. [Other files](https://github.com/curbengh/hexo-yam/blob/ba77db0094a7c07ea9f70f010bfc15541d4105ca/index.js#L64) are also compressed. Support gzip and [brotli](https://en.wikipedia.org/wiki/Brotli) [compressions](https://en.wikipedia.org/wiki/HTTP_compression).
## Table of contents
- [Installation](#installation)
@ -101,13 +100,12 @@ minify:
- **priority** - Plugin's priority. Defaults to `10`.
- **verbose** - Verbose output. Defaults to `false`.
- **exclude** - Exclude files. Support [wildcard](http://www.globtester.com/) pattern(s) in a string or array.
- **compress** - Compress options.
- **mangle** - Mangle variable names. Defaults to `true`. Pass an object to specify [mangle options](https://github.com/terser-js/terser#mangle-options).
- **output** - Output options.
- To retain comments, `output: {comments: true}`.
- **compress** - [Compress options](https://swc.rs/docs/configuration/minification#jscminifycompress).
- **mangle** - Mangle variable names. Defaults to `true`. Pass an object to specify [mangle options](https://swc.rs/docs/configuration/minification#jscminifymangle).
- **format** - [Format options](https://swc.rs/docs/configuration/minification#jscminifyformat).
- **globOptions** - See [globbing](#globbing) section.
For more options, see [Terser](https://github.com/terser-js/terser).
For more options, see [SWC](https://swc.rs/docs/configuration/minification).
## SVG

View File

@ -39,7 +39,7 @@ hexo.config.minify.js = Object.assign({
exclude: ['*.min.js'],
compress: {},
mangle: true,
output: {},
format: {},
globOptions: { basename: true }
}, hexo.config.minify.js)

View File

@ -2,7 +2,7 @@
const { minify: htmlMinify } = require('html-minifier-terser')
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,7 +109,7 @@ async function minifyJs (str, data) {
if (isMatch(path, exclude, globOptions)) return str
// Terser doesn't like unsupported options
// SWC/Terser doesn't like unsupported options
const jsOptions = Object.assign({}, options)
delete jsOptions.enable
delete jsOptions.priority
@ -120,7 +120,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": "^7.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
})
@ -24,7 +24,7 @@ describe('js', () => {
exclude: ['*.min.js'],
compress: {},
mangle: true,
output: {},
format: {},
globOptions: { basename: true }
}
}
@ -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}`)
})
})