Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for html, js, css minifiers #21

Merged
merged 2 commits into from
Dec 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules/
package-lock.json
tmp/
*.log
coverage/
22 changes: 10 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ cache:
npm: true

script:
- npm install snyk
- snyk auth $SNYK_TOKEN
- snyk test # Check node modules for vulnerability
- snyk protect # Patch node modules (if available)
- snyk monitor # Update dependencies to snyk
- npm run lint
- npm run test

- npm install standard
- standard

branches:
only:
- master # Only build master branch
- /^greenkeeper.*$/ # Greenkeeper branches
after_script:
- if ([[ $TRAVIS_EVENT_TYPE == "push" ]] || [[ $TRAVIS_EVENT_TYPE == "cron" ]]) && [[ $TRAVIS_BRANCH == "master" ]] && [[ $TRAVIS_NODE_VERSION == "12" ]]; then
npm install snyk;
snyk auth $SNYK_TOKEN;
snyk test;
snyk protect;
snyk monitor;
fi
52 changes: 31 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
/* global hexo */
'use strict'

hexo.config.minify = Object.assign({
enable: true
}, hexo.config.minify)

hexo.config.minify.html = Object.assign({
const minifyDefault = { enable: true }
const htmlDefault = {
enable: true,
priority: 10,
logger: false,
Expand All @@ -21,19 +18,17 @@ hexo.config.minify.html = Object.assign({
minifyJS: true,
minifyCSS: true,
globOptions: { basename: true }
}, hexo.config.minify.html)

hexo.config.minify.css = Object.assign({
}
const cssDefault = {
enable: true,
priority: 10,
// TODO: rename to verbose
logger: false,
exclude: ['*.min.css'],
level: 2,
globOptions: { basename: true }
}, hexo.config.minify.css)

hexo.config.minify.js = Object.assign({
}
const jsDefault = {
enable: true,
priority: 10,
logger: false,
Expand All @@ -42,32 +37,37 @@ hexo.config.minify.js = Object.assign({
mangle: true,
output: {},
globOptions: { basename: true }
}, hexo.config.minify.js)

hexo.config.minify.svg = Object.assign({
}
const svgDefault = {
enable: true,
priority: 10,
logger: false,
include: ['*.svg', '!*.min.svg'],
plugins: [],
globOptions: { basename: true }
}, hexo.config.minify.svg)

hexo.config.minify.gzip = Object.assign({
}
const gzipDefault = {
enable: true,
priority: 10,
logger: false,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'],
globOptions: { basename: true }
}, hexo.config.minify.gzip)

hexo.config.minify.brotli = Object.assign({
}
const brotliDefault = {
enable: true,
priority: 10,
logger: false,
include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'],
globOptions: { basename: true }
}, hexo.config.minify.brotli)
}

hexo.config.minify = Object.assign(minifyDefault, hexo.config.minify)
hexo.config.minify.html = Object.assign(htmlDefault, hexo.config.minify.html)
hexo.config.minify.css = Object.assign(cssDefault, hexo.config.minify.css)
hexo.config.minify.js = Object.assign(jsDefault, hexo.config.minify.js)
hexo.config.minify.svg = Object.assign(svgDefault, hexo.config.minify.svg)
hexo.config.minify.gzip = Object.assign(gzipDefault, hexo.config.minify.gzip)
hexo.config.minify.brotli = Object.assign(brotliDefault, hexo.config.minify.brotli)

if (hexo.config.minify.enable === true) {
const filter = require('./lib/filter')
Expand All @@ -78,3 +78,13 @@ if (hexo.config.minify.enable === true) {
hexo.extend.filter.register('after_generate', filter.gzipFn, hexo.config.minify.gzip.priority)
hexo.extend.filter.register('after_generate', filter.brotliFn, hexo.config.minify.brotli.priority)
}

module.exports = {
minifyDefault,
htmlDefault,
cssDefault,
jsDefault,
svgDefault,
gzipDefault,
brotliDefault
}
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"lib/",
"index.js"
],
"scripts": {
"lint": "standard",
"test": "jest test/filter.test.js"
},
"engines": {
"node": ">= 8.6.0"
},
Expand All @@ -26,6 +30,11 @@
"svgo": "^1.2.2",
"terser": "^4.0.0"
},
"devDependencies": {
"hexo": "^4.2.0",
"jest": "^24.9.0",
"standard": "^14.3.1"
},
"keywords": [
"html",
"js",
Expand All @@ -38,6 +47,12 @@
"hexo-yam",
"hexo"
],
"jest": {
"clearMocks": true,
"collectCoverage": true,
"coverageDirectory": "coverage",
"testEnvironment": "node"
},
"greenkeeper": {
"commitMessages": {
"initialBadge": "docs(readme): Add Greenkeeper badge",
Expand Down
160 changes: 160 additions & 0 deletions test/filter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/* eslint-env jest */
'use strict'

const Hexo = require('hexo')
const hexo = new Hexo(__dirname)
global.hexo = hexo

describe('html', () => {
const { htmlDefault } = require('../index')
const h = require('../lib/filter').minifyHtml.bind(hexo)
const Htmlminifier = require('html-minifier').minify

beforeEach(() => {
hexo.config.minify.html = htmlDefault
})

test('default', () => {
const input = '<p id="">foo</p>'
const result = h(input, { path: '' })
const expected = Htmlminifier(input, hexo.config.minify.html)
expect(result).toBe(expected)
})

test('option', () => {
const customOpt = { removeEmptyAttributes: false }
hexo.config.minify.html = customOpt

const input = '<p id="">foo</p>'
const result = h(input, { path: '' })
const expected = Htmlminifier(input, customOpt)
expect(result).toBe(input)
expect(result).toBe(expected)
})

test('exclude', () => {
const exclude = '*.min.html'
hexo.config.minify.html.exclude = exclude

const input = '<p id="">foo</p>'
const result = h(input, { path: 'foo/bar.min.html' })
expect(result).toBe(input)
})

test('exclude - slash in pattern', () => {
const exclude = '**/bar.html'
hexo.config.minify.html.exclude = exclude

const input = '<p id="">foo</p>'
const result = h(input, { path: 'foo/bar.html' })
expect(result).toBe(input)
})
})

describe('css', () => {
const { cssDefault } = require('../index')
const c = require('../lib/filter').minifyCss.bind(hexo)
const CleanCSS = require('clean-css')

beforeEach(() => {
hexo.config.minify.css = cssDefault
})

test('default', async () => {
const input = 'foo { bar: baz; } foo { aaa: bbb; }'
const result = await c(input, { path: '' })
const { styles } = await new CleanCSS(hexo.config.minify.css).minify(input)
expect(result).toBe(styles)
})

test('option', async () => {
const customOpt = {
level: {
1: {
mergeAdjacentRules: false
}
}
}
hexo.config.minify.css = customOpt

const input = 'foo { bar: baz; } foo { aaa: bbb; }'
const result = await c(input, { path: '' })
const { styles } = await new CleanCSS(customOpt).minify(input)
expect(result).toBe(styles)
})

test('exclude - *.min.css', async () => {
const input = 'foo { bar: baz; } foo { aaa: bbb; }'
const result = await c(input, { path: 'foo/bar.min.css' })
expect(result).toBe(input)
})

test('exclude - basename', async () => {
const exclude = '*baz.css'
hexo.config.minify.css.exclude = exclude
const input = 'foo { bar: baz; } foo { aaa: bbb; }'
const result = await c(input, { path: 'foo/barbaz.css' })
expect(result).toBe(input)
})

test('exclude - slash in pattern', async () => {
const exclude = '**/bar.css'
hexo.config.minify.css.exclude = exclude
const input = 'foo { bar: baz; } foo { aaa: bbb; }'
const result = await c(input, { path: 'foo/bar.css' })
expect(result).toBe(input)
})
})

describe('js', () => {
const { jsDefault } = require('../index')
const j = require('../lib/filter').minifyJs.bind(hexo)
const Terser = require('terser')

beforeEach(() => {
hexo.config.minify.js = jsDefault
})

test('default', () => {
const input = 'var o = { "foo": 1, bar: 3 };'
const result = j(input, { path: '' })
const { code } = Terser.minify(input, { mangle: jsDefault.mangle })
expect(result).toBe(code)
})

test('option', () => {
const customOpt = {
mangle: {
properties: true
}
}
hexo.config.minify.js = customOpt

const input = 'var o = { "foo": 1, bar: 3 };'
const result = j(input, { path: '' })
const { code } = Terser.minify(input, customOpt)
expect(result).toBe(code)
})

test('exclude - *.min.js', () => {
const input = 'var o = { "foo": 1, bar: 3 };'
const result = j(input, { path: 'foo/bar.min.js' })
expect(result).toBe(input)
})

test('exclude - basename', () => {
const exclude = '*baz.js'
hexo.config.minify.js.exclude = exclude
const input = 'var o = { "foo": 1, bar: 3 };'
const result = j(input, { path: 'foo/barbaz.js' })
expect(result).toBe(input)
})

test('exclude - slash in pattern', () => {
const exclude = '**/bar.js'
hexo.config.minify.js.exclude = exclude
const input = 'var o = { "foo": 1, bar: 3 };'
const result = j(input, { path: 'foo/bar.js' })
expect(result).toBe(input)
})
})