Skip to content

Commit cf759df

Browse files
committed
build
1 parent 6ed91fd commit cf759df

File tree

7 files changed

+144
-7
lines changed

7 files changed

+144
-7
lines changed

.github/workflows/deploy.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@ jobs:
1919
- name: Setup Node.js
2020
uses: actions/setup-node@v3
2121
with:
22-
node-version: '18'
22+
node-version: '20'
2323

2424
- name: Install Hexo and dependencies
2525
run: |
26-
npm install -g hexo-cli
26+
npm install -g hexo-cli gulp-cli
2727
npm install
2828
2929
- name: Build static files
3030
run: |
3131
hexo clean
3232
hexo generate
33+
gulp build # 执行 Gulp 构建任务
3334
3435
- name: Create deployment info file
3536
run: |

gulpfile.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

gulpfile.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import gulp from 'gulp';
2+
import gulpTasks from './themes/hexo-theme-snippet/gulpfile.mjs';
3+
4+
// 执行默认任务
5+
gulp.task('default', gulpTasks);
6+
7+

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"author": "shenliyang",
66
"license": "MIT",
77
"private": true,
8+
"type": "module",
89
"scripts": {
910
"test": "echo \"Error: no test specified\" && exit 1",
1011
"deploy": "hexo deploy",
@@ -24,7 +25,7 @@
2425
},
2526
"dependencies": {
2627
"ejs": "^3.1.10",
27-
"gulp-cli": "^3.1.0",
28+
"gulp-cli": "3",
2829
"hexo": "^7.3.0",
2930
"hexo-abbrlink": "^2.2.1",
3031
"hexo-deployer-git": "^4.0.0",
@@ -40,7 +41,7 @@
4041
"hexo-server": "^3.0.0"
4142
},
4243
"devDependencies": {
43-
"gulp": "^5.0.1",
44+
"gulp": "5",
4445
"gulp-autoprefixer": "^9.0.0",
4546
"gulp-clean-css": "^4.3.0",
4647
"gulp-htmlclean": "^2.7.22",

pnpm-lock.yaml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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

Comments
 (0)