-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
93 lines (77 loc) · 2.25 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
// Initialize modules needed
const { src, dest, watch, series, parallel } = 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 imagemin = require('gulp-imagemin');
//File path variables
//Super helpful, so you don't have to keep writing them over and over. Also, protects from error.
const files = {
scssPath: 'library/sass/**/*.scss',
jsPath: 'library/js/**/*.js',
imgPath: 'library/img/*'
}
//Sass task
function scssTask(){
return src(files.scssPath)
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(postcss([ autoprefixer(), cssnano() ]))
.pipe(sourcemaps.write('.'))
.pipe(dest('dist/css')
);
}
//JS task
function jsTask() {
return src(files.jsPath)
.pipe(concat('production.js'))
.pipe(uglify())
.pipe(dest('dist/js')
);
}
//Minify Images
function imgSmush() {
return src(files.imgPath)
.pipe(imagemin([
imagemin.gifsicle({interlaced: true}),
imagemin.mozjpeg({quality: 75, progressive: true}),
imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo({
plugins: [
{removeViewBox: true},
{cleanupIDs: false}
]
})
]))
.pipe(dest('dist/img'));
}
// Cachebusting task (This allows your CSS and JS files to upgrade their versions each time to avoid browser loading cached versions)
//Generates a string based on current time
const cbString = new Date().getTime();
//Runs a search + Replace for query string at end of .css and .js files
function cacheBustTask(){
return src(['index.php'])
.pipe(replace(/cb=\d+/g, 'cb=' + cbString))
.pipe(dest('.'))
}
//Watch
function watchTask(){
watch([files.scssPath, files.jsPath],
{interval: 1000, usePolling: true}, //Makes docker work
series(
parallel(scssTask, jsTask),
cacheBustTask
)
);
}
// Default task when you start gulp in terminal:
exports.default = series(
parallel(scssTask, jsTask, imgSmush),
cacheBustTask,
watchTask
);