-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
121 lines (102 loc) · 2.86 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
const gulp = require('gulp');
const htmlmin = require('gulp-htmlmin');
const autoprefixer = require('gulp-autoprefixer');
const csso = require('gulp-csso');
const plumber = require("gulp-plumber");
const uglify = require("gulp-uglify");
const browsersync = require("browser-sync").create();
const fs = require('fs-extra');
let distDir = 'dist';
const webRoot = 'dist';
const AUTOPREFIXER_BROWSERS = [
'ie > 11',
'ie_mob > 11',
'ff >= 64',
'chrome >= 71',
'safari >= 10',
'opera >= 23',
'ios >= 7',
'android >= 4.4',
'bb >= 10'
];
const browserSync = (done) => {
browsersync.init({
server: {
baseDir: distDir
},
port: 3030
});
done();
};
const browserSyncReload = (done) => {
browsersync.reload();
done();
};
const minifyHTML = () => {
return gulp.src(['./src/index.html','src/**/*.html'], ['!lib'])
.pipe(plumber())
.pipe(htmlmin({
collapseWhitespace: true,
conservativeCollapse: true,
minifyJS: true,
minifyCSS: true,
removeComments: true,
ignoreCustomFragments: [/{{[{]?(.*?)[}]?}}/]
}))
.pipe(gulp.dest(distDir));
};
const minifyCSS = () => {
return gulp.src('./src/**/*.css')
.pipe(plumber())
.pipe(autoprefixer({browsers: AUTOPREFIXER_BROWSERS}))
.pipe(csso())
.pipe(gulp.dest(distDir));
};
const minifyJS = () => {
return gulp.src('./src/**/*.js')
.pipe(plumber())
.pipe(uglify())
.pipe(gulp.dest(distDir));
};
const copyImages = () => {
return gulp.src('./src/img/**')
.pipe(plumber())
.pipe(gulp.dest(distDir+'/img'));
};
const copyIcon = () => {
return gulp.src('./src/favicon.*')
.pipe(plumber())
.pipe(gulp.dest(distDir));
};
const copyLib = () => {
return gulp.src('./src/lib/**')
.pipe(plumber())
.pipe(gulp.dest(distDir+'/lib'));
};
const build = (done) => {
return gulp.series(copyLib, copyImages, copyIcon, minifyHTML, minifyCSS, minifyJS)(done);
};
const clean = (done) => {
if (fs.existsSync(distDir))
fs.removeSync(distDir);
fs.mkdirSync(distDir);
return done();
};
const install = (done) => {
distDir = webRoot;
return gulp.series(clean, build)(done);
};
const watchFiles = () => {
gulp.watch(["./src/**/*.html"], gulp.series(copyLib, copyImages, copyIcon, minifyHTML, browserSyncReload));
gulp.watch(["./src/**/*.css"], gulp.series(minifyCSS, browserSyncReload));
gulp.watch(["./src/**/*.js"], gulp.series(minifyJS, browserSyncReload));
};
const watchLocal = gulp.parallel(watchFiles, browserSync);
const defaultTask = (done) => {
return gulp.series(clean, build)(done);
};
exports.clean = clean;
exports.build = build;
exports.install = install;
exports.watch_local = watchLocal;
exports.default = defaultTask;