1+ import { src , dest , watch , series , parallel } from 'gulp' ;
2+ import less from 'gulp-less' ;
3+ import rename from 'gulp-rename' ;
4+ import minifycss from 'gulp-clean-css' ;
5+ import autoprefixer from 'gulp-autoprefixer' ; // 直接导入,无需 .default
6+ import uglify from 'gulp-uglify' ;
7+ import jshint from 'gulp-jshint' ;
8+ import stylish from 'jshint-stylish' ;
9+ import notify from 'gulp-notify' ;
10+ import plumber from 'gulp-plumber' ;
11+ import htmlclean from 'gulp-htmlclean' ;
12+ import htmlmin from 'gulp-htmlmin' ;
13+ import rev from 'gulp-rev-append' ;
14+ import path from 'path' ;
15+
16+ const paths = {
17+ root : './' ,
18+ source : './themes/hexo-theme-snippet/source/'
19+ } ;
20+
21+ /*====================================================
22+ 开发任务
23+ ====================================================*/
24+
25+ // 编译 Less -> CSS
26+ function compileLess ( ) {
27+ return src ( paths . source + 'css/less/_style.less' )
28+ . pipe (
29+ plumber ( {
30+ errorHandler : notify . onError ( 'Error: <%= error.message %>' )
31+ } )
32+ )
33+ . pipe ( less ( ) )
34+ . pipe ( rename ( { basename : 'style' } ) )
35+ . pipe ( dest ( paths . source + 'css' ) )
36+ . pipe ( notify ( { message : 'LESS 编译完成' , disableHtml : true } ) ) ;
37+ }
38+
39+ // 校验 JS 代码
40+ function lintJs ( ) {
41+ return src ( paths . source + 'js/*.js' )
42+ . pipe ( jshint ( ) )
43+ . pipe ( jshint . reporter ( stylish ) )
44+ . pipe ( dest ( paths . source + 'js/' ) )
45+ . pipe ( notify ( { message : 'JS 校验完成' , disableHtml : true } ) ) ;
46+ }
47+
48+ // 开发模式监听
49+ function watchFiles ( ) {
50+ watch ( paths . source + 'css/less/*.less' , compileLess ) ;
51+ watch ( paths . source + 'js/*.js' , lintJs ) ;
52+ }
53+
54+ /*====================================================
55+ 生产构建任务
56+ ====================================================*/
57+
58+ // 压缩 CSS 并添加前缀
59+ function optimizeCss ( ) {
60+ return src ( './public/**/*.css' )
61+ . pipe (
62+ autoprefixer ( {
63+ overrideBrowserslist : [
64+ 'last 10 versions' ,
65+ 'Firefox >= 20' ,
66+ 'Opera >= 36' ,
67+ 'ie >= 9' ,
68+ 'Android >= 4.0'
69+ ] ,
70+ cascade : true ,
71+ remove : false
72+ } )
73+ )
74+ . pipe ( minifycss ( ) )
75+ . pipe ( dest ( './public' ) )
76+ . pipe ( notify ( { message : 'CSS 优化完成' , disableHtml : true } ) ) ;
77+ }
78+
79+ // 压缩 JS
80+ function optimizeJs ( ) {
81+ return src ( './public/js/*.js' )
82+ . pipe ( uglify ( ) )
83+ . pipe ( dest ( './public/js' ) )
84+ . pipe ( notify ( { message : 'JS 压缩完成' , disableHtml : true } ) ) ;
85+ }
86+
87+ // 压缩 HTML
88+ function optimizeHtml ( ) {
89+ return src ( './public/**/*.html' )
90+ . pipe ( htmlclean ( ) )
91+ . pipe (
92+ htmlmin ( {
93+ removeComments : true ,
94+ collapseWhitespace : true ,
95+ minifyJS : true ,
96+ minifyCSS : true ,
97+ minifyURLs : true
98+ } )
99+ )
100+ . pipe ( dest ( './public' ) ) ;
101+ }
102+
103+ // 添加版本号
104+ function addVersion ( ) {
105+ return src ( './public/**/*.html' )
106+ . pipe ( rev ( ) )
107+ . pipe ( dest ( './public' ) ) ;
108+ }
109+
110+ /*====================================================
111+ 任务组合
112+ ====================================================*/
113+
114+ // 完整构建流程
115+ const build = series (
116+ parallel ( optimizeCss , optimizeJs ) ,
117+ addVersion ,
118+ optimizeHtml
119+ ) ;
120+
121+ // 开发模式
122+ const dev = series (
123+ parallel ( compileLess , lintJs ) ,
124+ watchFiles
125+ ) ;
126+
127+ // 导出任务
128+ export { build , dev } ;
129+ export default build ; // gulp 默认执行 build
0 commit comments