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). 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 ## Table of contents
- [Installation](#installation) - [Installation](#installation)
@ -101,13 +100,12 @@ minify:
- **priority** - Plugin's priority. Defaults to `10`. - **priority** - Plugin's priority. Defaults to `10`.
- **verbose** - Verbose output. Defaults to `false`. - **verbose** - Verbose output. Defaults to `false`.
- **exclude** - Exclude files. Support [wildcard](http://www.globtester.com/) pattern(s) in a string or array. - **exclude** - Exclude files. Support [wildcard](http://www.globtester.com/) pattern(s) in a string or array.
- **compress** - Compress options. - **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://github.com/terser-js/terser#mangle-options). - **mangle** - Mangle variable names. Defaults to `true`. Pass an object to specify [mangle options](https://swc.rs/docs/configuration/minification#jscminifymangle).
- **output** - Output options. - **format** - [Format options](https://swc.rs/docs/configuration/minification#jscminifyformat).
- To retain comments, `output: {comments: true}`.
- **globOptions** - See [globbing](#globbing) section. - **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 ## SVG

View File

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

View File

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