-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
95 lines (76 loc) · 2.11 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
const gulp = require('gulp');
const sass = require('gulp-sass')(require('node-sass'));
const fileinclude = require('gulp-file-include');
const server = require('browser-sync').create();
const paths = {
src: './src',
dest: './build'
};
// Compile scss into css
function compileSCSS(){
// 1. Find my scss file
return gulp.src(`${paths.src}/assets/scss/**/*.scss`)
// 2. Pass that file through sass compiler
.pipe(sass().on('error', sass.logError))
// 3. Compiles CSS to the destination folder
.pipe(gulp.dest(`${paths.dest}/assets/styles/`))
// 4. Stream changes
.pipe(server.stream());
}
// Copy assets
function copyAssets() {
gulp.src([
`${paths.src}/assets/**/*`,
`!${paths.src}/assets/scss`, // ignore
`!${paths.src}/assets/scss/*.scss` // ignore
])
.pipe(gulp.dest(`${paths.dest}/assets`));
}
// HTML include functionality
function includeHTML(){
return gulp.src([
`${paths.src}/**/*.html`,
`!${paths.src}/templates/*.html` // ignore
])
.pipe(fileinclude({
prefix: '@@',
basepath: `${paths.src}/templates`
}))
.pipe(gulp.dest(`${paths.dest}/`));
}
// Watches changes on SCSS, HTML and JS files using browserSync
function watch(){
// Init serve files from the build folder
server.init({
server: {
baseDir: paths.dest
}
});
// Build and reload for the first time
build();
server.reload();
// Watch HTML Task
gulp.watch([
`${paths.src}/**/*.html`,
`!${paths.src}/build/*` // ignore
]).on('change', function() {
includeHTML();
copyAssets();
server.reload();
});
// Watch SCSS Task
gulp.watch(`${paths.src}/assets/scss/**/*.scss`, compileSCSS);
// Watch JS Task
gulp.watch(`${paths.src}/assets/scripts/**/*.js`).on('change', function(){
copyAssets();
server.reload();
});
}
// Build for production
async function build(){
await includeHTML();
await compileSCSS();
await copyAssets();
}
exports.watch = watch;
exports.build = build;