-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
111 lines (98 loc) · 2.93 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const autoprefixer = require('gulp-autoprefixer');
const browserSync = require('browser-sync').create();
const cleanCSS = require('gulp-clean-css');
const concat = require('gulp-concat');
const del = require('del');
const gulp = require('gulp');
const gulpIf = require('gulp-if');
const gzip = require('gulp-gzip');
const htmlmin = require('gulp-htmlmin');
const notify = require('gulp-notify');
const plumber = require('gulp-plumber');
const sass = require('gulp-sass')(require('sass'));
const uglify = require('gulp-uglify');
const options = {
dist: './dist',
src: {
assets: ['./src/assets/**'],
html: './src/index.html',
js: ['./src/scripts/main.js', './src/scripts/modules/**'],
sass: ['./src/sass/main.scss']
},
watch: {
sass: ['./src/sass/**/*.scss']
}
};
let isProduction = false;
function errorHandler() {
return plumber({
errorHandler: (err) => {
notify.onError({
title: 'Gulp error in ' + err.plugin,
message: err.toString()
})(err);
}
});
}
gulp.task('clean', () => {
return del(options.dist);
});
gulp.task('assets', () => {
return gulp.src(options.src.assets)
.pipe(gulp.dest(options.dist));
});
gulp.task('html', () => {
return gulp.src(options.src.html)
.pipe(gulpIf(isProduction, htmlmin({
collapseWhitespace: true,
removeComments: true
})))
.pipe(gulp.dest(options.dist))
.pipe(gulpIf(isProduction, gzip()))
.pipe(gulp.dest(options.dist))
.pipe(browserSync.stream());
});
gulp.task('sass', () => {
return gulp.src(options.src.sass)
.pipe(gulpIf(!isProduction, errorHandler()))
.pipe(sass({
outputStyle: 'expanded'
}))
.pipe(autoprefixer())
.pipe(gulpIf(isProduction, cleanCSS()))
.pipe(gulp.dest(options.dist))
.pipe(gulpIf(isProduction, gzip()))
.pipe(gulp.dest(options.dist))
.pipe(browserSync.stream());
});
gulp.task('scripts', () => {
return gulp.src(options.src.js)
.pipe(concat('main.js'))
.pipe(gulpIf(isProduction, uglify()))
.pipe(gulp.dest(options.dist))
.pipe(gulpIf(isProduction, gzip()))
.pipe(gulp.dest(options.dist))
.pipe(browserSync.stream());
});
gulp.task('browserSync', () => {
browserSync.init({
server: {
baseDir: options.dist
}
});
})
gulp.task('watch', () => {
gulp.watch(options.src.html, gulp.series('html'));
gulp.watch(options.src.js, gulp.series('scripts'));
gulp.watch(options.watch.sass, gulp.series('sass'));
})
gulp.task('dev', gulp.series(
'assets',
gulp.parallel('html', 'sass', 'scripts'),
gulp.parallel('watch', 'browserSync')
))
gulp.task('prod', (done) => {
isProduction = true;
gulp.series('clean', 'assets', 'html', 'sass', 'scripts')(done)
});
gulp.task('default', gulp.series('prod'))