This repository has been archived by the owner on Dec 7, 2022. It is now read-only.
generated from mavisland/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
234 lines (213 loc) · 5.98 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Packages
const { src, dest, series, parallel, watch } = require("gulp");
const autoprefixer = require("autoprefixer");
const browserSync = require("browser-sync").create();
const babel = require("gulp-babel");
const del = require("del");
const fs = require("fs");
const cleanCSS = require("gulp-clean-css");
const concat = require("gulp-concat");
const config = require("./config");
const data = require("gulp-data");
const gulpif = require("gulp-if");
const header = require("gulp-header");
const imagemin = require("gulp-imagemin");
const pkg = require("./package.json");
const plumber = require("gulp-plumber");
const postcss = require("gulp-postcss");
const rename = require("gulp-rename");
const sass = require("gulp-sass")(require("node-sass"));
const sourcemaps = require("gulp-sourcemaps");
const spritesmith = require("gulp.spritesmith");
const terser = require("gulp-terser");
const twig = require("gulp-twig");
const wait = require("gulp-wait");
const zip = require("gulp-zip");
// File Banner
const banner = [
"/*!",
" * <%= pkg.name %> - <%= pkg.description %>",
" * @version v<%= pkg.version %>",
" * @link <%= pkg.homepage %>",
" * @license <%= pkg.license %>",
" */",
"",
].join("\n");
// Get Timestamp
const getTimestamp = () => {
let date = new Date();
let year = date.getFullYear().toString();
let month = ("0" + (date.getMonth() + 1)).slice(-2);
let day = ("0" + date.getDate()).slice(-2);
let hour = ("0" + date.getHours().toString()).slice(-2);
let minute = ("0" + date.getMinutes().toString()).slice(-2);
let second = ("0" + date.getSeconds().toString()).slice(-2);
return year + month + day + hour + minute + second;
};
// Archive pre-existing content from output folders
const archiveDist = (cb) => {
src(config.archive.input)
.pipe(zip(pkg.name + "_v" + pkg.version + "-build_" + getTimestamp() + ".zip"))
.pipe(dest(config.archive.output));
return cb();
};
// Remove pre-existing content from output folders
const cleanDist = (cb) => {
del.sync(config.clean);
return cb();
};
// Optimise GIF, JPEG, PNG and SVG images
const buildImages = () => {
return src(config.images.input)
.pipe(plumber())
.pipe(
imagemin({
interlaced: true,
progressive: true,
optimizationLevel: 5,
svgoPlugins: [
{
removeViewBox: true,
},
],
})
)
.pipe(dest(config.images.output));
};
// Concanate & minify JavaScript files
const buildScripts = () => {
return src(config.scripts.input)
.pipe(plumber())
.pipe(gulpif(process.env.NODE_ENV === "development", sourcemaps.init()))
.pipe(
babel({
presets: ["@babel/env"],
})
)
.pipe(concat("app.js"))
.pipe(header(banner, { pkg: pkg }))
.pipe(dest(config.scripts.output))
.pipe(
terser({
keep_fnames: true,
mangle: false,
})
)
.pipe(
rename({
suffix: ".min",
})
)
.pipe(gulpif(process.env.NODE_ENV === "development", sourcemaps.write(".")))
.pipe(dest(config.scripts.output));
};
// Convert a set of images into a spritesheet and CSS variables
const buildSprites = (cb) => {
const spriteData = src(config.sprites.input)
.pipe(plumber())
.pipe(
spritesmith({
imgName: "s.png",
cssName: "_sprites.scss",
cssFormat: "scss",
cssTemplate: "src/sprites/scss.template.handlebars",
imgPath: "../images/s.png",
padding: 3,
imgOpts: {
quality: 100,
},
})
);
spriteData.img.pipe(dest(config.sprites.output));
spriteData.css.pipe(dest(config.sprites.output));
return cb();
};
// Compile, autoprefix & minify SASS files
const buildStyles = () => {
return src(config.styles.input)
.pipe(wait(500))
.pipe(plumber())
.pipe(gulpif(process.env.NODE_ENV === "development", sourcemaps.init()))
.pipe(
sass({
outputStyle: "expanded",
includePaths: ["./node_modules", "./src"],
})
)
.pipe(postcss([autoprefixer()]))
.pipe(header(banner, { pkg: pkg }))
.pipe(dest(config.styles.output))
.pipe(
cleanCSS({
level: {
1: {
specialComments: 0,
},
},
})
)
.pipe(header(banner, { pkg: pkg }))
.pipe(
rename({
suffix: ".min",
})
)
.pipe(gulpif(process.env.NODE_ENV === "development", sourcemaps.write(".")))
.pipe(dest(config.styles.output));
};
// Compile Twig files to HTML
const buildTemplates = () => {
return src(config.templates.input)
.pipe(plumber())
.pipe(
data(() => {
return JSON.parse(fs.readFileSync(config.templates.data));
})
)
.pipe(twig())
.pipe(dest(config.templates.output));
};
const copyFonts = () => {
return src(config.fonts.input).pipe(dest(config.fonts.output));
};
const copyScripts = () => {
return src(config.scripts.copy).pipe(dest(config.scripts.output));
};
// Watch for changes to the source directory
const serveDist = (cb) => {
browserSync.init({
server: {
baseDir: config.server.root,
},
});
cb();
};
// Reload the browser when files change
const reloadBrowser = (cb) => {
browserSync.reload();
cb();
};
// Watch all file changes
const watchSource = () => {
watch(config.images.watch, series(buildImages, reloadBrowser));
watch(config.scripts.copy, series(copyScripts, reloadBrowser));
watch(config.scripts.watch, series(buildScripts, reloadBrowser));
watch(config.styles.watch, series(buildStyles, reloadBrowser));
watch([config.templates.watch, config.templates.data], series(buildTemplates, reloadBrowser));
};
// Archive task
exports.archive = archiveDist;
// Clean task
exports.clean = cleanDist;
// Sprites task
exports.sprites = buildSprites;
// Build task
exports.build = series(
parallel(copyFonts, copyScripts),
parallel(buildScripts, buildStyles, buildTemplates),
buildImages
);
// Watch Task
exports.watch = watchSource;
// Default task
exports.default = series(exports.build, serveDist, watchSource);