-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
73 lines (62 loc) · 1.49 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
const { watch, src, dest, series, parallel } = require('gulp');
const server = require('browser-sync').create();
const fs = require('fs');
const rev = require('gulp-rev');
const revRewrite = require('gulp-rev-rewrite');
const fileInclude = require('gulp-file-include');
const del = require('del');
// clean folder dist
const clean = () => del(['./dist']);
// run browser sync
function reload() {
server.reload();
}
function includeHtml() {
return src([
'./src/*.html',
'!./src/includes/*.html'
])
.pipe(fileInclude())
.pipe(dest('./dist'));
}
async function copyAssets() {
return src([
'./src/assets/**/*'
])
.pipe(dest('./dist/assets'));
}
async function buildAndReload() {
await includeHtml();
await copyAssets();
reload();
}
exports.default = async function() {
server.init({
server: {
baseDir: './dist'
}
})
buildAndReload();
watch(
['./src/*.html','./src/includes/*.html','./src/assets/**/*'],
series(clean,buildAndReload)
);
}
// Create hash for cache js and css file
// Step 1
function revision() {
return src('dist/assets/**/*.{css,js}')
.pipe(rev())
.pipe(dest('dist/assets'))
.pipe(rev.manifest())
.pipe(dest('dist/assets'));
}
// Step 2
function rewrite() {
const manifest = fs.readFileSync('dist/assets/rev-manifest.json');
return src('dist/**/*.html')
.pipe(revRewrite({ manifest }))
.pipe(dest('dist'));
}
// Run 'gulp build' for deploying to production
exports.build = series(revision, rewrite);