-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
230 lines (204 loc) · 4.83 KB
/
gulpfile.js
File metadata and controls
230 lines (204 loc) · 4.83 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
const gulp = require('gulp');
const sass = require('gulp-sass')(require('sass'));
const clean = require('gulp-clean');
const rename = require('gulp-rename');
const cleanCSS = require('gulp-clean-css');
const uglify = require('gulp-uglify');
const zip = require('gulp-zip');
const webpack = require('webpack-stream');
const { execFileSync } = require('node:child_process');
const { version } = require('./package.json');
/* Remove all compiled files */
function cleanDist() {
return gulp.src('dist', { allowEmpty: true }).pipe(clean());
}
/**
* CSS tasks
*/
/* Build the CSS from source */
function compileCSS() {
const outputNames = {
ofh: 'ofh-design-system-toolkit',
'ofh-participant': 'ofh-design-system-toolkit-participant',
'ofh-research': 'ofh-design-system-toolkit-research',
};
return gulp
.src(['ofh.scss', 'ofh-participant.scss', 'ofh-research.scss'])
.pipe(sass.sync({
api: 'modern-compiler',
quietDeps: true,
}))
.pipe(
rename((path) => {
path.basename = outputNames[path.basename] || path.basename;
}),
)
.pipe(gulp.dest('dist/'))
.on('error', (err) => {
console.error(err);
process.exit(1);
});
}
/* Minify CSS and add a min.css suffix */
function minifyCSS() {
return gulp
.src([
'dist/*.css',
'!dist/*.min.css', // don't re-minify minified css
])
.pipe(cleanCSS())
.pipe(
rename({
suffix: `-${version}.min`,
}),
)
.pipe(gulp.dest('dist/'));
}
/**
* JavaScript tasks
*/
/* Use Webpack to build and minify the Our Future Health components JS. */
function webpackJS() {
return gulp
.src('./ofh.js')
.pipe(
webpack({
mode: 'production',
stats: {
preset: 'errors-warnings',
colors: true,
timings: true,
},
module: {
rules: [
{
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
},
],
},
output: {
filename: 'ofh-design-system-toolkit.js',
},
target: 'web',
}),
)
.pipe(gulp.dest('./dist'));
}
/* Minify the JS file for release */
function minifyJS() {
return gulp
.src([
'dist/*.js',
'!dist/*.min.js', // don't re-minify minified javascript
])
.pipe(uglify())
.pipe(
rename({
suffix: '.min',
}),
)
.pipe(gulp.dest('dist/'));
}
/* Version the JS file for release */
function versionJS() {
return gulp
.src([
'dist/*.js',
'!dist/*.min.js', // don't re-minify minified javascript
])
.pipe(uglify())
.pipe(
rename({
suffix: `-${version}.min`,
}),
)
.pipe(gulp.dest('dist/'));
}
/**
* Assets tasks
*/
function buildIconSprite(done) {
execFileSync('node', ['scripts/build-icon-sprite.js'], {
stdio: 'inherit',
});
done();
}
/**
* Copy assets such as icons and images into the distribution
*/
function assets() {
return gulp.src('assets/**').pipe(gulp.dest('dist/assets/'));
}
/**
* Release tasks
*/
/* Copy JS files into their relevant folders */
function jsFolder() {
return gulp
.src('dist/*.min.js', '!dist/js/ofh-design-system-toolkit.min.js')
.pipe(clean())
.pipe(gulp.dest('dist/js/'));
}
/* Copy CSS files into their relevant folders */
function cssFolder() {
return gulp.src('dist/*.min.css').pipe(clean()).pipe(gulp.dest('dist/css/'));
}
function createZip() {
return gulp
.src(
[
'dist/css/*.min.css',
'dist/js/*.min.js',
'dist/assets/**',
'!dist/js/ofh-design-system-toolkit.min.js',
],
{ base: 'dist' },
)
.pipe(zip(`ofh-design-system-toolkit-${version}.zip`))
.pipe(gulp.dest('dist'));
}
/**
* Development tasks
*/
/*
* Rebuild the toolkit when source files change.
*
* Notes:
* - `dist/**` is excluded so generated bundles do not trigger rebuilds.
* - `assets/icons/icon-sprite.svg` is also excluded because it is generated by
* `buildIconSprite`. Watching it causes the watcher to react to its own
* output and loop.
* - Source icon SVGs in `assets/icons/*.svg` are still watched by the general
* package file glob,
* so editing an icon continues to regenerate the sprite automatically.
*/
function watch() {
gulp.watch(
['./**/*', '!dist/**', '!assets/icons/icon-sprite.svg'],
gulp.series(['build']),
);
}
gulp.task('clean', cleanDist);
gulp.task('style', compileCSS);
gulp.task('build', gulp.series([buildIconSprite, compileCSS, webpackJS]));
gulp.task(
'bundle',
gulp.series([
cleanDist,
'build',
minifyCSS,
minifyJS,
versionJS,
assets,
]),
);
gulp.task(
'zip',
gulp.series(['bundle', assets, jsFolder, cssFolder, createZip]),
);
gulp.task('watch', watch);