diff --git a/index.js b/index.js index d95e5aa..ba15fcd 100644 --- a/index.js +++ b/index.js @@ -1,89 +1,81 @@ /* global hexo */ 'use strict' -const minifyDefault = { enable: true } -const htmlDefault = { +hexo.config.minify = Object.assign({ enable: true, - priority: 10, - verbose: false, - exclude: [], - collapseBooleanAttributes: true, - collapseWhitespace: true, - // Ignore '' https://hexo.io/docs/tag-plugins#Post-Excerpt - ignoreCustomComments: [/^\s*more/], - removeComments: true, - removeEmptyAttributes: true, - removeScriptTypeAttributes: true, - removeStyleLinkTypeAttributes: true, - minifyJS: true, - minifyCSS: true, - globOptions: { basename: true } -} -const cssDefault = { - enable: true, - priority: 10, - verbose: false, - exclude: ['*.min.css'], - level: 2, - globOptions: { basename: true } -} -const jsDefault = { - enable: true, - priority: 10, - verbose: false, - exclude: ['*.min.js'], - compress: {}, - mangle: true, - output: {}, - globOptions: { basename: true } -} -const svgDefault = { - enable: true, - priority: 10, - verbose: false, - include: ['*.svg', '!*.min.svg'], - plugins: [], - globOptions: { basename: true } -} -const gzipDefault = { - enable: true, - priority: 10, - verbose: false, - include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'], - globOptions: { basename: true } -} -const brotliDefault = { - enable: true, - priority: 10, - verbose: false, - include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'], - globOptions: { basename: true } -} -const xmlDefault = { - enable: false, - priority: 10, - verbose: false, - include: ['*.xml', '!*.min.xml'], - removeComments: true, - globOptions: { basename: true } -} -const jsonDefault = { - enable: false, - priority: 10, - verbose: false, - include: ['*.json', '!*.min.json'], - globOptions: { basename: true } -} - -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) -hexo.config.minify.xml = Object.assign(xmlDefault, hexo.config.minify.xml) -hexo.config.minify.json = Object.assign(jsonDefault, hexo.config.minify.json) + html: { + enable: true, + priority: 10, + verbose: false, + exclude: [], + collapseBooleanAttributes: true, + collapseWhitespace: true, + // Ignore '' https://hexo.io/docs/tag-plugins#Post-Excerpt + ignoreCustomComments: [/^\s*more/], + removeComments: true, + removeEmptyAttributes: true, + removeScriptTypeAttributes: true, + removeStyleLinkTypeAttributes: true, + minifyJS: true, + minifyCSS: true, + globOptions: { basename: true } + }, + css: { + enable: true, + priority: 10, + verbose: false, + exclude: ['*.min.css'], + level: 2, + globOptions: { basename: true } + }, + js: { + enable: true, + priority: 10, + verbose: false, + exclude: ['*.min.js'], + compress: {}, + mangle: true, + output: {}, + globOptions: { basename: true } + }, + svg: { + enable: true, + priority: 10, + verbose: false, + include: ['*.svg', '!*.min.svg'], + plugins: [], + globOptions: { basename: true } + }, + gzip: { + enable: true, + priority: 10, + verbose: false, + include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'], + globOptions: { basename: true } + }, + brotli: { + enable: true, + priority: 10, + verbose: false, + include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'], + globOptions: { basename: true } + }, + xml: { + enable: false, + priority: 10, + verbose: false, + include: ['*.xml', '!*.min.xml'], + removeComments: true, + globOptions: { basename: true } + }, + json: { + enable: false, + priority: 10, + verbose: false, + include: ['*.json', '!*.min.json'], + globOptions: { basename: true } + } +}, hexo.config.minify) if (hexo.config.minify.enable === true) { const filter = require('./lib/filter') @@ -96,15 +88,3 @@ if (hexo.config.minify.enable === true) { hexo.extend.filter.register('after_generate', filter.minifyXml, hexo.config.minify.xml.priority) hexo.extend.filter.register('after_generate', filter.minifyJson, hexo.config.minify.json.priority) } - -module.exports = { - minifyDefault, - htmlDefault, - cssDefault, - jsDefault, - svgDefault, - gzipDefault, - brotliDefault, - xmlDefault, - jsonDefault -} diff --git a/test/brotli.test.js b/test/brotli.test.js index f645851..bdff854 100644 --- a/test/brotli.test.js +++ b/test/brotli.test.js @@ -2,20 +2,26 @@ 'use strict' const Hexo = require('hexo') -const hexo = new Hexo(__dirname) -global.hexo = hexo -const { brotliDefault } = require('../index') -const b = require('../lib/filter').brotliFn.bind(hexo) const zlib = require('zlib') const { promisify } = require('util') const brotli = promisify(zlib.brotliCompress) const unbrotli = promisify(zlib.brotliDecompress) -const path = 'foo.txt' -const input = 'Lorem ipsum dolor sit amet consectetur adipiscing elit fusce' describe('brotli', () => { + const hexo = new Hexo(__dirname) + const b = require('../lib/filter').brotliFn.bind(hexo) + const path = 'foo.txt' + const input = 'Lorem ipsum dolor sit amet consectetur adipiscing elit fusce' + beforeEach(() => { - hexo.config.minify.brotli = Object.assign({}, brotliDefault) + hexo.config.minify = { + brotli: { + enable: true, + verbose: false, + include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'], + globOptions: { basename: true } + } + } hexo.route.set(path, input) }) diff --git a/test/css.test.js b/test/css.test.js index 3773798..a876ee4 100644 --- a/test/css.test.js +++ b/test/css.test.js @@ -2,17 +2,24 @@ 'use strict' const Hexo = require('hexo') -const hexo = new Hexo(__dirname) -global.hexo = hexo -const { cssDefault } = require('../index') -const c = require('../lib/filter').minifyCss.bind(hexo) const CleanCSS = require('clean-css') -const input = 'foo { bar: baz; } foo { aaa: bbb; }' -const path = 'foo.css' describe('css', () => { + const hexo = new Hexo(__dirname) + const c = require('../lib/filter').minifyCss.bind(hexo) + const input = 'foo { bar: baz; } foo { aaa: bbb; }' + const path = 'foo.css' + beforeEach(() => { - hexo.config.minify.css = Object.assign({}, cssDefault) + hexo.config.minify = { + css: { + enable: true, + verbose: false, + exclude: ['*.min.css'], + level: 2, + globOptions: { basename: true } + } + } }) test('default', async () => { diff --git a/test/gzip.test.js b/test/gzip.test.js index 534b049..027ef80 100644 --- a/test/gzip.test.js +++ b/test/gzip.test.js @@ -2,20 +2,26 @@ 'use strict' const Hexo = require('hexo') -const hexo = new Hexo(__dirname) -global.hexo = hexo -const { gzipDefault } = require('../index') -const g = require('../lib/filter').gzipFn.bind(hexo) const zlib = require('zlib') const { promisify } = require('util') const gzip = promisify(zlib.gzip) const unzip = promisify(zlib.unzip) -const path = 'foo.txt' -const input = 'Lorem ipsum dolor sit amet consectetur adipiscing elit fusce' describe('gzip', () => { + const hexo = new Hexo(__dirname) + const g = require('../lib/filter').gzipFn.bind(hexo) + const path = 'foo.txt' + const input = 'Lorem ipsum dolor sit amet consectetur adipiscing elit fusce' + beforeEach(() => { - hexo.config.minify.gzip = Object.assign({}, gzipDefault) + hexo.config.minify = { + gzip: { + enable: true, + verbose: false, + include: ['*.html', '*.css', '*.js', '*.txt', '*.ttf', '*.atom', '*.stl', '*.xml', '*.svg', '*.eot', '*.json'], + globOptions: { basename: true } + } + } hexo.route.set(path, input) }) diff --git a/test/html.test.js b/test/html.test.js index df444bf..8c7e821 100644 --- a/test/html.test.js +++ b/test/html.test.js @@ -2,18 +2,34 @@ 'use strict' const Hexo = require('hexo') -const hexo = new Hexo(__dirname) -global.hexo = hexo -const { htmlDefault } = require('../index') -const h = require('../lib/filter').minifyHtml.bind(hexo) const Htmlminifier = require('html-minifier').minify -const input = '

foo

' -const path = 'index.html' -const expected = Htmlminifier(input, htmlDefault) describe('html', () => { + const hexo = new Hexo(__dirname) + const h = require('../lib/filter').minifyHtml.bind(hexo) + const input = '

foo

' + const path = 'index.html' + const defaultCfg = { + html: { + enable: true, + verbose: false, + exclude: [], + collapseBooleanAttributes: true, + collapseWhitespace: true, + ignoreCustomComments: [/^\s*more/], + removeComments: true, + removeEmptyAttributes: true, + removeScriptTypeAttributes: true, + removeStyleLinkTypeAttributes: true, + minifyJS: true, + minifyCSS: true, + globOptions: { basename: true } + } + } + const expected = Htmlminifier(input, defaultCfg.html) + beforeEach(() => { - hexo.config.minify.html = Object.assign({}, htmlDefault) + hexo.config.minify = JSON.parse(JSON.stringify(defaultCfg)) }) test('default', () => { diff --git a/test/js.test.js b/test/js.test.js index 857593e..0dff3bd 100644 --- a/test/js.test.js +++ b/test/js.test.js @@ -2,23 +2,32 @@ 'use strict' const Hexo = require('hexo') -const hexo = new Hexo(__dirname) -global.hexo = hexo -const { jsDefault } = require('../index') -const j = require('../lib/filter').minifyJs.bind(hexo) const { minify: terserMinify } = require('terser') -const input = 'var o = { "foo": 1, bar: 3 };' -const path = 'foo.js' -let expected = '' describe('js', () => { + 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 = '' + beforeAll(async () => { - const { code } = await terserMinify(input, { mangle: jsDefault.mangle }) + const { code } = await terserMinify(input, { mangle: true }) expected = code }) beforeEach(async () => { - hexo.config.minify.js = Object.assign({}, jsDefault) + hexo.config.minify = { + js: { + enable: true, + verbose: false, + exclude: ['*.min.js'], + compress: {}, + mangle: true, + output: {}, + globOptions: { basename: true } + } + } }) test('default', async () => { diff --git a/test/json.test.js b/test/json.test.js index 6d9d01c..15a4309 100644 --- a/test/json.test.js +++ b/test/json.test.js @@ -2,17 +2,23 @@ 'use strict' const Hexo = require('hexo') -const hexo = new Hexo(__dirname) -global.hexo = hexo -const { jsonDefault } = require('../index') -const jsonFn = require('../lib/filter').minifyJson.bind(hexo) -const path = 'foo.json' -const input = '{\n\t"vitae": "hendrerit",\n\t"tristique": [\n\t\t"primis",\n\t\t"quam"\n\t]\n}' -const expected = '{"vitae":"hendrerit","tristique":["primis","quam"]}' describe('xml', () => { + const hexo = new Hexo(__dirname) + const jsonFn = require('../lib/filter').minifyJson.bind(hexo) + const path = 'foo.json' + const input = '{\n\t"vitae": "hendrerit",\n\t"tristique": [\n\t\t"primis",\n\t\t"quam"\n\t]\n}' + const expected = '{"vitae":"hendrerit","tristique":["primis","quam"]}' + beforeEach(() => { - hexo.config.minify.json = Object.assign({}, jsonDefault) + hexo.config.minify = { + json: { + enable: false, + verbose: false, + include: ['*.json', '!*.min.json'], + globOptions: { basename: true } + } + } // plugin is disabled by default hexo.config.minify.json.enable = true hexo.route.set(path, input) diff --git a/test/svg.test.js b/test/svg.test.js index 24744d7..8bd75ed 100644 --- a/test/svg.test.js +++ b/test/svg.test.js @@ -2,17 +2,24 @@ 'use strict' const Hexo = require('hexo') -const hexo = new Hexo(__dirname) -global.hexo = hexo -const { svgDefault } = require('../index') -const s = require('../lib/filter').minifySvg.bind(hexo) const Svgo = require('svgo') -const input = '' -const path = 'foo.svg' describe('svg', () => { + const hexo = new Hexo(__dirname) + const s = require('../lib/filter').minifySvg.bind(hexo) + const input = '' + const path = 'foo.svg' + beforeEach(() => { - hexo.config.minify.svg = Object.assign({}, svgDefault) + hexo.config.minify = { + svg: { + enable: true, + verbose: false, + include: ['*.svg', '!*.min.svg'], + plugins: [], + globOptions: { basename: true } + } + } hexo.route.set(path, input) }) diff --git a/test/xml.test.js b/test/xml.test.js index 5a3c185..eb328ac 100644 --- a/test/xml.test.js +++ b/test/xml.test.js @@ -2,17 +2,24 @@ 'use strict' const Hexo = require('hexo') -const hexo = new Hexo(__dirname) -global.hexo = hexo -const { xmlDefault } = require('../index') -const x = require('../lib/filter').minifyXml.bind(hexo) -const path = 'foo.xml' -const input = '\n\n \n foo\n' -const expected = 'foo' describe('xml', () => { + const hexo = new Hexo(__dirname) + const x = require('../lib/filter').minifyXml.bind(hexo) + const path = 'foo.xml' + const input = '\n\n \n foo\n' + const expected = 'foo' + beforeEach(() => { - hexo.config.minify.xml = Object.assign({}, xmlDefault) + hexo.config.minify = { + xml: { + enable: false, + verbose: false, + include: ['*.xml', '!*.min.xml'], + removeComments: true, + globOptions: { basename: true } + } + } // plugin is disabled by default hexo.config.minify.xml.enable = true hexo.route.set(path, input)