Gulp 压缩静态资源

使用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 目录中的静态资源文件进行压缩。

遇到的坑:

  1. 提示必须指定,是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. (/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. (/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

你可能感兴趣的:(个人博客)