forked from thecodercoder/frontend-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
150 lines (127 loc) · 3.34 KB
/
gulpfile.babel.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict';
// Initialize modules
// Import specific gulp API functions lets us write them below as series()
// instead of gulp.series()
import { src, dest, watch, series, parallel } from 'gulp';
// Upgrade gulp-sass to v5
// https://github.com/dlmanning/gulp-sass/tree/master#migrating-to-version-5
const sass = require('gulp-sass')(require('sass'));
// Import all the Gulp-related packages we want to use
import postcss from 'gulp-postcss';
import rename from 'gulp-rename';
import autoprefixer from 'autoprefixer';
import cssnano from 'cssnano';
import imagemin from 'gulp-imagemin';
import babel from 'gulp-babel';
import concat from 'gulp-concat';
import uglify from 'gulp-uglify';
import cachebust from 'gulp-cache-bust';
import browserSync from 'browser-sync';
const server = browserSync.create();
// setup the BrowserSync server
function reload(done) {
server.reload();
done();
}
function serve(done) {
server.init({
server: {
baseDir: './dist',
},
});
done();
}
// File paths to watch
const files = {
htmlPath: './*.html',
scssPath: 'src/assets/scss/**/*.scss',
jsPath: 'src/assets/js/**/*.js',
imagePath: 'src/assets/images/**/*.*',
};
// Sass task
// compiles the style.scss file into style.css
function scssTask() {
return (
src(files.scssPath)
.pipe(sass({ outputStyle: 'expanded' }))
// PostCSS plugins
.pipe(postcss([autoprefixer()]))
// write pre-minifies styles
.pipe(dest('dist/assets/css'))
// PostCSS plugins
.pipe(postcss([cssnano()]))
// rename files
.pipe(
rename({
suffix: '.min',
})
)
// put final CSS file in dist folder
.pipe(
dest('dist/assets/css', {
sourcemaps: '.',
})
)
);
}
// JS task
// concatenates and uglifies JS files
function jsTask() {
return (
src(files.jsPath)
//,'!' + 'includes/js/jquery.min.js', // to exclude any specific files
// transpile files
.pipe(babel())
// concat files
.pipe(concat('app.min.js'))
// write pre-minifies files
.pipe(dest('dist/assets/js'))
// rename files
.pipe(
rename({
suffix: '.min',
})
)
// minify scripts
.pipe(uglify())
// put final JS file in /dist folder
.pipe(
dest('dist/assets/js', {
sourcemaps: '.',
})
)
);
}
// image task
// compress PNG, JPEG, GIF and SVG images
function imageTask() {
return src(files.imagePath).pipe(imagemin()).pipe(dest('dist/assets/images'));
}
// cache busting task
// add a timestamp string to CSS/JS style/script
function cacheBustTask() {
return src(files.htmlPath)
.pipe(
cachebust({
type: 'timestamp',
})
)
.pipe(dest('dist/'));
}
// watch task: watch SCSS, JS, image and HTML files for changes
// If any change, run scss, js and image tasks simultaneously
// then reload via browsersync
function watchTask() {
watch(
[files.htmlPath, files.scssPath, files.jsPath, files.imagePath],
series(parallel(scssTask, jsTask, imageTask, cacheBustTask), reload)
);
}
// Export the default Gulp task so it can be run
// Runs the scss, js, image and cache busting tasks simultaneously
// start local server and watch tasks
export default series(
parallel(scssTask, jsTask, imageTask, cacheBustTask),
serve,
watchTask
);