mirror of https://github.com/curbengh/hexo-yam
				
				
				
			Add gzip compression
This commit is contained in:
		
							parent
							
								
									732d07325c
								
							
						
					
					
						commit
						ea13ea9967
					
				
							
								
								
									
										10
									
								
								README.md
								
								
								
								
							
							
						
						
									
										10
									
								
								README.md
								
								
								
								
							|  | @ -8,7 +8,7 @@ | |||
| 
 | ||||
| > This project is based on [hexo-neat](https://github.com/rozbo/hexo-neat) | ||||
| 
 | ||||
| Yet Another Minifier for Hexo. | ||||
| Yet Another Minifier for Hexo. Minify and compress (gzip) html, js and css. | ||||
| 
 | ||||
| The original package has not been [updated](https://www.npmjs.com/package/hexo-neat) for a while. Its outdated dependencies suffer from minor [vulnerability](https://snyk.io/test/npm/hexo-neat). | ||||
| 
 | ||||
|  | @ -68,6 +68,13 @@ neat_js: | |||
| - **exclude**: Exclude files | ||||
| **Note:** For more options, see '[UglifyJS](https://github.com/mishoo/UglifyJS2)' | ||||
| 
 | ||||
| ---------- | ||||
| 
 | ||||
| ``` yaml | ||||
| gzip: | ||||
|   enable: true | ||||
| ``` | ||||
| - **enable** - Enable the plugin. Defaults to `true`. | ||||
| 
 | ||||
| ## Credits | ||||
| All credits go to the following work: | ||||
|  | @ -75,3 +82,4 @@ All credits go to the following work: | |||
| - neat html by [HTMLMinifier](https://github.com/kangax/html-minifier) | ||||
| - neat css  by [clean-css](https://github.com/jakubpawlowicz/clean-css) | ||||
| - neat js   by  [UglifyJS](http://lisperator.net/uglifyjs/) | ||||
| - gzip inspired by [hexo-generator-optimize](https://github.com/JackyRen/hexo-generator-optimize) | ||||
							
								
								
									
										6
									
								
								index.js
								
								
								
								
							
							
						
						
									
										6
									
								
								index.js
								
								
								
								
							|  | @ -35,10 +35,16 @@ var assign = require('object-assign'); | |||
|             exclude: ['*.min.js'] | ||||
|         }, hexo.config.neat_js); | ||||
| 
 | ||||
|         // html, css, js compression
 | ||||
|         hexo.config.gzip = assign({ | ||||
|             enable: true, | ||||
|             logger: true, | ||||
|         }, hexo.config.gzip); | ||||
| 
 | ||||
|         var filter = require('./lib/filter'); | ||||
|         hexo.extend.filter.register('after_render:html', filter.logic_html); | ||||
|         hexo.extend.filter.register('after_render:css', filter.logic_css); | ||||
|         hexo.extend.filter.register('after_render:js', filter.logic_js); | ||||
|         hexo.extend.filter.register('before_exit', filter.logic_gzip); | ||||
|     } | ||||
| //}
 | ||||
|  |  | |||
|  | @ -6,7 +6,8 @@ var CleanCSS = require('clean-css'), | |||
|     streamToArray = require('stream-to-array'); | ||||
| var Promise = require('bluebird'); | ||||
| var minimatch = require('minimatch'); | ||||
| 
 | ||||
| var zlib = require('zlib'); | ||||
| var fs = require('fs'); | ||||
| 
 | ||||
| function logic_html(str, data) { | ||||
|     var hexo = this, | ||||
|  | @ -93,8 +94,47 @@ function logic_js(str, data) { | |||
|     return result.code; | ||||
| } | ||||
| 
 | ||||
| function logic_gzip() { | ||||
|     var hexo = this, | ||||
|         options = hexo.config.gzip; | ||||
|     // Return if disabled.
 | ||||
|     if (false === options.enable) return; | ||||
| 
 | ||||
|     var publicFolder = hexo.base_dir+'public'; | ||||
| 
 | ||||
|     var compressFile = function (currentPath) { | ||||
|       var files = fs.readdirSync(currentPath); | ||||
|       for (var i in files) { | ||||
|         var currentFile = currentPath + '/' + files[i]; | ||||
|         var stats = fs.statSync(currentFile); | ||||
|         if (stats.isFile()) { | ||||
|           if(currentFile.endsWith(".htm") || | ||||
|               currentFile.endsWith(".html") || | ||||
|               currentFile.endsWith(".js") || | ||||
|               currentFile.endsWith(".css") || | ||||
|               currentFile.endsWith(".txt")) { | ||||
|             var gzip = zlib.createGzip('level=9'); | ||||
|             var inp = fs.createReadStream(currentFile); | ||||
|             var out = fs.createWriteStream(currentFile+'.gz'); | ||||
|             inp.pipe(gzip).pipe(out); | ||||
| 
 | ||||
|             if (options.logger) { | ||||
|               var log = hexo.log || console.log; | ||||
|               log.log('Compress the file: %s', currentFile); | ||||
|             } | ||||
|           } | ||||
|         } | ||||
|         else if (stats.isDirectory()) { | ||||
|           compressFile(currentFile); | ||||
|         } | ||||
|       } | ||||
|     }; | ||||
|     compressFile(publicFolder); | ||||
| } | ||||
| 
 | ||||
| module.exports = { | ||||
|     logic_html: logic_html, | ||||
|     logic_css: logic_css, | ||||
|     logic_js: logic_js, | ||||
|     logic_gzip: logic_gzip, | ||||
| }; | ||||
|  |  | |||
							
								
								
									
										12
									
								
								package.json
								
								
								
								
							
							
						
						
									
										12
									
								
								package.json
								
								
								
								
							|  | @ -1,7 +1,7 @@ | |||
| { | ||||
|   "name": "hexo-yam", | ||||
|   "description": "Yet Another Minifier. Minify html, js and css", | ||||
|   "version": "0.3.0", | ||||
|   "description": "Yet Another Minifier. Minify and compress html, js and css", | ||||
|   "version": "0.4.0", | ||||
|   "readme": "README.md", | ||||
|   "main": "index.js", | ||||
|   "directories": { | ||||
|  | @ -12,6 +12,7 @@ | |||
|   }, | ||||
|   "author": "weyusi", | ||||
|   "license": "MIT", | ||||
|   "homepage": "https://github.com/weyusi/hexo-yam", | ||||
|   "repository": { | ||||
|     "type": "git", | ||||
|     "url": "https://github.com/weyusi/hexo-yam.git" | ||||
|  | @ -29,7 +30,10 @@ | |||
|     "html", | ||||
|     "js", | ||||
|     "css", | ||||
|     "hexo", | ||||
|     "minify" | ||||
|     "minify", | ||||
|     "compress", | ||||
|     "gzip", | ||||
|     "hexo-yam", | ||||
|     "hexo" | ||||
|   ] | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue