From 528486761d7bcc9ab81df27740b355490c370684 Mon Sep 17 00:00:00 2001 From: weyusi Date: Tue, 23 Apr 2019 15:10:43 +0930 Subject: [PATCH] feat: allow custom file extension for compressions --- README.md | 30 +++++++++++++++++++++++++++++- index.js | 10 ++++++---- lib/filter.js | 12 ++++-------- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 4454f9d..d441999 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ neat_html: - **enable** - Enable the plugin. Defaults to `true`. - **logger** - Verbose output. Defaults to `false`. - **exclude** - Exclude files. Support [wildcard](https://github.com/micromatch/nanomatch#features) glob pattern. - - It can be specified as a one-liner, `[*.min.html, *.note.html]`. + - Support one-liner, `exclude: [*.min.html, *.note.html]`. - To exclude a file, double asterisk and the full path must be specified, `**/themes/typing/source/js/source.js`. - `*source.js` also works, but it also excludes `resource.js`. - Test glob pattern on the web using [Globtester](http://www.globtester.com/). @@ -81,18 +81,46 @@ For more options, see [Terser](https://github.com/terser-js/terser). ``` yaml neat_gzip: enable: true + include: + - '*.html' + - '*.css' + - '*.js' + - '*.txt' + - '*.ttf' + - '*.atom' + - '*.stl' + - '*.xml' + - '*.svg' + - '*.eot' + - '*.json' ``` - **enable** - Enable the plugin. Defaults to `true`. - **logger** - Verbose output. Defaults to `false`. +- **include** - Include files. Support wildcard pattern. + - Support one-liner, `include: ['*.html','*.css','*.js']`. + - Must include asterisk and single quotes. `.html` is invalid. `'*.html'` is valid. ---------- ``` yaml neat_brotli: enable: true + include: + - '*.html' + - '*.css' + - '*.js' + - '*.txt' + - '*.ttf' + - '*.atom' + - '*.stl' + - '*.xml' + - '*.svg' + - '*.eot' + - '*.json' ``` - **enable** - Enable the plugin. Defaults to `true`. - **logger** - Verbose output. Defaults to `false`. +- **include** - Include files. Support wildcard pattern. ## HTTP Compression While most modern web browsers [support](https://www.caniuse.com/#feat=brotli) Brotli, you also need to consider whether the web/app server, hosting platform, reverse proxy or CDN (whichever relevant to you) support it. diff --git a/index.js b/index.js index 56c72eb..b03388b 100644 --- a/index.js +++ b/index.js @@ -33,16 +33,18 @@ if (hexo.config.neat_enable === true) { compress: {} }, hexo.config.neat_js) - // html, css, js compression + // gzip compression hexo.config.neat_gzip = Object.assign({ enable: true, - logger: false + logger: false, + include: ['*.html','*.css','*.js','*.txt','*.ttf','*.atom','*.stl','*.xml','*.svg','*.eot','*.json'] }, hexo.config.neat_gzip) - // html, css, js compression + // brotli compression hexo.config.neat_brotli = Object.assign({ enable: true, - logger: false + logger: false, + include: ['*.html','*.css','*.js','*.txt','*.ttf','*.atom','*.stl','*.xml','*.svg','*.eot','*.json'] }, hexo.config.neat_brotli) const filter = require('./lib/filter') diff --git a/lib/filter.js b/lib/filter.js index 20a021f..cc6d6a8 100644 --- a/lib/filter.js +++ b/lib/filter.js @@ -94,11 +94,9 @@ function logicGzip () { let route = hexo.route let routeList = route.list() + let include = options.include - return Promise.all(routeList.filter(path => (path.endsWith('.html') || path.endsWith('.js') || path.endsWith('.css') - || path.endsWith('.xml') || path.endsWith('.json') || path.endsWith('.txt') - || path.endsWith('.ttf') || path.endsWith('.atom') || path.endsWith('.stl') - || path.endsWith('.svg') || path.endsWith('.eot'))).map(path => { + return Promise.all((nanomatch(routeList, include, { matchBase: true })).map(path => { return new Promise((resolve, reject) => { // Grab all assets using hexo router let assetPath = route.get(path) @@ -137,11 +135,9 @@ function logicBrotli () { let route = hexo.route let routeList = route.list() + let include = options.include - return Promise.all(routeList.filter(path => (path.endsWith('.html') || path.endsWith('.js') || path.endsWith('.css') - || path.endsWith('.xml') || path.endsWith('.json') || path.endsWith('.txt') - || path.endsWith('.ttf') || path.endsWith('.atom') || path.endsWith('.stl') - || path.endsWith('.svg') || path.endsWith('.eot'))).map(path => { + return Promise.all((nanomatch(routeList, include, { matchBase: true })).map(path => { return new Promise((resolve, reject) => { // Grab all assets using hexo router let assetPath = route.get(path)