压缩静态资源示例

  |     |  

使用 gulp 压缩静态资源

在站点的根目录下执行以下命令:

1
2
npm install gulp -g
npm install gulp-minify-css gulp-uglify gulp-htmlmin gulp-htmlclean gulp --save

在博客根目录下新建 gulpfile.js,并填入以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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'
]);

执行 hexo clean & hexo g && gulp 就会根据 gulpfile.js 中的配置,对 public 目录中的静态资源文件进行压缩。

遇到的坑:

  1. 提示必须指定,是gulp版本的问题,看了网上的解决方案,需要将gulp调整至3.9.1,可在package.json中调整,改好执行npm install
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    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,又出现问题了:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    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版本的问题,看了下网上的解决方案:
    1
    2
    npm install -g n
    sudo n 11.15.0
    设置完后,在执行gulp,发现正常了。

使用hexo-all-minifier 压缩

使用下面的插件配置hexo压缩静态资源

https://github.com/chenzhutian/hexo-all-minifier

在博客根目录的_config.yml中加入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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

参考链接:
https://stackoverflow.com/questions/55921442/how-to-fix-referenceerror-primordials-is-not-defined-in-node
http://blog.fcj.one/hexo-gulp-post.html

文章目录
  1. 1. 使用 gulp 压缩静态资源
  2. 2. 使用hexo-all-minifier 压缩
作者共写了51.2k个字 本站总访问量  |   您是访问本站的第个小伙伴