-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
65 lines (59 loc) · 1.82 KB
/
gulpfile.js
File metadata and controls
65 lines (59 loc) · 1.82 KB
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
const { src, dest, parallel, watch } = require('gulp');
const cleanCSS = require('gulp-clean-css');
const uglify = require('gulp-uglify-es').default;
const rename = require('gulp-rename');
const gulpBrotli = require('gulp-brotli');
const zlib = require('zlib');
const paths = {
css: {
src: 'assets/css/*.css',
dest: 'dist/css/',
},
js: {
src: 'assets/js/*.js',
dest: 'dist/js/',
},
};
/*
* Task to Minify and Compress CSS
* This task takes a file suppose styles.css from assets/css
* Minifies it and compresses the file using Brotli Algorithm
* And saves the file as styles.min.css.br in dist/css directory
*/
function minifyAndCompressCSS() {
return src(paths.css.src)
.pipe(cleanCSS())
.pipe(rename({ extname: '.min.css' }))
.pipe(gulpBrotli(brotliOptions()))
.pipe(rename({ extname: '.br' }))
.pipe(dest(paths.css.dest));
}
/*
* Task to Minify and Compress JavaScript
* This task takes a file suppose main.js from assets/js
* Minifies it and compresses the file using Brotli Algorithm
* And saves the file as main.min.js.br in dist/js directory
*/
function minifyAndCompressJS() {
return src(paths.js.src)
.pipe(uglify())
.pipe(rename({ extname: '.min.js' }))
.pipe(gulpBrotli(brotliOptions()))
.pipe(rename({ extname: '.br' }))
.pipe(dest(paths.js.dest));
}
// task to watch files for change in dev environment
function watchFiles() {
watch(paths.css.src, { ignoreInitial: false }, minifyAndCompressCSS);
watch(paths.js.src, { ignoreInitial: false }, minifyAndCompressJS);
}
function brotliOptions() {
return {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
},
};
}
// export tasks so they are available in command line
exports.watch = watchFiles;
exports.build = parallel(minifyAndCompressCSS, minifyAndCompressJS);