-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
109 lines (97 loc) · 2.96 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
const { src, dest, parallel, series, watch } = require('gulp');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const concat = require('gulp-concat');
const postcss = require('gulp-postcss');
// const replace = require('gulp-replace');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const gutil = require('gulp-util');
const babel = require('gulp-babel');
const eslint = require('gulp-eslint');
const htmlmin = require('gulp-htmlmin');
const imagemin = require('gulp-imagemin');
const inlineCss = require('gulp-inline-css');
const browsersync = require('browser-sync').create();
const files = {
scssPath: 'src/*.scss',
jsPath: 'src/*.js',
htmlPath: 'src/*.html',
inlineStylingPath: 'dist/*.html',
imagePath: 'src/assets/*.*'
};
function css() {
return src(files.scssPath)
.pipe(sourcemaps.init())
.pipe(sass().on('error', gutil.log))
.pipe(postcss([autoprefixer(), cssnano()]).on('error', gutil.log))
.pipe(sourcemaps.write('.'))
.pipe(dest('dist/css/'))
.pipe(browsersync.stream());
}
function js() {
return src(files.jsPath)
.pipe(babel({ presets: ['@babel/env'] }).on('error', gutil.log))
.pipe(uglify())
.pipe(concat('script.js'))
.pipe(dest('dist/js'))
.pipe(browsersync.stream());
}
function jsLinting() {
return src(files.jsPath)
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failOnError());
}
function html() {
return src(files.htmlPath)
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(dest('dist/'))
.pipe(browsersync.stream());
}
function makeStylesInlineHTML() {
return src(files.inlineStylingPath)
.pipe(inlineCss())
.pipe(dest('dist/'))
.pipe(browsersync.stream());
}
function images() {
return src(files.imagePath)
.pipe(imagemin())
.pipe(dest('dist/assets'))
.pipe(browsersync.stream());
}
/* task for cache bursting */
// function cacheBursting() {
// let versionNumber = new Date().getTime();
// return src(files.htmlPath)
// .pipe(replace(/vs=\d+/g, `vs=${versionNumber}`))
// .pipe(dest('dist/'));
// }
function browserSync() {
browsersync.init({
server: {
baseDir: './dist'
}
});
}
function watchFiles() {
browserSync();
watch(
[
files.scssPath,
files.jsPath,
files.htmlPath
],
series(jsLinting, parallel(css, js, images, html))
).on('change', function() { browsersync.reload(); });
}
exports.default = series(watchFiles);
exports.css = css;
exports.inlineStyle = makeStylesInlineHTML;
exports.html = html;
exports.javascript = parallel(jsLinting, js);
exports.images = images;
exports.forProd = parallel(css, html, this.javascript, images);
exports.forEmail = parallel(css, html, this.javascript, images, makeStylesInlineHTML);