-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.mjs
259 lines (225 loc) · 7.27 KB
/
gulpfile.mjs
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
import gulp from 'gulp';
import autoprefixer from 'gulp-autoprefixer';
import babel from 'gulp-babel';
import browserify from 'browserify';
// var browserSync = require("browser-sync").create();
import browserSync from 'browser-sync';
browserSync.create();
import buffer from 'vinyl-buffer';
import cleanCSS from 'gulp-clean-css';
import { deleteAsync, deleteSync } from 'del';
import gulpIf from 'gulp-if';
// import imagemin from 'gulp-imagemin';
import plumber from 'gulp-plumber';
import rename from 'gulp-rename';
import replace from 'gulp-replace';
// var sass = require("gulp-sass")(require("sass"));
import * as dartSass from 'sass';
import gulpSass from 'gulp-sass';
const sass = gulpSass(dartSass);
import source from 'vinyl-source-stream';
import sourcemaps from 'gulp-sourcemaps';
import uglify from 'gulp-uglify';
import watchify from 'watchify';
import { spawn } from 'node:child_process';
const config = {
jekyll: ['pages', 'posts', 'layouts', 'includes', 'data'],
JEKYLL_ENV: 'development',
domains: {
default: 'ecclesmosque.org.uk',
alias: ['ecclesmosque.org.uk', 'ecclesmosque.org'],
},
};
function isProduction() {
return config.JEKYLL_ENV === 'production';
}
gulp.task('clean', function () {
return deleteAsync(['_site', 'assets/styles', 'assets/scripts', 'npm-debug.log']);
});
gulp.task('scripts', function () {
// set up the browserify instance on a task basis
var b = browserify({
cache: {},
packageCache: {},
plugin: !isProduction() ? [watchify] : [],
entries: '_assets/scripts/app.js',
debug: true,
});
function bundle() {
return (
b
.bundle()
.pipe(
plumber({
errorHandler: function (error) {
console.log(error.message);
this.emit('end');
},
})
)
.pipe(source('app.js'))
.pipe(buffer())
.pipe(babel())
.pipe(sourcemaps.init())
// Add transformation tasks to the pipeline here.
.pipe(gulpIf(isProduction(), rename({ suffix: '.min' })))
.pipe(gulpIf(isProduction(), uglify()))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('assets/scripts/'))
.pipe(browserSync.stream({ match: '**/*.js' }))
);
}
b.on('update', bundle);
return bundle();
});
gulp.task(
'jekyll-compile',
gulp.series('scripts', function (next) {
// clone the actual env vars to avoid overrides
var envs = Object.create(process.env);
envs.JEKYLL_ENV = config.JEKYLL_ENV;
return spawn('bundle', ['exec', 'jekyll', 'build', !isProduction() ? '--drafts' : '', isProduction() ? '--profile' : '', '--incremental'], {
stdio: 'inherit',
env: envs,
}).on('exit', function (code) {
next(code === 0 ? null : 'ERROR: Jekyll process exited with code: ' + code);
});
})
);
gulp.task('images', function () {
return (
// We have no images to minimize at the moment so return
gulp.src('.')
// gulp
// .src('_assets/images/**/*')
// .pipe(
// plumber({
// errorHandler: function (error) {
// console.log('Task - images:', error);
// this.emit('end');
// },
// })
// )
// .pipe(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true }))
// .pipe(gulp.dest('assets/images/'))
);
});
gulp.task('styles', function () {
return gulp
.src(['_assets/styles/**/*.scss'])
.pipe(
plumber({
errorHandler: function (error) {
console.log('Task - styles:', error);
this.emit('end');
},
})
)
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(replace(/\.\.\/font\//gim, '/assets/fonts/')) // fix for https://github.com/fontello/fontello/issues/573
.pipe(autoprefixer('last 2 versions'))
.pipe(sourcemaps.init())
.pipe(gulpIf(isProduction(), rename({ suffix: '.min' })))
.pipe(gulpIf(isProduction(), cleanCSS()))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('_site/assets/styles/')) // for BrowserSync
.pipe(gulp.dest('assets/styles/')) // for pruduction
.pipe(browserSync.stream({ match: '**/*.css' }));
});
gulp.task('html-proofer', gulp.series('jekyll-compile', 'images', 'styles', 'scripts'), function (next) {
function doHTMLProof(next) {
var proofer = spawn(
'bundle',
['exec', 'htmlproofer', '--url-swap', '.*' + config.domains.default + '/:/', '--internal-domains', config.domains.alias.join(','), './_site'],
{ stdio: 'inherit' }
);
proofer.on('exit', function (code) {
if (code !== 0) {
console.log('ERROR: htmlproofer process exited with code: ' + code);
this.emit('end');
}
next(null);
});
}
if (!isProduction()) {
next(null);
} else {
return doHTMLProof(next);
}
});
gulp.task('compile', gulp.series('clean', 'scripts', 'images', 'styles', 'jekyll-compile'));
gulp.task('update-assets', function () {
return gulp.src(['assets/**/*']).pipe(gulp.dest('_site/assets/'));
});
gulp.task(
'serve',
gulp.series('compile', function (next) {
browserSync.init({
server: '_site',
});
config.jekyll.forEach(function (contentType) {
gulp.watch('**/_' + contentType + '/**/*.*', gulp.series('jekyll-compile'));
});
gulp.watch('_config.yml', gulp.series('jekyll-compile'));
gulp.watch('_assets/styles/**/*.scss', gulp.series('styles'));
gulp.watch('_assets/scripts/**/*.js', gulp.series('scripts'));
gulp.watch('assets/**/*.*', gulp.series('update-assets'));
gulp.watch('_site/**/*').on('change', browserSync.reload);
return next();
})
);
gulp.task('setup-environment', function (next) {
config.JEKYLL_ENV = 'production';
next();
});
gulp.task('serve-prod', gulp.series('setup-environment', 'compile'), function () {
browserSync.init({
server: '_site',
});
});
gulp.task('update-icons', function (next) {
var fontello = spawn('fontello-cli', ['open', '--config', './_assets/icons/config.json']);
fontello.on('exit', function (code) {
next(code === 0 ? null : 'ERROR: fontello-cli process exited with code: ' + code);
});
});
gulp.task('download-icons', function (next) {
var fontello = spawn('./node_modules/.bin/fontello-cli', [
'install',
'--config',
'./_assets/icons/config.json',
'--font',
'./assets/fonts',
'--css',
'./_assets/styles/icons',
]);
fontello.stdout.on('data', function (data) {
console.log(data.toString());
});
fontello.on('exit', function (code, stdout, stderr) {
console.log({ stdout, stderr });
// rename *.css files to _*.scss
if (code === 0) {
gulp
.src('./_assets/styles/icons/**/*.css')
.pipe(
rename(function (path) {
path.basename = '_' + path.basename;
path.extname = '.scss';
})
)
.pipe(gulp.dest('./_assets/styles/icons/'))
.on('end', function () {
deleteSync(['./_assets/styles/icons/**/*.css']);
next(null);
});
} else {
next('ERROR: fontello-cli process exited with code: ' + code);
}
});
});
gulp.task('check-links', gulp.series('setup-environment', 'html-proofer'));
gulp.task('build', gulp.series('setup-environment', 'compile'));
gulp.task('test', gulp.series('setup-environment', 'compile', 'html-proofer'));
gulp.task('default', gulp.series('serve'));