├── .eslintrc ├── .github ├── dependabot.yml └── workflows │ ├── linter.yml │ └── tester.yml ├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── lib └── filter.js ├── package.json └── test ├── .eslintrc └── index.js /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "hexo", 3 | "root": true 4 | } 5 | -------------------------------------------------------------------------------- /.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/linter.yml: -------------------------------------------------------------------------------- 1 | name: Linter 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | linter: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v4 10 | - name: Use Node.js 14.x 11 | uses: actions/setup-node@v4 12 | with: 13 | node-version: '14.x' 14 | - name: Install Dependencies 15 | run: npm install 16 | - name: Lint 17 | run: | 18 | npm run eslint 19 | env: 20 | CI: true -------------------------------------------------------------------------------- /.github/workflows/tester.yml: -------------------------------------------------------------------------------- 1 | name: Tester 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | tester: 7 | runs-on: ${{ matrix.os }} 8 | strategy: 9 | matrix: 10 | os: [ubuntu-latest, windows-latest, macos-latest] 11 | node-version: ['16.x', '18.x', '20.x'] 12 | fail-fast: false 13 | steps: 14 | - uses: actions/checkout@v4 15 | - name: Use Node.js ${{ matrix.node-version }} 16 | uses: actions/setup-node@v4 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - name: Install Dependencies 20 | run: npm install 21 | - name: Test 22 | run: npm test -- --no-parallel 23 | env: 24 | CI: true 25 | coverage: 26 | runs-on: ${{ matrix.os }} 27 | strategy: 28 | matrix: 29 | os: [ubuntu-latest] 30 | node-version: ['14.x'] 31 | steps: 32 | - uses: actions/checkout@v4 33 | - name: Use Node.js ${{ matrix.node-version }} 34 | uses: actions/setup-node@v4 35 | with: 36 | node-version: ${{ matrix.node-version }} 37 | - name: Install Dependencies 38 | run: npm install 39 | - name: Coverage 40 | run: npm run test-cov 41 | env: 42 | CI: true 43 | - name: Coveralls 44 | uses: coverallsapp/github-action@master 45 | with: 46 | github-token: ${{ secrets.github_token }} 47 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | tmp/ 4 | *.log 5 | .idea/ 6 | .nyc_output/ 7 | package-lock.json 8 | -------------------------------------------------------------------------------- /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-clean-css 2 | 3 | [![NPM version](https://badge.fury.io/js/hexo-clean-css.svg)](https://www.npmjs.com/package/hexo-clean-css) 4 | 5 | Minify CSS files with [clean-css]. 6 | 7 | ## Installation 8 | 9 | ``` bash 10 | $ npm install hexo-clean-css --save 11 | ``` 12 | 13 | ## Options 14 | 15 | ``` yaml 16 | clean_css: 17 | exclude: 18 | - '*.min.css' 19 | ``` 20 | 21 | - **exclude**: Exclude files 22 | 23 | [clean-css]: https://github.com/jakubpawlowicz/clean-css 24 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /* global hexo */ 4 | 5 | hexo.config.clean_css = Object.assign({ 6 | exclude: ['*.min.css'] 7 | }, hexo.config.clean_css); 8 | 9 | hexo.extend.filter.register('after_render:css', require('./lib/filter')); 10 | -------------------------------------------------------------------------------- /lib/filter.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const CleanCSS = require('clean-css'); 4 | const micromatch = require('micromatch'); 5 | 6 | module.exports = async function(str, data) { 7 | const options = this.config.clean_css; 8 | const path = data.path; 9 | const exclude = options.exclude; 10 | 11 | if (path && exclude && exclude.length) { 12 | if (micromatch.isMatch(path, exclude, { basename: true })) return str; 13 | } 14 | 15 | try { 16 | const { styles } = await new CleanCSS(options).minify(str); 17 | return styles; 18 | } catch (err) { 19 | throw new Error(err); 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hexo-clean-css", 3 | "version": "2.0.0", 4 | "description": "Minify CSS files with clean-css.", 5 | "main": "index.js", 6 | "scripts": { 7 | "eslint": "eslint .", 8 | "test": "mocha test/index.js", 9 | "test-cov": "nyc --reporter=lcovonly npm run test" 10 | }, 11 | "files": [ 12 | "lib/" 13 | ], 14 | "directories": { 15 | "lib": "./lib" 16 | }, 17 | "repository": "hexojs/hexo-clean-css", 18 | "keywords": [ 19 | "hexo", 20 | "filter", 21 | "uglify" 22 | ], 23 | "author": "Tommy Chen (https://zespia.tw)", 24 | "maintainers": [ 25 | "Abner Chou (https://abnerchou.me)" 26 | ], 27 | "license": "MIT", 28 | "dependencies": { 29 | "clean-css": "^5.1.2", 30 | "micromatch": "^4.0.4" 31 | }, 32 | "devDependencies": { 33 | "chai": "^4.3.4", 34 | "eslint": "^8.9.0", 35 | "eslint-config-hexo": "^5.0.0", 36 | "mocha": "^10.2.0", 37 | "nyc": "^15.1.0" 38 | }, 39 | "engines": { 40 | "node": ">=14" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /test/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "hexo/test" 3 | } 4 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('chai').should(); 4 | const CleanCSS = require('clean-css'); 5 | const ctx = { 6 | config: { 7 | clean_css: {} 8 | } 9 | }; 10 | const c = require('../lib/filter').bind(ctx); 11 | 12 | describe('hexo-clean-css', () => { 13 | after(() => { ctx.config.clean_css = {}; }); 14 | 15 | it('default', async () => { 16 | const input = 'a{font-weight:bold;}'; 17 | const result = await c(input, { path: 'foo' }); 18 | const { styles } = new CleanCSS({}).minify(input); 19 | 20 | result.should.eql(styles); 21 | }); 22 | 23 | it('options - clean_css', async () => { 24 | ctx.config.clean_css = { format: 'keep-breaks' }; 25 | const input = 'a{font-weight:bold;}\nb{font-weight:bold;}'; 26 | const result = await c(input, { path: 'foo' }); 27 | const { styles } = new CleanCSS(ctx.config.clean_css).minify(input); 28 | 29 | result.should.eql(styles); 30 | }); 31 | 32 | it('options - exclude (string)', async () => { 33 | ctx.config.clean_css = { exclude: '*lo.css' }; 34 | const input = 'a{font-weight:bold;}'; 35 | const result = await c(input, { path: 'foo/bar/hello.css' }); 36 | 37 | result.should.eql(input); 38 | }); 39 | 40 | it('options - exclude (array)', async () => { 41 | ctx.config.clean_css = { exclude: ['*lo.css', 'other.css'] }; 42 | const input = 'a{font-weight:bold;}'; 43 | const result = await c(input, { path: 'foo/bar/hello.css' }); 44 | 45 | result.should.eql(input); 46 | }); 47 | 48 | it('options - invalid', async () => { 49 | ctx.config.clean_css = { level: 9000 }; 50 | const input = 'a{font-weight:bold;}'; 51 | let result, expected; 52 | 53 | try { 54 | await c(input, { path: 'foo' }); 55 | } catch (err) { 56 | result = err.message.split('\n')[0]; 57 | } 58 | 59 | try { 60 | new CleanCSS(ctx.config.clean_css).minify(input); 61 | } catch (err) { 62 | expected = err.message.split('\n')[0]; 63 | } 64 | 65 | result.includes(expected).should.eql(true); 66 | }); 67 | }); 68 | --------------------------------------------------------------------------------