2020-01-03 00:51:45 +00:00
|
|
|
/* eslint-env jest */
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Hexo = require('hexo')
|
2020-09-05 10:56:33 +00:00
|
|
|
const { minify: terserMinify } = require('terser')
|
2020-01-03 00:51:45 +00:00
|
|
|
|
|
|
|
describe('js', () => {
|
2020-09-05 11:18:35 +00:00
|
|
|
const hexo = new Hexo(__dirname)
|
|
|
|
const j = require('../lib/filter').minifyJs.bind(hexo)
|
|
|
|
const input = 'var o = { "foo": 1, bar: 3 };'
|
|
|
|
const path = 'foo.js'
|
|
|
|
let expected = ''
|
|
|
|
|
2020-09-05 10:56:33 +00:00
|
|
|
beforeAll(async () => {
|
2020-09-05 11:18:35 +00:00
|
|
|
const { code } = await terserMinify(input, { mangle: true })
|
2020-09-05 10:56:33 +00:00
|
|
|
expected = code
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
2020-09-05 11:18:35 +00:00
|
|
|
hexo.config.minify = {
|
|
|
|
js: {
|
|
|
|
enable: true,
|
|
|
|
verbose: false,
|
|
|
|
exclude: ['*.min.js'],
|
|
|
|
compress: {},
|
|
|
|
mangle: true,
|
|
|
|
output: {},
|
|
|
|
globOptions: { basename: true }
|
|
|
|
}
|
|
|
|
}
|
2020-01-03 00:51:45 +00:00
|
|
|
})
|
|
|
|
|
2020-09-05 10:56:33 +00:00
|
|
|
test('default', async () => {
|
|
|
|
const result = await j(input, { path })
|
2020-01-03 00:51:45 +00:00
|
|
|
|
|
|
|
expect(result).toBeDefined()
|
|
|
|
expect(expected).toBeDefined()
|
|
|
|
expect(result).toBe(expected)
|
|
|
|
})
|
|
|
|
|
2020-09-05 10:56:33 +00:00
|
|
|
test('disable', async () => {
|
2020-01-03 00:51:45 +00:00
|
|
|
hexo.config.minify.js.enable = false
|
|
|
|
|
2020-09-05 10:56:33 +00:00
|
|
|
const result = await j(input, { path })
|
2020-01-03 00:51:45 +00:00
|
|
|
|
|
|
|
expect(result).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
2020-09-05 10:56:33 +00:00
|
|
|
test('empty file', async () => {
|
|
|
|
const result = await j('', { path })
|
2020-01-03 00:51:45 +00:00
|
|
|
|
|
|
|
expect(result).toBeUndefined()
|
|
|
|
})
|
|
|
|
|
2020-09-05 10:56:33 +00:00
|
|
|
test('option', async () => {
|
2020-01-03 00:51:45 +00:00
|
|
|
const customOpt = {
|
|
|
|
mangle: {
|
|
|
|
properties: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hexo.config.minify.js = customOpt
|
|
|
|
|
2020-09-05 10:56:33 +00:00
|
|
|
const result = await j(input, { path })
|
|
|
|
const { code: expected } = await terserMinify(input, customOpt)
|
2020-01-03 00:51:45 +00:00
|
|
|
|
|
|
|
expect(result).toBe(expected)
|
|
|
|
})
|
|
|
|
|
2020-09-05 10:56:33 +00:00
|
|
|
test('option - verbose', async () => {
|
2020-01-03 00:51:45 +00:00
|
|
|
hexo.config.minify.js.verbose = true
|
|
|
|
hexo.log.log = jest.fn()
|
2020-09-05 10:56:33 +00:00
|
|
|
await j(input, { path })
|
2020-01-03 00:51:45 +00:00
|
|
|
|
|
|
|
expect(hexo.log.log.mock.calls[0][0]).toContain(`js: ${path}`)
|
|
|
|
})
|
|
|
|
|
2020-09-05 10:56:33 +00:00
|
|
|
test('option - invalid', async () => {
|
2020-01-03 00:51:45 +00:00
|
|
|
const customOpt = {
|
|
|
|
mangle: {
|
|
|
|
foo: 'bar'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hexo.config.minify.js = customOpt
|
|
|
|
|
2020-09-05 11:58:20 +00:00
|
|
|
let expected
|
2020-09-05 10:56:33 +00:00
|
|
|
try {
|
|
|
|
await terserMinify(input, customOpt)
|
|
|
|
} catch (err) {
|
2020-09-05 11:58:20 +00:00
|
|
|
expected = err
|
2020-09-05 10:56:33 +00:00
|
|
|
}
|
2020-01-09 15:14:25 +00:00
|
|
|
|
2020-09-05 11:58:20 +00:00
|
|
|
expect(expected).toBeDefined()
|
|
|
|
await expect(j(input, { path })).rejects.toThrow(`Path: ${path}\n${expected}`)
|
2020-01-03 00:51:45 +00:00
|
|
|
})
|
|
|
|
|
2020-09-05 10:56:33 +00:00
|
|
|
test('exclude - *.min.js', async () => {
|
|
|
|
const result = await j(input, { path: 'foo/bar.min.js' })
|
2020-01-03 00:51:45 +00:00
|
|
|
|
|
|
|
expect(result).toBe(input)
|
|
|
|
})
|
|
|
|
|
2020-09-05 10:56:33 +00:00
|
|
|
test('exclude - basename', async () => {
|
2020-01-03 00:51:45 +00:00
|
|
|
const exclude = '*baz.js'
|
|
|
|
hexo.config.minify.js.exclude = exclude
|
2020-09-05 10:56:33 +00:00
|
|
|
const result = await j(input, { path: 'foo/barbaz.js' })
|
2020-01-03 00:51:45 +00:00
|
|
|
|
|
|
|
expect(result).toBe(input)
|
|
|
|
})
|
|
|
|
|
2020-09-05 10:56:33 +00:00
|
|
|
test('exclude - slash in pattern', async () => {
|
2020-01-03 00:51:45 +00:00
|
|
|
const exclude = '**/lectus/**/*.js'
|
|
|
|
hexo.config.minify.js.exclude = exclude
|
2020-09-05 10:56:33 +00:00
|
|
|
const result = await j(input, { path: 'eleifend/lectus/nullam/dapibus/netus.js' })
|
2020-01-03 00:51:45 +00:00
|
|
|
|
|
|
|
expect(result).toBe(input)
|
|
|
|
})
|
2020-05-25 08:36:21 +00:00
|
|
|
|
2020-09-05 10:56:33 +00:00
|
|
|
test('invalid string', async () => {
|
2020-05-25 08:36:21 +00:00
|
|
|
const invalid = 'console.log("\\");'
|
|
|
|
|
2020-09-05 11:58:20 +00:00
|
|
|
await expect(j(invalid, { path })).rejects.toThrow(`Path: ${path}\nSyntaxError`)
|
2020-05-25 08:36:21 +00:00
|
|
|
})
|
2020-01-03 00:51:45 +00:00
|
|
|
})
|