-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgulpfile.mjs
More file actions
executable file
·267 lines (248 loc) · 6.59 KB
/
gulpfile.mjs
File metadata and controls
executable file
·267 lines (248 loc) · 6.59 KB
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
/**
* Customize the project in the config.js file
*/
import config from './gulp-config.js';
/**
* Load gulp plugins and passing them semantic names.
*/
import gulp from 'gulp'; // Gulp of-course
// CSS related plugins.
import * as dartSass from 'sass'; // Gulp pluign for Sass compilation
import gulpSass from 'gulp-sass';
var sass = gulpSass(dartSass);
import csso from 'gulp-csso'; // CSS optimixations and minimizing
import autoprefixer from 'autoprefixer'; // Autoprefixing magic
import postcss from 'gulp-postcss'; // Run PostCSS tasks
import sortMediaQueries from 'postcss-sort-media-queries'; // Merge similiar media queries
// JS related plugins.
import { createGulpEsbuild } from 'gulp-esbuild';
var esbuild = createGulpEsbuild({
pipe: true,
});
// Utility related plugins.
import fs from 'fs'; // The filesystem for manipulating files
import { exec } from 'child_process';
import plumber from 'gulp-plumber'; // Prevent pipe breaking caused by errors from gulp plugins
import sourcemaps from 'gulp-sourcemaps'; // For generating sourcemaps
import rename from 'gulp-rename'; // Renames files E.g. style.css -> style.min.css
import lineec from 'gulp-line-ending-corrector'; // Consistent Line Endings for non UNIX systems
import log from 'fancy-log'; // Fancy logging
// import debug from 'gulp-debug'; // For debugging Gulp filenames and paths
import { deleteSync } from 'del'; // Handles deleting files and directories
import prettier from 'gulp-prettier'; // For linting and fixing files
/**
* Helper to log success messages
*
* @param {string} message The success message to display
*
* @return {string} The pretty formatted success message
*/
function logSuccess(message) {
log('✅✅✅✅ ' + message);
}
/**
* Helper to log serror messages
*
* @param {string} message The error message to display
*
* @return {string} The pretty formatted error message
*/
function logError(message) {
log('❌❌❌❌ ' + message);
// Make an audible beep
if (process.platform === 'darwin') {
// Play a beep sound on MacOS
exec("osascript -e 'beep'");
} else {
process.stdout.write('\x07');
}
}
/**
* Strip .src .js from a JavaScript file's basename
*
* @param {string} val The JS basename to be cleaned
*
* @return {string} The modified basename
*/
function cleanJSBasename(val) {
var val = val.replace('.src', '');
val = val.replace('.js', '');
return val;
}
/**
* Delete previously compiled files
*/
gulp.task('clean', function () {
deleteSync(config.filesToClean);
return gulp.src('.').on('end', () => logSuccess('🧹'));
});
/**
* Compile Sass and generate CSS files
*/
gulp.task('styles', function () {
let hadError = false;
return (
gulp
.src(config.styleSRC)
.pipe(
plumber({
errorHandler: function (err) {
hadError = true;
logError(err.messageFormatted);
this.emit('end'); // End stream if error is found
},
})
)
// .pipe(sourcemaps.init())
.pipe(
sass({
outputStyle: 'expanded',
precision: config.precision,
loadPaths: config.loadPaths,
})
)
.pipe(
postcss([
autoprefixer({
overrideBrowserslist: config.BROWSERS_LIST,
}),
sortMediaQueries(),
])
)
.pipe(rename({ suffix: '.min' }))
.pipe(csso())
.pipe(lineec())
// .pipe(sourcemaps.write('.'))
.pipe(gulp.dest(config.styleDestination))
.on('finish', () => {
if (!hadError) {
logSuccess('🎨');
}
})
);
});
/**
* See if a compiled CSS file contains certain patterns we know are errors
*/
gulp.task(
'styles:test',
gulp.series('clean', 'styles', function runStyleTests(done) {
const css = fs.readFileSync('assets/css/rh.min.css', 'utf8');
const checks = [
{
pattern: 'svg + xml',
message: 'There should be no spaces around + in "svg+xml"',
},
{
pattern: 'rem+3);',
message: 'There should be spaces around + in clamp() functions',
},
];
checks.forEach(({ pattern, message }) => {
if (css.includes(pattern)) {
throw new Error(logError(message));
}
});
return gulp.src('.').on('end', () => logSuccess('🧪'));
})
);
/**
* Compile JavaScript files using esbuild
*/
gulp.task('scripts', function () {
let hadError = false;
let pathMapping = {};
return gulp
.src(config.scriptSRC)
.pipe(
plumber({
errorHandler: function (err) {
hadError = true;
logError(err.message);
this.emit('end'); // End stream if error is found
},
})
)
.pipe(
rename(function (path) {
let key = cleanJSBasename(path.basename);
pathMapping[key] = path.dirname;
return path;
})
)
.pipe(
esbuild({
bundle: true,
minify: true,
sourcemap: true,
target: ['es2015'],
loader: { '.js': 'js' },
})
)
.pipe(lineec()) // Consistent Line Endings for non UNIX systems
.pipe(
// Maybe fix paths that the esbuild step clobbers?
rename(function (path) {
let key = cleanJSBasename(path.basename);
if (pathMapping[key]) {
path.dirname = pathMapping[key];
}
if (path.extname === '.js') {
path.basename = path.basename.replace('.src', '');
}
return path;
})
)
.pipe(gulp.dest(config.scriptDest))
.on('finish', () => {
if (!hadError) {
logSuccess('🛠️');
}
});
});
/**
* Copy files from a directery like node_modules to another location
*/
gulp.task('setup', function (done) {
config.filesToMove.forEach(function (file) {
if (!file.rename) {
file.rename = {};
}
gulp.src(file.src).pipe(rename(file.rename)).pipe(gulp.dest(file.dest));
});
done();
});
/**
* Run Prettier in "check" mode against all source files.
*
* This task verifies that JavaScript and stylesheet files conform
* to Prettier’s formatting rules without making any changes.
* If formatting issues are found, the task will fail.
*/
gulp.task('prettier', function () {
return gulp
.src([...config.scriptSRC, ...config.styleSRC])
.pipe(prettier.check());
});
/**
* Run Prettier in "write" mode to automatically fix formatting issues.
*
* This task reformats JavaScript and stylesheet files according
* to Prettier’s rules and writes the corrected files back to disk.
*/
gulp.task('prettier:fix', function () {
return gulp
.src([...config.scriptSRC, ...config.styleSRC])
.pipe(prettier())
.pipe(gulp.dest('.'));
});
/**
* Watches for file changes and runs specific tasks
*/
gulp.task(
'default',
gulp.parallel('clean', 'styles', 'scripts', function watchFiles() {
gulp.watch(config.styleWatchFiles, gulp.parallel('styles')); // Reload on SCSS file changes.
gulp.watch(config.scriptWatchFiles, gulp.series('scripts')); // Reload on scripts file changes.
})
);