forked from luangjokaj/gopablo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
305 lines (273 loc) · 9.08 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* -------------------------------------------------------------------------------------------------
SweetStatic - Static site generator
Orginally based on the GoPablo static site generator
Contributors: Luan Gjokaj, Sherif Saleh, Michael Levett
-------------------------------------------------------------------------------------------------- */
const { gulp, series, parallel, dest, src, watch } = require('gulp');
const babel = require('gulp-babel');
const browserSync = require('browser-sync').create();
const concat = require('gulp-concat');
const cssnano = require('cssnano');
const del = require('del');
const fileinclude = require('gulp-file-include');
const gutil = require('gulp-util');
const htmlmin = require('gulp-htmlmin');
const imagemin = require('gulp-imagemin');
const modRewrite = require('connect-modrewrite');
const plumber = require('gulp-plumber');
const postcss = require('gulp-postcss');
const postcssImport = require('postcss-import');
const postCSSMixins = require('postcss-mixins');
const tailwind = require('tailwindcss');
const autoprefixer = require('autoprefixer');
const postcssPresetEnv = require('postcss-preset-env');
const RevAll = require('gulp-rev-all');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const purgecss = require('gulp-purgecss');
/* -------------------------------------------------------------------------------------------------
PostCSS Plugins
-------------------------------------------------------------------------------------------------- */
const pluginsDev = [
postcssImport,
postCSSMixins,
tailwind,
postcssPresetEnv({
stage: 0,
features: {
'nesting-rules': true,
'color-mod-function': true,
'custom-media': true,
},
}),
];
const pluginsProd = [
postcssImport,
postCSSMixins,
tailwind,
autoprefixer,
postcssPresetEnv({
stage: 0,
features: {
'nesting-rules': true,
'color-mod-function': true,
'custom-media': true,
},
}),
require('cssnano')({
preset: [
'default',
{
discardComments: true,
},
],
}),
];
/* -------------------------------------------------------------------------------------------------
Header & Footer JavaScript Boundles
-------------------------------------------------------------------------------------------------- */
const headerJS = [
'./node_modules/aos/dist/aos.js',
];
const footerJS = [
// './node_modules/webfontloader/webfontloader.js',
'./src/assets/js/**',
];
/* -------------------------------------------------------------------------------------------------
Development Tasks
-------------------------------------------------------------------------------------------------- */
function devServer() {
browserSync.init({
logPrefix: '🍬 SweetStatic',
server: {
baseDir: './build',
},
middleware: [modRewrite(['^.([^\\.]+)$ /$1.html [L]'])],
});
watch('./src/assets/css/**/*.css', stylesDev);
watch('./src/assets/js/**', series(footerScriptsDev, Reload));
watch('./src/assets/img/**', series(copyImagesDev, Reload));
watch('./src/assets/fonts/**', series(copyFontsDev, Reload));
watch('./src/**/*.html', series(staticFilesDev, Reload));
}
function Reload(done) {
browserSync.reload();
done();
}
function copyImagesDev() {
return src('./src/assets/img/**').pipe(dest('./build/assets/img'));
}
function copyFontsDev() {
return src('./src/assets/fonts/**').pipe(dest('./build/assets/fonts'));
}
function stylesDev() {
return src(['./src/assets/css/styles.css', './src/assets/css/aos.css'])
.pipe(plumber({ errorHandler: onError }))
.pipe(sourcemaps.init())
.pipe(postcss(pluginsDev))
.pipe(sourcemaps.write('.'))
.pipe(dest('./build/assets/css'))
.pipe(browserSync.stream({ match: '**/*.css' }));
}
function headerScriptsDev() {
return src(headerJS)
.pipe(plumber({ errorHandler: onError }))
.pipe(sourcemaps.init())
.pipe(concat('header-bundle.js'))
.pipe(sourcemaps.write('.'))
.pipe(dest('./build/assets/js'));
}
function footerScriptsDev() {
return src(footerJS)
.pipe(plumber({ errorHandler: onError }))
.pipe(sourcemaps.init())
.pipe(
babel({
presets: ['@babel/preset-env'],
}),
)
.pipe(concat('footer-bundle.js'))
.pipe(sourcemaps.write('.'))
.pipe(dest('./build/assets/js'));
}
function staticFilesDev() {
return src('./src/*.html')
.pipe(plumber({ errorHandler: onError }))
.pipe(
fileinclude({
filters: {
prefix: '@@',
basepath: '@file',
},
}),
)
.pipe(dest('./build'));
}
exports.dev = series(
copyImagesDev,
copyFontsDev,
stylesDev,
headerScriptsDev,
footerScriptsDev,
staticFilesDev,
devServer
);
/* -------------------------------------------------------------------------------------------------
Production Tasks
-------------------------------------------------------------------------------------------------- */
async function cleanProd() {
await del(['./dist']);
}
function copyFontsProd() {
return src('./src/assets/fonts/**').pipe(dest('./dist/assets/fonts'));
}
function headerScriptsProd() {
return src(headerJS)
.pipe(plumber({ errorHandler: onError }))
.pipe(concat('header-bundle.js'))
.pipe(uglify())
.pipe(dest('./dist/assets/js'));
}
function footerScriptsProd() {
return src(footerJS)
.pipe(plumber({ errorHandler: onError }))
.pipe(
babel({
presets: ['@babel/preset-env'],
}),
)
.pipe(concat('footer-bundle.js'))
.pipe(uglify())
.pipe(dest('./dist/assets/js'));
}
function staticFilesProd() {
return src('./src/*.html')
.pipe(plumber({ errorHandler: onError }))
.pipe(
fileinclude({
filters: {
prefix: '@@',
basepath: '@file',
},
}),
)
.pipe(
htmlmin({
collapseWhitespace: true,
ignoreCustomFragments: [/<%[\s\S]*?%>/, /<\?[=|php]?[\s\S]*?\?>/],
}),
)
.pipe(dest('./dist'));
}
function copyImagesProd() {
return src('./src/assets/img/**').pipe(dest('./dist/assets/img'));
}
function processImages() {
return src(['./dist/assets/img/**'])
.pipe(plumber({ errorHandler: onError }))
.pipe(
imagemin([imagemin.svgo({ plugins: [{ removeViewBox: true }] })], {
verbose: true,
}),
)
.pipe(dest('./dist/assets/img'));
}
function stylesProd() {
return src(['./src/assets/css/styles.css', './src/assets/css/aos.css'])
.pipe(plumber({ errorHandler: onError }))
.pipe(postcss(pluginsProd))
.pipe(purgecss({
content: ['./src/**/*.html'], whitelist: ['aos-animate'],
defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || []
}))
.pipe(dest('./dist/assets/css'));
}
function copyEtcProd() {
return src('./src/etc/manifest.json').pipe(dest('./dist'));
}
function bustCaches() {
return src(['./dist/**'])
.pipe(
RevAll.revision({
dontRenameFile: [/^\/favicon.ico$/g, '.html'],
dontUpdateReference: [/^\/favicon.ico$/g, '.html'],
}),
)
.pipe(dest('./dist'))
.on('end', () => {
gutil.beep();
gutil.log(filesGenerated);
gutil.log(thankYou);
});
}
exports.prod = series(
cleanProd,
copyFontsProd,
headerScriptsProd,
footerScriptsProd,
staticFilesProd,
copyImagesProd,
processImages,
stylesProd,
copyEtcProd,
bustCaches,
);
/* -------------------------------------------------------------------------------------------------
Utility Tasks
-------------------------------------------------------------------------------------------------- */
const onError = err => {
gutil.beep();
gutil.log(staticBuild + ' - ' + errorMsg + ' ' + err.toString());
this.emit('end');
};
/* -------------------------------------------------------------------------------------------------
Messages
-------------------------------------------------------------------------------------------------- */
const errorMsg = '\x1b[41mError\x1b[0m';
const filesGenerated = 'Your production file are generated in: \x1b[1m' + __dirname + '/dist/ ✅';
const staticBuild = '\x1b[42m\x1b[1m🍬 SweetStatic\x1b[0m';
const staticBuildUrl = '\x1b[2m - https://www.gopablo.co/\x1b[0m';
const thankYou = 'Thank you for using ' + staticBuild + staticBuildUrl;
/* -------------------------------------------------------------------------------------------------
End of all Tasks
-------------------------------------------------------------------------------------------------- */