forked from slavanga/baseguide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
127 lines (115 loc) · 3.31 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const gulp = require('gulp');
const $ = require('gulp-load-plugins')();
const log = require('fancy-log');
const autoprefixer = require('autoprefixer');
const browserSync = require('browser-sync').create();
const config = {
'src': '',
'dest': 'dist/',
'minify': true,
'sourcemaps': false
};
// HTML
function html() {
return gulp.src(config.src + '*.html')
.pipe(browserSync.stream());
}
// Compile and autoprefix stylesheets
function styles() {
return gulp.src(config.src + 'scss/*.scss')
.pipe($.if(config.sourcemaps, $.sourcemaps.init()))
.pipe($.sass({
precision: 8,
outputStyle: 'expanded'
}).on('error', $.sass.logError))
.pipe($.postcss([
autoprefixer()
]))
.pipe($.if(config.sourcemaps, $.sourcemaps.write()))
.pipe(gulp.dest(config.dest + 'css'))
.pipe(browserSync.stream())
.pipe($.if(config.minify, $.cleanCss()))
.pipe($.if(config.sourcemaps, $.sourcemaps.write()))
.pipe($.if(config.minify, $.rename({suffix: '.min'})))
.pipe($.if(config.minify, gulp.dest(config.dest + 'css')))
.pipe(browserSync.stream());
}
// Lint stylesheets
function stylelint() {
return gulp.src(config.src + 'scss/**/*.scss')
.pipe($.postcss([
require('stylelint')({fix: true})
], {
syntax: require('postcss-scss')
}))
.pipe(gulp.dest(config.src + 'scss'));
}
// Compile javascript
function scripts() {
return gulp.src(config.src + 'js/*.js')
.pipe($.include().on('error', function(error) {
log.error(error.message);
this.emit('end');
}))
.pipe($.if(config.sourcemaps, $.sourcemaps.init()))
.pipe($.babel())
.pipe($.if(config.sourcemaps, $.sourcemaps.write()))
.pipe(gulp.dest(config.dest + 'js'))
.pipe(browserSync.stream())
.pipe($.if(config.minify, $.uglify().on('error', function(error) {
log.error(error.message);
this.emit('end');
})))
.pipe($.if(config.sourcemaps, $.sourcemaps.write()))
.pipe($.if(config.minify, $.rename({suffix: '.min'})))
.pipe($.if(config.minify, gulp.dest(config.dest + 'js')))
.pipe(browserSync.stream());
}
// Lint javascript
function eslint() {
return gulp.src(config.src + 'js/**/*.js')
.pipe($.eslint({fix: true}))
.pipe($.eslint.format())
.pipe(gulp.dest(config.src + 'js'));
}
// Optimize images
function images() {
return gulp.src(config.src + 'img/**/*.{gif,jpg,png,svg}')
.pipe($.cache($.imagemin()))
.pipe(gulp.dest(config.dest + 'img'))
.pipe(browserSync.stream());
}
// Serve compiled files
function serve(done) {
browserSync.init({
server: true,
notify: false,
snippetOptions: {
rule: {
match: /<\/body>/i
}
}
/*
proxy: 'example.com',
files: config.dest,
serveStatic: [{
route: '', // remote path
dir: config.dest // local path
}],
*/
});
done();
}
// Watch files for changes
function watch(done) {
gulp.watch(config.src + '*.html', html);
gulp.watch(config.src + 'scss/**/*.scss', styles);
gulp.watch(config.src + 'js/**/*.js', scripts);
gulp.watch(config.src + 'img/**/*.{gif,jpg,png,svg}', images);
done();
}
const build = gulp.parallel(html, styles, scripts, images);
exports.build = build;
exports.watch = watch;
exports.lint = gulp.parallel(stylelint, eslint);
exports.default = gulp.series(build, gulp.parallel(serve, watch));