This repository has been archived by the owner on Mar 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
134 lines (122 loc) · 2.9 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
128
129
130
131
132
133
134
'use strict';
// const { src, dest, watch, series, parallel, lastRun } = require('gulp'),
const { gulp, src, dest, series, parallel } = require('gulp'),
htmlmin = require('gulp-htmlmin'),
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
imagemin = require('gulp-imagemin'),
imageDataURI = require('gulp-image-data-uri'),
sass = require('gulp-sass'),
minifyCSS = require('gulp-minify-css'),
purify = require('gulp-purifycss'),
watch = require('gulp-watch'),
browserSync = require('browser-sync').create();
// paths
var paths = {
html: {
src: './*.html',
dist: 'dist/'
},
css: {
src: "src/sass/**/*.scss",
dist: "dist/css/"
},
js: {
src: 'src/js/*.js',
dist: 'dist/js/'
},
img: {
src: 'src/img/*',
dist: 'dist/img/'
}
};
// compile sass to css, clean + minify
function css() {
return src(paths.css.src)
.pipe(sass()
.on('error', sass.logError))
.pipe(purify([paths.js.dist, paths.html.src]))
.pipe(minifyCSS({
keepSpecialComments: 0
}))
.pipe(rename({ extname: '.min.css' }))
.pipe(dest(paths.css.dist))
.pipe(browserSync.stream())
}
exports.css = css;
// concat + minify scripts
function js() {
return src(paths.js.src)
.pipe(concat('scripts.js'))
.pipe(uglify())
.pipe(rename({ extname: '.min.js' }))
.pipe(dest(paths.js.dist))
.pipe(browserSync.stream())
}
exports.js = js;
// compress images
function img() {
return src(paths.img.src)
// No options
// .pipe(imagemin())
// With options
.pipe(imagemin([
imagemin.gifsicle({ interlaced: true }),
imagemin.jpegtran({ progressive: true }),
imagemin.optipng({ optimizationLevel: 5 }),
imagemin.svgo({
plugins: [
{ removeViewBox: true },
{ cleanupIDs: false }
]
})
], {
verbose: true
}))
.pipe(dest(paths.img.dist))
}
exports.img = img;
// convert images to data uri
exports.datauri = function() {
return src(paths.img.src)
.pipe(imageDataURI())
.pipe(concat('data-uri.css'))
.pipe(dest(paths.css.dist));
}
// minify html
function html() {
return src('index.html')
.pipe(htmlmin({
collapseWhitespace: true,
collapseInlineTagWhitespace: true,
removeComments: true
}))
.pipe(rename({ extname: '.html' }))
.pipe(dest('./dist'));
}
exports.html = html
// browsersync + watch
function sync() {
browserSync.init({
server: {
baseDir: "./dist",
index: "/index.html"
}
});
// single task
// watch('paths.css.src', css);
// composed task
watch([paths.html.src, paths.js.src, paths.css.src],
parallel(html, css, js)
)
.on('change', browserSync.reload);
}
exports.sync = sync;
// exports
exports.dev = series(html, css, js, sync);
exports.default = series(
html,
parallel(css, js),
img, sync
);