-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
215 lines (173 loc) · 5.19 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
const config = require('./gulp.config.js');
const {gulp, src, dest, watch, series, parallel} = require('gulp');
const browserSync = require('browser-sync');
const changed = require('gulp-changed');
const csso = require('gulp-csso');
const del = require('del');
const imagemin = require('gulp-imagemin');
const nunjucks = require('gulp-nunjucks');
const pngquant = require('imagemin-pngquant');
const postcss = require('gulp-postcss');
const rename = require('gulp-rename');
const rev = require('gulp-rev');
const sass = require('gulp-sass')(require('sass'));
const size = require('gulp-size');
const svgmin = require('gulp-svgmin');
const svgSymbols = require('gulp-svg-symbols');
const rollup = require("rollup");
const commonjs = require('@rollup/plugin-commonjs');
const multiEntry = require('@rollup/plugin-multi-entry');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const { terser } = require("rollup-plugin-terser");
// Styles task
const css = function (done) {
// Make sure this feature is activated before running
if (!config.tasks.css) return done();
return src(config.css.src + "**/*.scss")
.pipe(sass())
.pipe(dest(config.css.src))
.pipe(postcss())
.pipe(csso())
.pipe(size({ title: 'CSS', gzip: true, showFiles: true }))
.pipe(dest(config.css.dist))
.pipe(browserSync.stream());
}
const cssVendor = function (done) {
// Make sure this feature is activated before running
if (!config.tasks.cssVendor) return done();
return src(config.cssVendor.src)
.pipe(rename({
prefix: '_',
extname: '.scss'
}))
.pipe(dest(config.cssVendor.dist));
}
// Scripts task
const js = function (done) {
// Make sure this feature is activated before running
if (!config.tasks.js) return done();
return rollup.rollup({
input: config.js.src,
plugins: [
multiEntry(),
commonjs(),
nodeResolve(),
terser(),
]
}).then(bundle => {
return bundle.write({
file: config.js.dist + config.js.name,
format: 'iife'
});
});
}
// Images task
const img = function (done) {
// Make sure this feature is activated before running
if (!config.tasks.img) return done();
return src(config.img.src)
.pipe(changed(config.img.src))
.pipe(imagemin([
imagemin.gifsicle({ interlaced: true }),
imagemin.mozjpeg({ quality: 50, progressive: true }),
pngquant({ quality: [0.5, 0.5] }),
imagemin.svgo()
]))
.pipe(size({ title: 'Images', gzip: true }))
.pipe(dest(config.img.dist));
}
// SVG Sprite task
const svgSprite = function (done) {
// Make sure this feature is activated before running
if (!config.tasks.svgSprite) return done();
return src(config.svgSprite.src)
.pipe(svgmin({ plugins: [{ removeViewBox: false }] }))
.pipe(svgSymbols({ templates: ['default-svg'] }))
.pipe(rename(config.svgSprite.name))
.pipe(size({ title: 'SVG Sprite', gzip: true }))
.pipe(dest(config.svgSprite.dist));
}
// Fonts task
const fonts = function (done) {
// Make sure this feature is activated before running
if (!config.tasks.fonts) return done();
return src(config.fonts.src)
.pipe(changed(config.fonts.src))
.pipe(dest(config.fonts.dist));
}
// Templates task
const templates = function (done) {
// Make sure this feature is activated before running
if (!config.tasks.templates) return done();
return src(config.templates.src)
.pipe(nunjucks.compile())
.pipe(dest(config.templates.dist));
};
// Version task
const version = function (done) {
// Make sure this feature is activated before running
if (!config.tasks.version) return done();
return src(config.version.src, { base: config.version.base })
.pipe(rev())
.pipe(dest(config.version.dist))
.pipe(rev.manifest())
.pipe(dest(config.version.dist));
// Signal completion
done()
}
// Clean task
const clean = function (done) {
// Make sure this feature is activated before running
if (!config.tasks.clean) return done();
del.sync(
config.clean
);
// Signal completion
return done();
}
// Server task
const startServer = function (done) {
// Make sure this feature is activated before running
if (!config.tasks.reload) return done();
// Initialize BrowserSync
browserSync.init({
server: config.url
});
// Signal completion
done();
};
// Reload the browser when files change
const reloadBrowser = function (done) {
// Make sure this feature is activated before running
if (!config.tasks.reload) return done();
browserSync.reload();
// Signal completion
done();
};
// Watch for changes
const watchSource = function (done) {
watch(config.css.src + "**/*.scss", series(css));
watch(config.tailwind, series(css));
watch(config.js.src, series(js, reloadBrowser));
watch(config.img.src, series(img));
watch(config.svgSprite.src, series(svgSprite));
watch(config.fonts.src, series(fonts));
watch(config.templates.watch, series(css, templates, reloadBrowser));
// Signal completion
done();
};
// Default task
exports.default = series(
clean,
cssVendor,
parallel(css, js, img, svgSprite, fonts, templates),
startServer,
watchSource,
);
// Build task
exports.build = series(
clean,
cssVendor,
parallel(css, js, img, svgSprite, fonts, templates),
version
);