├── .eslintignore ├── .eslintrc ├── .github ├── dependabot.yml └── workflows │ └── ci.yml ├── .gitignore ├── .mocharc.yml ├── LICENSE ├── README.md ├── index.js ├── lib └── filter.js ├── package.json └── test ├── .eslintrc └── index.js /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules/ -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "hexo", 3 | "root": true 4 | } -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: npm 4 | directory: "/" 5 | schedule: 6 | interval: monthly 7 | - package-ecosystem: github-actions 8 | directory: "/" 9 | schedule: 10 | interval: monthly 11 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - "master" 7 | pull_request: 8 | 9 | env: 10 | CI: true 11 | default_node_version: "14" 12 | 13 | jobs: 14 | test: 15 | runs-on: ${{ matrix.os }} 16 | strategy: 17 | matrix: 18 | os: [ubuntu-latest, windows-latest, macos-latest] 19 | node-version: ["14.x", "16.x", "18.x"] 20 | fail-fast: false 21 | steps: 22 | - uses: actions/checkout@v4 23 | - name: Use Node.js ${{ matrix.node-version }} 24 | uses: actions/setup-node@v4 25 | with: 26 | node-version: ${{ matrix.node-version }} 27 | cache: npm 28 | cache-dependency-path: "package.json" 29 | 30 | - name: Install Dependencies 31 | run: npm install 32 | - name: Test 33 | run: npm run test 34 | 35 | coverage: 36 | runs-on: ubuntu-latest 37 | steps: 38 | - uses: actions/checkout@v4 39 | - name: Use Node.js ${{env.default_node_version}} 40 | uses: actions/setup-node@v4 41 | with: 42 | node-version: ${{env.default_node_version}} 43 | cache: npm 44 | cache-dependency-path: "package.json" 45 | 46 | - name: Install Dependencies 47 | run: npm install 48 | - name: Coverage 49 | run: npm run test-cov 50 | 51 | - name: Coveralls 52 | uses: coverallsapp/github-action@v2 53 | with: 54 | github-token: ${{ secrets.github_token }} 55 | 56 | lint: 57 | runs-on: ubuntu-latest 58 | steps: 59 | - uses: actions/checkout@v4 60 | - name: Use Node.js ${{env.default_node_version}} 61 | uses: actions/setup-node@v4 62 | with: 63 | node-version: ${{env.default_node_version}} 64 | cache: npm 65 | cache-dependency-path: "package.json" 66 | 67 | - name: Install Dependencies 68 | run: npm install 69 | - name: Lint 70 | run: | 71 | npm run eslint 72 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | tmp/ 4 | *.log 5 | .idea/ 6 | .nyc_output/ 7 | coverage/ 8 | -------------------------------------------------------------------------------- /.mocharc.yml: -------------------------------------------------------------------------------- 1 | colors: true 2 | reporter: spec 3 | ui: bdd 4 | full-trace: true 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Tommy Chen 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hexo-uglify 2 | 3 | [![NPM version](https://badge.fury.io/js/hexo-uglify.svg)](https://www.npmjs.com/package/hexo-uglify) 4 | [![CI](https://github.com/hexojs/hexo-uglify/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/hexojs/hexo-uglify/actions/workflows/ci.yml) 5 | [![Coverage Status](https://coveralls.io/repos/github/hexojs/hexo-uglify/badge.svg?branch=master)](https://coveralls.io/github/hexojs/hexo-uglify?branch=master) 6 | 7 | Minify JavaScript files with [Terser](https://www.npmjs.com/package/terser). 8 | 9 | ## Installation 10 | 11 | ```bash 12 | $ npm install hexo-uglify --save 13 | ``` 14 | 15 | ## Options 16 | 17 | ```yaml 18 | uglify: 19 | exclude: 20 | - "*.min.js" 21 | mangle: true 22 | # output: 23 | # compress: 24 | ``` 25 | 26 | - **exclude**: Exclude files. Use [glob expressions](https://github.com/micromatch/micromatch#extended-globbing) for path matching. 27 | - **mangle**: Mangle file names. 28 | - **output**: Output options. 29 | - **compress**: Compress options. 30 | 31 | Refer to the [docs] for more options and details. 32 | 33 | [docs]: https://github.com/terser/terser#minify-options 34 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /* global hexo */ 4 | 5 | hexo.config.uglify = Object.assign({ 6 | exclude: ['*.min.js'], 7 | mangle: true, 8 | output: {}, 9 | compress: {} 10 | }, hexo.config.uglify); 11 | 12 | hexo.extend.filter.register('after_render:js', require('./lib/filter')); 13 | -------------------------------------------------------------------------------- /lib/filter.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const { minify } = require('terser'); 4 | const { isMatch } = require('micromatch'); 5 | 6 | module.exports = async function(str, data) { 7 | const options = this.config.uglify; 8 | 9 | const { path } = data; 10 | const { exclude } = options; 11 | 12 | if (path && exclude && exclude.length) { 13 | if (isMatch(path, exclude, { basename: true })) return str; 14 | } 15 | 16 | // Remove unsupported options for Terser 17 | const jsOptions = Object.assign({}, options); 18 | delete jsOptions.exclude; 19 | delete jsOptions.es6; 20 | 21 | const { code } = await minify(str, jsOptions); 22 | 23 | return code; 24 | }; 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hexo-uglify", 3 | "version": "2.0.0", 4 | "description": "Minify JavaScript files with Terser.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha test", 8 | "test-cov": "c8 --reporter=lcovonly mocha test", 9 | "eslint": "eslint ." 10 | }, 11 | "directories": { 12 | "lib": "./lib" 13 | }, 14 | "files": [ 15 | "lib/", 16 | "index.js" 17 | ], 18 | "engines": { 19 | "node": ">= 12" 20 | }, 21 | "repository": "hexojs/hexo-uglify", 22 | "keywords": [ 23 | "hexo", 24 | "filter", 25 | "terser" 26 | ], 27 | "author": "Tommy Chen (http://zespia.tw)", 28 | "maintainers": [ 29 | "Abner Chou (http://abnerchou.me)" 30 | ], 31 | "license": "MIT", 32 | "dependencies": { 33 | "micromatch": "^4.0.4", 34 | "terser": "^5.10.0" 35 | }, 36 | "devDependencies": { 37 | "c8": "^9.1.0", 38 | "chai": "^4.3.4", 39 | "eslint": "^8.3.0", 40 | "eslint-config-hexo": "5.0.0", 41 | "hexo": "^7.0.0", 42 | "mocha": "^10.0.0", 43 | "nyc": "^15.1.0" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /test/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "hexo/test" 3 | } 4 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('chai').should(); 4 | 5 | describe('hexo-uglify', () => { 6 | const Hexo = require('hexo'); 7 | const hexo = new Hexo(__dirname, { silent: true }); 8 | const f = require('../lib/filter').bind(hexo); 9 | const defaultCfg = JSON.parse(JSON.stringify(Object.assign(hexo.config, { 10 | uglify: { 11 | mangle: true, 12 | output: {}, 13 | compress: {}, 14 | exclude: '*.min.js' 15 | } 16 | }))); 17 | const es5 = ` 18 | var x = { 19 | baz_: 0, 20 | foo_: 1, 21 | calc: function() { 22 | return this.foo_ + this.baz_; 23 | } 24 | }; 25 | x.bar_ = 2; 26 | x["baz_"] = 3; 27 | console.log(x.calc());`; 28 | 29 | beforeEach(() => { 30 | hexo.config = JSON.parse(JSON.stringify(defaultCfg)); 31 | }); 32 | 33 | it('es5 - terser (default options)', async () => { 34 | const result = await f(es5, { path: 'source/test.js' }); 35 | 36 | result.length.should.below(es5.length); 37 | }); 38 | 39 | it('es6 - terser (default options)', async () => { 40 | const code = ` 41 | const x = { 42 | baz_: 0, 43 | foo_: 1, 44 | calc() { 45 | return this.foo_ + this.baz_; 46 | } 47 | }; 48 | x.bar_ = (a) => a + 2; 49 | x["baz_"] = 3; 50 | console.log(x.calc());`; 51 | 52 | const result = await f(code, { path: 'source/test.js' }); 53 | 54 | result.length.should.below(code.length); 55 | }); 56 | 57 | it('exclude - terser should ignore *.min.js by default', async () => { 58 | const result = await f(es5, { path: 'source/test.min.js' }); 59 | 60 | result.should.eql(es5); 61 | }); 62 | 63 | it('after_render', async () => { 64 | hexo.extend.filter.register('after_render:js', f); 65 | const data = { path: null }; 66 | const result = await hexo.extend.filter.exec('after_render:js', es5, { 67 | args: [data] 68 | }); 69 | result.length.should.below(es5.length); 70 | 71 | hexo.extend.filter.unregister('after_render:js', f); 72 | }); 73 | }); 74 | --------------------------------------------------------------------------------