-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
254 lines (228 loc) · 6.88 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
const gulp = require("gulp");
const util = require("gulp-util");
const plumber = require("gulp-plumber");
const imagemin = require("gulp-imagemin");
const clean = require("gulp-clean");
const changed = require("gulp-changed");
const sass = require("gulp-sass");
const rollup = require("./buildtools/rollup");
const lunr = require("hugo-lunr-zh");
const sourcemaps = require("gulp-sourcemaps");
const postcss = require("gulp-postcss");
const purifycss = require("gulp-purifycss");
const htmlmin = require("gulp-htmlmin");
const rev = require("gulp-rev");
const replace = require("gulp-rev-replace");
const $if = require("gulp-if");
const run = require("run-sequence");
const pngquant = require("imagemin-pngquant");
const cp = require("child_process");
const fs = require("fs");
const toml = require("toml");
const bs = require("browser-sync").create();
const c = Object.assign({}, require("./package").config);
const siteConf = toml.parse(fs.readFileSync("./config.toml"));
const srcDir = "assets";
const devDir = ".tmp";
const prodDir = siteConf.publishDir || "public";
const themeDir = "themes/canoe";
let env = "dev"; // dev , prod , theme
gulp.task("clean", () =>
gulp.src([devDir, prodDir], { read: false }).pipe(clean())
);
gulp.task("clean:theme", () =>
gulp.src(`${themeDir}/{layouts,static}`).pipe(clean())
);
gulp.task("copy:static", () => {
const destDir =
env === "dev" ? devDir : env === "prod" ? prodDir : `${themeDir}/static`;
return gulp.src(`${srcDir}/static/**/*`).pipe(gulp.dest(destDir));
});
// => dev
gulp.task("style", () => {
// cover the global destDir
const destDir = env === "theme" ? `${themeDir}/static/css` : `${devDir}/css`;
return gulp
.src(`${srcDir}/style/**/*.{scss,css}`)
.pipe(plumber())
.pipe(changed(destDir))
.pipe($if(env === "dev", sourcemaps.init()))
.pipe(sass().on("error", sass.logError))
.pipe($if(env === "dev", sourcemaps.write()))
.pipe(gulp.dest(destDir))
.pipe(bs.stream({ match: "**/*.css" }));
});
//=> dev
gulp.task("script", () => {
const destDir = env === "theme" ? `${themeDir}/static/js` : `${devDir}/js`;
return rollup(
[
{
entry: `./${srcDir}/script/polyfill.ts`,
dest: `./${destDir}/polyfill.js`
},
{ entry: `./${srcDir}/script/index.ts`, dest: `./${destDir}/index.js` },
{ entry: `./${srcDir}/script/canvas.ts`, dest: `./${destDir}/canvas.js` }
// { entry: `${srcDir}/script/worker.ts`, dest: `./${destDir}/worker.js` }
],
env === "dev"
);
});
// => dev
gulp.task("image", () => {
const destDir = env === "theme" ? `${themeDir}/static/img` : `${devDir}/img`;
return gulp
.src(`${srcDir}/image/**/*.{png,jpg}`)
.pipe(changed(destDir))
.pipe(
$if(
env !== "dev",
imagemin({
progressive: true,
use: [pngquant({ quality: 90 })]
})
)
)
.pipe(gulp.dest(destDir))
.pipe(bs.stream({ match: "**/*.{png,jpg}" }));
});
gulp.task("pimg", () => {
const destDir = `${devDir}/pimg`;
return gulp
.src(`pimg/**/*.{png,jpg}`)
.pipe(changed(destDir))
.pipe(
$if(
env !== "dev",
imagemin({
progressive: true,
use: [pngquant({ quality: 90 })]
})
)
)
.pipe(gulp.dest(destDir))
.pipe(bs.stream({ match: "**/*.{png,jpg}" }));
});
// => devDir
gulp.task("hugo", cb => {
const prodArgs = ["-d", `./${devDir}`];
const devArgs = ["-d", `./${devDir}`, "-w", "-b", "/."];
const hugo = cp.spawn("hugo", env === "dev" ? devArgs : prodArgs);
hugo.stdout.on("data", data => util.log(data.toString()));
hugo.stderr.on("data", data => util.log("error: ", data.toString()));
hugo.on("exit", code => {
util.log("hugo process exited with code", code);
env !== "dev" && cb();
});
// env == dev is in watch mode
env === "dev" && cb();
});
//devDir => prodDir
gulp.task("rev", () => {
const revExts = "png,svg,jpg,css,js";
return gulp
.src(`${devDir}/**/*.{${revExts}}`)
.pipe(rev())
.pipe(gulp.dest(prodDir))
.pipe(rev.manifest("rev-manifest.json"))
.pipe(gulp.dest(devDir));
});
gulp.task('generate-service-worker', function(callback) {
var swPrecache = require('sw-precache');
var rootDir = 'docs';
swPrecache.write(`${rootDir}/service-worker.js`, {
staticFileGlobs: [rootDir + '/**/*.{js,html,css,png,jpg,gif,svg,eot,ttf,woff,json,woff2}'],
stripPrefix: rootDir,
runtimeCaching: [{
urlPattern: "/*",
handler: 'cacheFirst',
options: { origin: "cdn.bootcss.com" }
}, {
urlPattern: "/*",
handler: 'cacheFirst',
options: { origin: "i.loli.net" }
}, {
urlPattern: "/*",
handler: 'cacheFirst',
options: { origin: "fonts.cat.net" }
}, {
urlPattern: "/*",
handler: 'cacheFirst',
options: { origin: "gstatic.cat.net" }
}]
}, callback);
});
// prodDir => prodDir
gulp.task("ref", () => {
const refExts = "html,css,js";
return gulp
.src(`${prodDir}/**/*.{${refExts}}`)
.pipe(replace({ manifest: gulp.src(`${devDir}/rev-manifest.json`) }))
.pipe(gulp.dest(prodDir));
});
// devDir => target
gulp.task("htmlmin", () => {
return gulp
.src(`${devDir}/**/*.{html,xml}`)
.pipe($if("*.html", htmlmin({ collapseWhitespace: true })))
.pipe(gulp.dest(prodDir));
});
gulp.task("lunr", () => {
const destDir = env === "dev" ? devDir : prodDir;
const option = {
output: `${destDir}/index.json`
};
if (siteConf.metaDataFormat === "yaml") {
option.matterDelims = "---";
option.matterType = "yaml";
}
return lunr(option);
});
gulp.task("purifycss", () => {
const base = env === "theme" ? `${themeDir}` : `${devDir}`;
return gulp
.src(`${base}/**/*.css`)
.pipe(purifycss([`${base}/**/*.html`, `${base}/**/*.js`]))
.pipe(
postcss([
require("autoprefixer")({ browsers: c.browserslist }),
require("cssnano")()
])
)
.pipe(gulp.dest(base));
});
gulp.task("build:dev", cb => {
run("hugo", ["style", "script", "image", "pimg", "copy:static", "lunr"], cb);
});
gulp.task("build", ["clean"], cb => {
env = "prod";
run("build:dev", "purifycss", ["rev", "htmlmin"], "ref", "generate-service-worker", cb);
});
gulp.task("serve", ["build:dev"], () => {
bs.init({
reloadDebounce: 200,
port: c.port,
server: {
baseDir: devDir
}
});
//watch resources
gulp.watch(`${srcDir}/style/**/*.{scss,css}`, ["style"]);
gulp.watch(`${srcDir}/image/**/*.{png,jpg}`, ["image"]);
gulp.watch(`${srcDir}/pimg/**/*.{png,jpg}`, ["pimg"]);
const toReload = [`${devDir}/**/*.html`, `${devDir}/js/**/*.js`];
gulp.watch(toReload).on("change", () => bs.reload());
});
// theme
gulp.task("copy:layouts", () => {
gulp.src("layouts/**/*.html").pipe(gulp.dest(`${themeDir}/layouts`));
});
gulp.task("theme", cb => {
env = "theme";
run(
["clean", "clean:theme"],
["script", "style", "image", "copy:static", "copy:layouts"],
"purifycss",
cb
);
});