-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
gulpfile.js
81 lines (74 loc) · 2.04 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
const { src, dest, watch, series, parallel } = require('gulp');
const gulpLoadPlugins = require('gulp-load-plugins');
const browserSync = require('browser-sync').create();
const sass = require('gulp-sass')(require('sass'));
const postcss = require('gulp-postcss');
const postcssPresetEnv = require('postcss-preset-env');
// Load all Gulp plugins into the variable g
const g = gulpLoadPlugins();
// Styles Task
function styles() {
return src('src/sass/**/*.scss')
.pipe(g.plumber({
errorHandler: function (error) {
console.log(error.message);
this.emit('end');
}
}))
.pipe(g.sourcemaps.init())
.pipe(sass().on('error', sass.logError))
// Use PostCSS with preset-env
.pipe(postcss([
postcssPresetEnv(/* plugin options */)
]))
.pipe(g.rename({ suffix: '.min' }))
.pipe(g.sourcemaps.write('.'))
.pipe(dest('dist/css/'))
.pipe(browserSync.stream());
}
// Scripts Task
function scripts() {
return src('src/scripts/**/*.js')
.pipe(g.plumber({
errorHandler: function (error) {
console.log(error.message);
this.emit('end');
}
}))
.pipe(g.sourcemaps.init())
.pipe(g.concat('bundle.js'))
.pipe(dest('dist/scripts/'))
.pipe(g.rename({ suffix: '.min' }))
.pipe(g.uglify())
.pipe(g.sourcemaps.write('.'))
.pipe(dest('dist/scripts/'))
.pipe(browserSync.stream());
}
// HTML Task
function html() {
return src('src/*.html')
.pipe(dest('dist/'))
.pipe(browserSync.stream());
}
// Browser Sync Task
function serve() {
browserSync.init({
server: {
baseDir: './dist/'
}
});
}
// Watch Task
function watchFiles() {
watch('src/sass/**/*.scss', styles);
watch('src/scripts/**/*.js', scripts);
watch('src/*.html', html);
// Reloads the browser whenever HTML or JS files change
watch('src/**/*.html').on('change', browserSync.reload);
watch('src/scripts/**/*.js').on('change', browserSync.reload);
}
// Default Gulp Task
exports.default = series(
parallel(html, styles, scripts),
parallel(serve, watchFiles)
);