This repository has been archived by the owner on Aug 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
gulpfile.js
191 lines (176 loc) · 5.51 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// https://levelup.gitconnected.com/how-to-setup-your-workflow-using-gulp-v4-0-0-5450e3d7c512
const gulp = require('gulp')
const sass = require('gulp-sass')
const postcss = require('gulp-postcss')
const autoprefixer = require('autoprefixer')
const cssnano = require('cssnano')
const concat = require('gulp-concat')
const uglify = require('gulp-uglify')
const imagemin = require('gulp-imagemin')
const replace = require('gulp-replace')
const sourcemaps = require('gulp-sourcemaps')
const browserSync = require('browser-sync').create()
const paths = {
styles: {
// By using styles/**/*.sass we're telling gulp to check all folders for any sass file
src: 'src-static/scss/**/*.scss',
// Compiled files will end up in whichever folder it's found in (partials are not compiled)
dest: 'public/dist/css'
},
js: {
src: 'src-static/js/**/*.js',
dest: 'public/dist/js'
},
images: {
src: 'src-static/images/*',
dest: 'public/dist/images'
},
fonts: {
src: 'src-static/fonts/*',
dest: 'public/dist/css/fonts'
},
libs: {
src: 'src-static/lib/**/*',
dest: 'public/dist/lib'
}
}
/* Optimize our scss files */
function style() {
return (
gulp
.src(paths.styles.src)
// Initialize sourcemaps before compilation starts
.pipe(sourcemaps.init())
.pipe(sass())
.on('error', sass.logError)
// Use postcss with autoprefixer and compress the compiled file using cssnano
.pipe(postcss([autoprefixer(), cssnano()]))
// Now add/write the sourcemaps
.pipe(sourcemaps.write())
.pipe(gulp.dest(paths.styles.dest))
// Add browsersync stream pipe after compilation
.pipe(browserSync.stream())
)
}
/* Optimize our images */
function image() {
return gulp
.src(paths.images.src)
.pipe(
imagemin([
imagemin.svgo({
plugins: [
{ removeXMLProcInst: false },
{ removeMetadata: false },
{ removeXMLNS: false },
{ removeEditorsNSData: false },
{ removeEmptyAttrs: false },
{ removeHiddenElems: false },
{ removeEmptyText: false },
{ removeEmptyContainers: false },
{ removeViewBox: false },
{ cleanupEnableBackground: false },
{ minifyStyles: false },
{ convertStyleToAttrs: false },
{ convertPathData: false },
{ convertTransform: false },
{ rremoveUnknownsAndDefaults: false },
{ removeNonInheritableGroupAttrs: false },
{ removeUselessStrokeAndFill: false },
{ removeUnusedNS: false },
{ prefixIds: false },
{ cleanupNumericValues: false },
{ cleanupListOfValues: false }
]
})
])
)
.pipe(gulp.dest(paths.images.dest))
.pipe(browserSync.stream())
}
/* Copy out fonts over to dist */
function font() {
return gulp.src(paths.fonts.src).pipe(gulp.dest(paths.fonts.dest))
}
/* Copy out libs over to dist */
function lib() {
return gulp.src(paths.libs.src).pipe(gulp.dest(paths.libs.dest))
}
/* Optimize and concat our scripts */
function script() {
return gulp
.src([
'src-static/lib/bootstrap/js/jquery-3.4.1.min.js',
'src-static/lib/bootstrap/js/popper.min.js',
'src-static/lib/bootstrap/js/bootstrap.4.3.min.js',
'src-static/lib/slick/slick.min.js',
'src-static/lib/matchHeight/jquery.matchHeight.js',
'src-static/lib/dateformat/jquery-dateformat.min.js',
'src-static/lib/raven.min.js',
'src-static/js/main.js'
])
.pipe(concat('main.js'))
.pipe(uglify())
.pipe(gulp.dest(paths.js.dest))
.pipe(browserSync.stream())
}
/* Cache busting by adding timestamp query string to our optimized files */
const timeStamp = new Date().getTime()
function cacheBust() {
return gulp
.src(['public/**/*.html'])
.pipe(replace(/CACHEBUSTER_TIMESTAMP/g, timeStamp))
.pipe(gulp.dest('./public/.'))
}
// A simple task to reload the page
function reload() {
browserSync.reload()
}
// Add browsersync initialization at the start of the watch task
function watch() {
browserSync.init({
// You can tell browserSync to use this directory and serve it as a mini-server
server: {
baseDir: './public'
}
// If you are already serving your website locally using something like apache
// You can use the proxy setting to proxy that instead
// proxy: "yourlocal.dev"
})
gulp.watch(paths.styles.src, style)
gulp.watch(paths.js.src, script)
gulp.watch(paths.images.src, image)
gulp.watch(paths.fonts.src, font)
gulp.watch(paths.libs.src, lib)
// We should tell gulp which files to watch to trigger the reload
// This can be html or whatever you're using to develop your website
// Note -- you can obviously add the path to the Paths object
gulp.watch('public/*.html', reload)
}
// Don't forget to expose the task!
exports.watch = watch
// Expose the task by exporting it
// This allows you to run it from the commandline using
// $ gulp style
exports.style = style
exports.image = image
exports.font = font
exports.lib = lib
exports.script = script
exports.cacheBust = cacheBust
/*
* Specify if tasks run in series or parallel using `gulp.series` and `gulp.parallel`
*/
const build = gulp.parallel(
...[style, image, font, lib, script].concat(
process.env.TRAVIS ? cacheBust : []
)
)
/*
* You can still use `gulp.task` to expose tasks
*/
gulp.task('build', build)
/*
* Define default task that can be called by just running `gulp` from cli
*/
gulp.task('default', build)