From 2e2258f6de5135fe29721c3e655bd53c5cac5ae2 Mon Sep 17 00:00:00 2001 From: MDLeom <43627182+curbengh@users.noreply.github.com> Date: Sun, 6 Nov 2022 06:35:58 +0000 Subject: [PATCH 1/3] feat: replace terser with swc - closes #136 --- lib/filter.js | 7 ++++--- package.json | 3 ++- test/js.test.js | 20 ++++++++++++++------ 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/filter.js b/lib/filter.js index ac09d71..f4cc97d 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -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') @@ -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 @@ -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) { diff --git a/package.json b/package.json index 28e794e..248f531 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": "^6.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..e778f92 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 }) @@ -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}`) }) }) From 591b1d8713cca4fcab5b0e707a515c8fc68e9d35 Mon Sep 17 00:00:00 2001 From: MDLeom <43627182+curbengh@users.noreply.github.com> Date: Sun, 6 Nov 2022 06:47:37 +0000 Subject: [PATCH 2/3] fix(js): rename "output" option to "format" - https://swc.rs/docs/configuration/minification#jscminifyformat - https://terser.org/docs/api-reference.html#format-options --- README.md | 10 ++++------ index.js | 2 +- lib/filter.js | 3 +-- test/js.test.js | 2 +- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9c274af..896e3e8 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,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) @@ -103,13 +102,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 f4cc97d..11600a4 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -109,10 +109,9 @@ async function minifyJs (str, data) { if (isMatch(path, exclude, globOptions)) return str - // Terser/SWC doesn't like unsupported options + // SWC/Terser 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 diff --git a/test/js.test.js b/test/js.test.js index e778f92..47b3dfb 100644 --- a/test/js.test.js +++ b/test/js.test.js @@ -24,7 +24,7 @@ describe('js', () => { exclude: ['*.min.js'], compress: {}, mangle: true, - output: {}, + format: {}, globOptions: { basename: true } } } From 85b8c7fa495384534c90e0d49a5401fc547cc4d0 Mon Sep 17 00:00:00 2001 From: MDLeom <43627182+curbengh@users.noreply.github.com> Date: Mon, 7 Nov 2022 08:53:21 +0000 Subject: [PATCH 3/3] ci: add test for yarn & pnpm --- .github/workflows/pkg-mgr.yml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .github/workflows/pkg-mgr.yml 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