mirror of https://github.com/curbengh/hexo-yam
				
				
				
			
						commit
						55b48a76aa
					
				
							
								
								
									
										18
									
								
								README.md
								
								
								
								
							
							
						
						
									
										18
									
								
								README.md
								
								
								
								
							|  | @ -78,6 +78,24 @@ For more options, see [Terser](https://github.com/terser-js/terser). | ||||||
| 
 | 
 | ||||||
| ---------- | ---------- | ||||||
| 
 | 
 | ||||||
|  | ``` yaml | ||||||
|  | neat_svg: | ||||||
|  |   enable: true | ||||||
|  |   include: | ||||||
|  |     - '*.svg' | ||||||
|  |     - '!*.min.svg' | ||||||
|  | ``` | ||||||
|  | - **enable** - Enable the plugin. Defaults to `true`. | ||||||
|  | - **logger** - Verbose output. Defaults to `false`. | ||||||
|  | - **include** - Include files. Support wildcard pattern. | ||||||
|  |   - Exclude `*.min.svg` by default. | ||||||
|  | - **plugin** - Plugin options. | ||||||
|  |   - To retain comments, `plugins: [{removeComments: false}]`. | ||||||
|  | 
 | ||||||
|  | For more options, see [svgo](https://github.com/svg/svgo). | ||||||
|  | 
 | ||||||
|  | ---------- | ||||||
|  | 
 | ||||||
| ``` yaml | ``` yaml | ||||||
| neat_gzip: | neat_gzip: | ||||||
|   enable: true |   enable: true | ||||||
|  |  | ||||||
							
								
								
									
										9
									
								
								index.js
								
								
								
								
							
							
						
						
									
										9
									
								
								index.js
								
								
								
								
							|  | @ -33,6 +33,14 @@ if (hexo.config.neat_enable === true) { | ||||||
|     compress: {} |     compress: {} | ||||||
|   }, hexo.config.neat_js) |   }, hexo.config.neat_js) | ||||||
| 
 | 
 | ||||||
|  |   // SVG minifier
 | ||||||
|  |   hexo.config.neat_svg = Object.assign({ | ||||||
|  |     enable: true, | ||||||
|  |     logger: false, | ||||||
|  |     include: ['*.svg','!*.min.svg'], | ||||||
|  |     plugin: [] | ||||||
|  |   }, hexo.config.neat_svg) | ||||||
|  | 
 | ||||||
|   // gzip compression
 |   // gzip compression
 | ||||||
|   hexo.config.neat_gzip = Object.assign({ |   hexo.config.neat_gzip = Object.assign({ | ||||||
|     enable: true, |     enable: true, | ||||||
|  | @ -51,6 +59,7 @@ if (hexo.config.neat_enable === true) { | ||||||
|   hexo.extend.filter.register('after_render:html', filter.logicHtml) |   hexo.extend.filter.register('after_render:html', filter.logicHtml) | ||||||
|   hexo.extend.filter.register('after_render:css', filter.logicCss) |   hexo.extend.filter.register('after_render:css', filter.logicCss) | ||||||
|   hexo.extend.filter.register('after_render:js', filter.logicJs) |   hexo.extend.filter.register('after_render:js', filter.logicJs) | ||||||
|  |   hexo.extend.filter.register('after_generate', filter.logicSvg) | ||||||
|   hexo.extend.filter.register('after_generate', filter.logicGzip) |   hexo.extend.filter.register('after_generate', filter.logicGzip) | ||||||
|   hexo.extend.filter.register('after_generate', filter.logicBrotli) |   hexo.extend.filter.register('after_generate', filter.logicBrotli) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -6,6 +6,7 @@ const Htmlminifier = require('html-minifier').minify | ||||||
| const nanomatch = require('nanomatch') | const nanomatch = require('nanomatch') | ||||||
| const zlib = require('zlib') | const zlib = require('zlib') | ||||||
| const br = require('iltorb') | const br = require('iltorb') | ||||||
|  | const svgo = require('svgo') | ||||||
| 
 | 
 | ||||||
| function logicHtml (str, data) { | function logicHtml (str, data) { | ||||||
|   const hexo = this |   const hexo = this | ||||||
|  | @ -86,6 +87,43 @@ function logicJs (str, data) { | ||||||
|   return result.code |   return result.code | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | function logicSvg () { | ||||||
|  |   const hexo = this | ||||||
|  |   const options = hexo.config.neat_svg | ||||||
|  |   // Return if disabled.
 | ||||||
|  |   if (options.enable === false) return | ||||||
|  | 
 | ||||||
|  |   let route = hexo.route | ||||||
|  |   let routeList = route.list() | ||||||
|  |   let include = options.include | ||||||
|  | 
 | ||||||
|  |   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) | ||||||
|  |       let assetTxt = '' | ||||||
|  |       // Extract the content
 | ||||||
|  |       assetPath.on('data', (chunk) => (assetTxt += chunk)) | ||||||
|  |       assetPath.on('end', () => { | ||||||
|  |         if (assetTxt.length) { | ||||||
|  |           // gzip compress using highest level
 | ||||||
|  |           new svgo(options).optimize(assetTxt).then(function(result) { | ||||||
|  |               // Save the compressed file to .gz
 | ||||||
|  |               route.set(path, result.data) | ||||||
|  |               // Logging
 | ||||||
|  |               let saved = ((assetTxt.length - result.data.length) / assetTxt.length * 100).toFixed(2) | ||||||
|  |               if (options.logger) { | ||||||
|  |                 let log = hexo.log || console.log | ||||||
|  |                 log.log('Minify the svg: %s [%s saved]', path, saved + '%') | ||||||
|  |               } | ||||||
|  |               resolve(assetTxt) | ||||||
|  |           }) | ||||||
|  |         } | ||||||
|  |       }) | ||||||
|  |     }) | ||||||
|  |   })) | ||||||
|  | } | ||||||
|  | 
 | ||||||
| function logicGzip () { | function logicGzip () { | ||||||
|   const hexo = this |   const hexo = this | ||||||
|   const options = hexo.config.neat_gzip |   const options = hexo.config.neat_gzip | ||||||
|  | @ -174,6 +212,7 @@ module.exports = { | ||||||
|   logicHtml: logicHtml, |   logicHtml: logicHtml, | ||||||
|   logicCss: logicCss, |   logicCss: logicCss, | ||||||
|   logicJs: logicJs, |   logicJs: logicJs, | ||||||
|  |   logicSvg: logicSvg, | ||||||
|   logicGzip: logicGzip, |   logicGzip: logicGzip, | ||||||
|   logicBrotli: logicBrotli |   logicBrotli: logicBrotli | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -22,12 +22,14 @@ | ||||||
|     "html-minifier": "^4.0.0", |     "html-minifier": "^4.0.0", | ||||||
|     "iltorb": "^2.4.2", |     "iltorb": "^2.4.2", | ||||||
|     "nanomatch": "^1.2.13", |     "nanomatch": "^1.2.13", | ||||||
|  |     "svgo": "^1.2.2", | ||||||
|     "terser": "^3.17.0" |     "terser": "^3.17.0" | ||||||
|   }, |   }, | ||||||
|   "keywords": [ |   "keywords": [ | ||||||
|     "html", |     "html", | ||||||
|     "js", |     "js", | ||||||
|     "css", |     "css", | ||||||
|  |     "svg", | ||||||
|     "minify", |     "minify", | ||||||
|     "compress", |     "compress", | ||||||
|     "gzip", |     "gzip", | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue