-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
90 lines (79 loc) · 2.83 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
const gulp = require('gulp'); // to import gulp from gulp modules
const concat = require('gulp-concat'); // to concat files
const prefix = require('gulp-autoprefixer'); // to add css prefixes
const sass = require('gulp-sass'); // to manipulate sass file
const livereload = require('gulp-livereload'); // to make live reload
const sourcemaps = require('gulp-sourcemaps'); // to create a map
const uglify = require('gulp-uglify'); // to minify js scripts does not support ecma script so I replace it with terser
const webserver = require('gulp-webserver'); // to start a server
var debug = require("gulp-debug"); // for debugging
const del = require("del"); // to delete files and folders
const terser = require('gulp-terser');
// html task
function html(){
return gulp.src('src/index.html')
.pipe(gulp.dest('dist/'))
.pipe(debug())
.pipe(livereload());
}
// // css task
function css(){
return gulp.src('src/public/scss/styles.scss')
.pipe(sourcemaps.init({loadMaps: true})) // init the source map
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(prefix('last 2 versions'))
// We do not need it at this stage but we can use it
//later if we have other css libraries that we want to add
.pipe(concat('main.css'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('dist/public/css'))
.pipe(livereload());
}
// js task
function js(){
return gulp.src('src/public/js/*.js')
.pipe(concat('main.js'))
//.pipe(uglify())
.pipe(terser())
.pipe(gulp.dest('dist/public/js'))
.pipe(livereload());
}
// vendor task
function vendor_module(){
return gulp.src('src/public/vendor/**/*')
.pipe(gulp.dest('dist/public/vendor'))
.pipe(livereload());
}
// Clean vendor
function vendor_clean() {
return del(["dist/public/vendor/"]);
}
// Server Task
function server() {
gulp.src('dist') // <-- your app folder
.pipe(webserver({
livereload: true,
open: true,
port: 3000 // set a port to avoid conflicts with other local apps
}));
}
// Watch files
function watchFiles() {
livereload.listen();
gulp.watch("src/index.html", html);
gulp.watch(['src/public/scss/*.scss','src/public/scss/**/*.scss'], css);
gulp.watch('src/public/js/*.js', js);
gulp.watch("src/public/vendor/**/*", gulp.parallel(vendor_clean, vendor_module));
}
// Define complex tasks
const vendor = gulp.series(vendor_clean, vendor_module);
const build = gulp.series(html,css,js,vendor);
const watch = gulp.series(build, gulp.parallel(watchFiles, server));
// Export tasks
exports.html = html;
exports.css = css;
exports.js = js;
exports.vendor = vendor;
exports.build = build;
exports.watch = watch;
exports.default = watch;