diff --git a/.github/workflows/pkg-mgr.yml b/.github/workflows/pkg-mgr.yml new file mode 100644 index 0000000..56faa79 --- /dev/null +++ b/.github/workflows/pkg-mgr.yml @@ -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 diff --git a/README.md b/README.md index 73cb0f4..b17defe 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 318533a..5000060 100644 --- a/index.js +++ b/index.js @@ -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) diff --git a/lib/filter.js b/lib/filter.js index 48b3df4..e7d5874 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -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) { diff --git a/package.json b/package.json index 0807395..af1d653 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/js.test.js b/test/js.test.js index 706072c..47b3dfb 100644 --- a/test/js.test.js +++ b/test/js.test.js @@ -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}`) }) })