使用gulp 壓縮靜態資源
在項目的根目錄下執行以下命令:
npm install gulp -g
npm install gulp-minify-css gulp-uglify gulp-htmlmin gulp-htmlclean gulp --save
在博客根目錄下新建 gulpfile.js,並填入以下內容:
var gulp = require('gulp');
var minifycss = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var htmlmin = require('gulp-htmlmin');
var htmlclean = require('gulp-htmlclean');
// 壓縮 public 目錄 css
gulp.task('minify-css', function() {
return gulp.src('./public/**/*.css')
.pipe(minifycss())
.pipe(gulp.dest('./public'));
});
// 壓縮 public 目錄 html
gulp.task('minify-html', function() {
return gulp.src('./public/**/*.html')
.pipe(htmlclean())
.pipe(htmlmin({
removeComments: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true,
}))
.pipe(gulp.dest('./public'))
});
// 壓縮 public/js 目錄 js
gulp.task('minify-js', function() {
return gulp.src('./public/**/*.js')
.pipe(uglify())
.pipe(gulp.dest('./public'));
});
// 執行 gulp 命令時執行的任務
gulp.task('default', [
'minify-html','minify-css','minify-js'
]);
執行 gulp 就會根據 gulpfile.js 中的配置,對 public 目錄中的靜態資源文件進行壓縮。
遇到的坑:
- 提示必須指定,是gulp版本的問題,看了網上的解決方案,需要將gulp調整至3.9.1,可在package.json中調整,改好執行npm install
assert.js:378
throw err;
^
AssertionError [ERR_ASSERTION]: Task function must be specified
at Gulp.set [as _setTask] (/home/ZoeyBlogs/node_modules/undertaker/lib/set-task.js:10:3)
at Gulp.task (/home/ZoeyBlogs/node_modules/undertaker/lib/task.js:13:8)
at Object.<anonymous> (/home/ZoeyBlogs/gulpfile.js:31:6)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1103:10)
at Module.load (internal/modules/cjs/loader.js:914:32)
at Function.Module._load (internal/modules/cjs/loader.js:822:14)
at Module.require (internal/modules/cjs/loader.js:956:19)
at require (internal/modules/cjs/helpers.js:74:18)
at execute (/usr/local/lib/node_modules/gulp/node_modules/gulp-cli/lib/versioned/^4.0.0/index.js:36:18) {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
}
在執行gulp,又出現問題了:
fs.js:27
const { Math, Object } = primordials;
^
ReferenceError: primordials is not defined
at fs.js:27:26
at req_ (/home/ZoeyBlogs/node_modules/natives/index.js:143:24)
at Object.req [as require] (/home/ZoeyBlogs/node_modules/natives/index.js:55:10)
at Object.<anonymous> (/home/ZoeyBlogs/node_modules/vinyl-fs/node_modules/graceful-fs/fs.js:1:37)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1103:10)
at Module.load (internal/modules/cjs/loader.js:914:32)
at Function.Module._load (internal/modules/cjs/loader.js:822:14)
at Module.require (internal/modules/cjs/loader.js:956:19)
at require (internal/modules/cjs/helpers.js:74:18)
這個是node版本的問題,看了下網上的解決方案:
npm install -g n
sudo n 11.15.0
設置完後,在執行gulp,發現正常了。
hexo 使用 hexo-all-minifier 壓縮
使用下面的插件配置hexo壓縮靜態資源
https://github.com/chenzhutian/hexo-all-minifier
在博客根目錄的_config.yml中加入
html_minifier:
enable: true
ignore_error: false
exclude:
css_minifier:
enable: true
exclude:
- '*.min.css'
js_minifier:
enable: true
mangle: true
output:
compress:
exclude:
- '*.min.js'
image_minifier:
enable: true
interlaced: false
multipass: false
optimizationLevel: 2
pngquant: false
progressive: false
我的博客:Zoey-Share-Site
參考鏈接:
https://stackoverflow.com/questions/55921442/how-to-fix-referenceerror-primordials-is-not-defined-in-node
http://blog.fcj.one/hexo-gulp-post.html