-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgulpfile.js
96 lines (83 loc) · 2.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const gulp = require('gulp');
const sass = require('gulp-sass')(require('sass'));
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const cssnano = require('gulp-cssnano');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify-es').default;
const browserSync = require('browser-sync');
const reload = browserSync.reload;
const ejs = require("gulp-ejs");
const plumber = require("gulp-plumber");
const rename = require('gulp-rename');
// Sass
gulp.task('sass', function (done) {
gulp.src('sass/**/*.scss')
.pipe(sass().on('error', sass.logError)) // Keep running gulp even though occurred compile error
.pipe(postcss([
autoprefixer({
cascade: false
})
]))
.pipe(cssnano())
.pipe(gulp.dest('build/css'))
.pipe(reload({stream:true}));
done();
});
// Js-concat-uglify
gulp.task('js', function(done) {
gulp.src(['js/**'])
.pipe(uglify({
output:{
comments: /^!/ // Keep some comments
}
}))
.pipe(gulp.dest('build/js'))
.pipe(reload({stream:true}));
done();
});
// ejs
var fs = require('fs');
var json = JSON.parse(fs.readFileSync('site.json')); // parse json
gulp.task('ejs', function(done) {
gulp.src(['templates/**/*.ejs','!' + 'templates/**/_*.ejs']) // Don't build html which starts from underline
.pipe(plumber())
.pipe(ejs(json))
.pipe(rename({
extname: '.html'
}))
.pipe(gulp.dest('build'))
done();
});
// Static server
gulp.task('browser-sync', function(done) {
browserSync({
server: {
baseDir: 'build/', // Target directory
index : 'index.html', // index file
https: true
}
});
done();
});
// watch files
gulp.task('watch-files', function(done) {
gulp.watch('sass/**/*.scss', gulp.task('sass'));
gulp.watch('js/**', gulp.task('js'));
gulp.watch('build/**/*.html', gulp.task('bs-reload'));
gulp.watch('templates/**/*.ejs', gulp.task('ejs'));
gulp.watch('site.json', gulp.task('ejs'));
done();
});
// Reload all browsers
gulp.task('bs-reload', function (done) {
browserSync.reload();
done();
});
// Build
gulp.task('build', gulp.series('ejs', gulp.parallel('sass', 'js')));
// Task for `gulp` command
gulp.task('default', gulp.series('browser-sync', 'watch-files', function(done){
done();
console.log('Default all task done!');
}));