-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
185 lines (175 loc) · 5.16 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
/**
* @author Joren Thijs
* @version V1.0
*
* @summary This gulp file contains all development and build tools for a standard website.
* @description This file was made using Gulp V4.0.2.
* It performs build tasks like compiling and hosting a dev server.
* It also performs publish tasks like bundeling and minifying.
*
* @tutorial type gulp watch to start the dev environment.
* type gulp release to bundle and minify project and export it into the dist folder.
*
* @exports build Compile the sass into css
* @exports watch Starts the dev server and watches for file changes
* @exports release Compiles and minifys project and publishes to the dist folder
* @exports releaseAll Cleares cache + Compiles and minifys project and publishes to the dist folder
* @exports clearCache Cleares cache
* @exports clearDist Deletes contents of the dist folder
*/
// Imports
const gulp = require('gulp');
const sass = require('gulp-sass');
const autoprefixer = require('gulp-autoprefixer');
const sourceMaps = require('gulp-sourcemaps');
const lineEndingCorrector = require('gulp-line-ending-corrector');
const uglify = require('gulp-uglify');
const cleanCSS = require('gulp-clean-css');
const strip_comments = require('gulp-strip-json-comments');
const prettier = require('gulp-prettier');
const useref = require('gulp-useref');
const gulpIf = require('gulp-if');
const imageMin = require('gulp-imagemin');
const cache = require('gulp-cache');
const del = require('del');
const browserSync = require('browser-sync').create();
/**
* Configuration for the gulp-prettier formatter
*/
const prettierOptions = {
tabWidth: 4,
useTabs: false,
singleQuote: true,
semi: true,
trailingComma: 'es5',
overrides: [
{
files: '*.json',
options: {
tabWidth: 2,
},
},
],
};
/**
* Compiles the scss in the '/scss/' folder into regular css into the '/css/' folder
*/
function compileSass() {
return (
gulp
// Locate sass files
.src('./src/scss/*.scss')
// Initialize sourceMaps
.pipe(sourceMaps.init())
// Compile sass into css
.pipe(sass().on('error', sass.logError))
// Add support for older browsers
.pipe(autoprefixer('last 2 versions'))
// Format CSS
.pipe(prettier(prettierOptions))
// Correct Line endings
.pipe(lineEndingCorrector())
// Write sourceMaps
.pipe(sourceMaps.write('./maps'))
// Save the compiled css
.pipe(gulp.dest('./src/css'))
// Stream changes to all browsers
.pipe(browserSync.stream())
);
}
/**
* Publish HTML, CSS, JS to the dist folder and concat and minify the CSS and JS while replacing its's html references
*/
function bundleHTML() {
return (
gulp
// Locate HTML Files
.src('./src/*.html')
// Concat CSS and JS and replace references with bundle references
.pipe(useref())
// Minify the bundled Assets
// Remove comments
.pipe(strip_comments())
// Format CSS and JS
.pipe(prettier(prettierOptions))
// Minify the CSS
.pipe(gulpIf('*.css', cleanCSS()))
// Minify the JS
.pipe(gulpIf('*.js', uglify()))
// Correct Line endings
.pipe(lineEndingCorrector())
// Save the HTML Files
.pipe(gulp.dest('./dist'))
);
}
/**
* Publish fonts to the './dist' folder
*/
function bundleFonts() {
return (
gulp
// Locate fonts
.src('./src/fonts/**/*')
// Save the fonts
.pipe(gulp.dest('./dist/fonts'))
);
}
/**
* Publish and minify images to the './dist' folder
*/
function bundleImages() {
return (
gulp
// Locate Images
.src('./src/images/**/*.+(png|jpg|jpeg|gif|svg)')
// Minify images and cache them for better performance
.pipe(cache(imageMin()))
// Save the Images
.pipe(gulp.dest('./dist/images'))
);
}
/**
* Clear the gulp-cache
*/
function clearCache() {
return cache.clearAll();
}
/**
* Delete the contents of the './dist' folder
*/
function clearDist() {
return del(['./dist/**', '!dist']);
}
/**
* Starts the dev server and watches for file changes
* @listens port 3000
*/
function watch() {
browserSync.init({
server: {
baseDir: './src',
},
open: false,
reloadOnRestart: true,
});
compileSass();
gulp.watch('./src/scss/**/*.scss', compileSass);
gulp.watch('./src/*.html').on('change', browserSync.reload);
gulp.watch('./src/js/**/*.js').on('change', browserSync.reload);
}
// Exports
exports.build = compileSass;
exports.watch = watch;
exports.release = gulp.series(
compileSass,
clearDist,
gulp.parallel(bundleHTML, bundleImages, bundleFonts)
);
exports.releaseAll = gulp.series(
clearCache,
compileSass,
clearDist,
gulp.parallel(bundleHTML, bundleImages, bundleFonts)
);
exports.clearCache = clearCache;
exports.clearDist = clearDist;