-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
gulpfile.js
executable file
·187 lines (173 loc) · 4.99 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var gulp = require("gulp");
var sass = require("gulp-sass")(require("sass"));
var postcss = require("gulp-postcss");
var cssvariables = require("postcss-css-variables");
var cleanCSS = require("gulp-clean-css");
var autoprefixer = require("autoprefixer");
var rename = require("gulp-rename");
var wait = require("gulp-wait");
var merge = require("merge2");
var concat = require("gulp-concat");
var uglify = require("gulp-uglify");
var jshint = require("gulp-jshint");
// 错误处理
var plumber = require("gulp-plumber");
var stylish = require("jshint-stylish");
var del = require("del");
var zip = require("gulp-zip");
var beautify = require("gulp-jsbeautifier");
var browserSync = require("browser-sync").create();
gulp.task("beautify", () =>
gulp.src(["./*.css", "./*.html", "./*.js"]).pipe(beautify())
);
// JS检查
gulp.task("lint", function () {
return gulp
.src("./assets/js/index.js")
.pipe(jshint())
.pipe(jshint.reporter("default"));
});
gulp.task("css", function () {
var sassStream = gulp
.src("./assets/scss/screen.scss")
.pipe(plumber())
.pipe(wait(100))
.pipe(sass({ outputStyle: "expanded" }).on("error", sass.logError))
.pipe(postcss([autoprefixer(), cssvariables({ preserve: true })]));
var cssStream = gulp
.src(
[
"./assets/css/bootstrap.min.css",
"./assets/css/hl-styles/atom-one-dark.min.css",
],
{ allowEmpty: true }
)
.pipe(concat("css-files.css"));
return merge(cssStream, sassStream)
.pipe(concat("app.bundle.min.css"))
.pipe(
cleanCSS({
level: { 1: { specialComments: 0 } },
compatibility: "ie9",
})
)
.pipe(rename("bundle-css.hbs"))
.pipe(gulp.dest("./partials/styles"))
.pipe(browserSync.stream());
});
gulp.task("ampcss", function () {
return gulp
.src([
"./assets/scss/ampstyle-light.scss",
"./assets/scss/ampstyle-dark.scss",
])
.pipe(plumber())
.pipe(wait(100))
.pipe(sass({ outputStyle: "expanded" }).on("error", sass.logError))
.pipe(postcss([autoprefixer(), cssvariables()]))
.pipe(
cleanCSS({
level: { 1: { specialComments: 0 } },
compatibility: "ie9",
})
)
.pipe(rename({ extname: ".hbs" }))
.pipe(gulp.dest("./partials/styles"));
});
gulp.task("concat-js", function () {
return gulp
.src(
[
"./assets/js/vendor/jquery-3.5.1.min.js",
"./assets/js/vendor/jquery.fitvids.js",
// './assets/js/vendor/highlight.pack.js',
"./assets/js/vendor/fuse.min.js",
"./assets/js/vendor/medium-zoom.min.js",
"./assets/js/vendor/clipboard.min.js",
"./assets/js/vendor/jquery.disqusloader.js",
"./assets/js/vendor/jquery.toc.js",
"./assets/js/vendor/lazy.js",
"./assets/js/vendor/prism.js",
"./assets/js/index.js",
],
{ allowEmpty: true }
)
.pipe(plumber())
.pipe(concat("app.bundle.min.js"))
.pipe(
uglify({
compress: {
drop_console: true,
},
})
) // 压缩成一行
.pipe(gulp.dest("./assets/js"));
});
gulp.task(
"watch",
gulp.series("css", "ampcss", "concat-js", function () {
browserSync.init({
proxy: "http://localhost:2368",
});
gulp
.watch(
[
"./assets/scss/**/*.scss",
"!./assets/scss/ampstyle-light.scss",
"!./assets/scss/ampstyle-dark.scss",
],
{ allowEmpty: true }
)
.on("change", gulp.series("css"));
gulp
.watch([
"./assets/scss/ampstyle-light.scss",
"./assets/scss/ampstyle-dark.scss",
])
.on("change", gulp.series("ampcss"));
gulp
.watch(["./assets/js/**/*.js", "!./assets/js/app.bundle.min.js"], {
allowEmpty: true,
})
.on("change", gulp.series("concat-js", browserSync.reload));
gulp.watch("./**/*.hbs").on("change", browserSync.reload);
})
);
gulp.task("clean", function () {
return del(["./build", "./dist"]);
});
gulp.task(
"build",
gulp.series("clean", "lint", "css", "concat-js", function () {
var targetDir = "build/";
return gulp
.src([
"**",
"!assets/scss",
"!assets/scss/**/*",
"!assets/css",
"!assets/css/**/*",
"assets/js/**",
"!assets/js/vendor",
"!assets/js/vendor/**/*",
"!node_modules",
"!node_modules/**",
"!build",
"!build/**",
"!dist",
"!dist/**",
])
.pipe(gulp.dest(targetDir));
})
);
gulp.task("zip", function () {
var targetDir = "dist/";
var themeName = require("./package.json").name;
var filename = themeName + ".zip";
return gulp
.src(["./build/**/*"])
.pipe(zip(filename))
.pipe(gulp.dest(targetDir));
});
gulp.task("upload", gulp.series("clean", "build", "zip"));
gulp.task("default", gulp.parallel("watch"));