-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathGulpfile.js
187 lines (162 loc) · 5.22 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
// -----------------------------------------------------------------------------
// Plugins and variables
// -----------------------------------------------------------------------------
/**
* Gulp plugins
*/
var gulp = require("gulp");
var browserSync = require("browser-sync").create();
var del = require("del");
var sass = require("gulp-sass")(require('sass'));
var sourcemaps = require("gulp-sourcemaps");
var prefix = require("gulp-autoprefixer");
var concat = require("gulp-concat");
var rename = require("gulp-rename");
var uglify = require("gulp-uglify");
var deporder = require("gulp-deporder");
var changed = require("gulp-changed");
var imagemin = require("gulp-imagemin");
var minifyCss = require("gulp-minify-css");
var ghPages = require("gulp-gh-pages");
var babel = require('gulp-babel');
/**
* Predefined filepaths to be used in the tasks
*/
const path = {
build:{
css: "build/assets/css/",
js: "build/assets/js/",
img: "build/assets/img/",
data: "build/assets/data/",
html: "build/"
},
src:{
sass: "src/assets/scss/",
js: "src/assets/js/",
img: "src/assets/img/",
data: "src/assets/data/",
html: "src/"
}
};
// -----------------------------------------------------------------------------
// Utilities
// -----------------------------------------------------------------------------
/**
* Pre-cleaning the build folder
*/
function clean() {
return del([
"build/",
]);
}
/**
* Deploy to gh-pages branch! Run using 'gulp deploy'
*/
function upload() {
return gulp.src("./build/**/*")
.pipe(ghPages());
}
// -----------------------------------------------------------------------------
// Build tasks
// -----------------------------------------------------------------------------
/**
* Compiles SCSS sourcefiles and outputs autoprefixed, minified CSS + sourcemaps
*/
function compileSass() {
return gulp.src(path.src.sass + "/*.scss")
.pipe(sourcemaps.init())
.pipe(sass.sync())
.on("error", sass.logError) //Log SCSS errors in console!
.pipe(prefix(["last 2 versions", "> 1%"], { cascade: true }))
.pipe(rename("main.min.css"))
.pipe(minifyCss())
.pipe(sourcemaps.write("sourcemaps"))
.pipe(gulp.dest(path.build.css))
//Update browser sync!
.pipe(browserSync.stream());
}
/**
* Combines and minifies all source JavaScript files, including sourcemaps. Dependencies and ordering is done with the deporder plugin
*/
function js() {
return gulp.src(path.src.js + "**/*.js")
.pipe(deporder())
.pipe(sourcemaps.init())
.pipe(concat("concat.js"))
.pipe(rename("all.min.js"))
.pipe(babel({ presets: ['@babel/env'] }))
.pipe(uglify())
.pipe(sourcemaps.write("sourcemaps"))
.pipe(gulp.dest(path.build.js))
.pipe(browserSync.stream());
}
/**
* Optimizes images for web and outputs them to build folder.
*/
function img() {
return gulp.src(path.src.img + "*.*")
.pipe(changed(path.build.img)) // Ignore unchanged files
.pipe(imagemin({optimizationLevel: 5}))
.pipe(gulp.dest(path.build.img))
.pipe(browserSync.stream());
}
/**
* Outputs data files to build folder
*/
function data() {
return gulp.src(path.src.data + "*.*")
.pipe(changed(path.build.data)) // Ignore unchanged files
.pipe(gulp.dest(path.build.data))
.pipe(browserSync.stream());
}
/**
* Outputs html files to build folder
*/
function html() {
return gulp.src([path.src.html + "*.html", path.src.html + "CNAME"])
.pipe(changed(path.build.html)) // Ignore unchanged files
.pipe(gulp.dest(path.build.html))
.pipe(browserSync.stream());
}
// -----------------------------------------------------------------------------
// Watch and serve tasks
// -----------------------------------------------------------------------------
/**
* Start BrowserSync server and watch files for changes!
*/
function serve(cb) {
//Start browsersync server!
browserSync.init({
server: "build/",
port: 8000,
https: true,
});
//Watch folders!
gulp.watch(path.src.sass + "**/*.scss", gulp.series("sass"));
gulp.watch(path.src.html + "*.html", gulp.series("html"));
gulp.watch(path.src.js + "**/*.js", gulp.series("js"));
gulp.watch(path.src.img + "**/*", gulp.series("img"));
gulp.watch(path.src.data + "**/*.(csv|json)", gulp.series("data"));
cb();
}
/**
* Outputs all files.
*/
const build = gulp.parallel(compileSass, js, img, html, data);
// export tasks
exports.clean = clean;
exports.sass = compileSass;
exports.js = js;
exports.img = img;
exports.data = data;
exports.html = html;
exports.upload = upload;
exports.build = gulp.series(clean, build);
/**
* Run tasks in specified order! (1. clean, 2. build, 3. serve and watch)
*/
exports.default = gulp.series(clean, build, serve);
/**
* Alternative: Build, then deploy to gh-pages!
*/
exports.deploy = gulp.series(clean, build, upload);