forked from nhsuk/nhsuk-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
212 lines (188 loc) · 4.42 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
const gulp = require('gulp');
const sass = require('gulp-sass');
const clean = require('gulp-clean');
const rename = require("gulp-rename");
const cleanCSS = require('gulp-clean-css');
const concat = require('gulp-concat');
const del = require('del');
const uglify = require('gulp-uglify');
const zip = require('gulp-zip');
const webpack = require('webpack-stream');
const package = require('./package.json');
/**
* Import gulp tasks used for creating
* our website pages.
*/
require('./tasks/docs.js');
/* Remove all compiled files */
function cleanDist() {
return gulp.src('dist', { allowEmpty: true})
.pipe(clean())
}
/**
* CSS tasks
*/
/* Build the CSS from source */
function compileCSS() {
return gulp.src(['packages/nhsuk.scss'])
.pipe(sass())
.pipe(gulp.dest('dist/'))
.on('error', (err) => {
console.log(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: `-${package.version}.min`
}))
.pipe(gulp.dest('dist/'))
}
/**
* JavaScript tasks
*/
/* Use Webpack to build and minify the NHS.UK components JS. */
function webpackJS() {
return gulp.src('./packages/nhsuk.js')
.pipe(webpack({
mode: 'production',
output: {
filename: 'nhsuk.js',
},
target: 'web',
module: {
rules: [
{
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
}
}))
.pipe(gulp.dest('./dist'));
}
/* Concat the NHS.UK components JS with third party JS (typeahead). */
function concatJS() {
return gulp.src([
'packages/components/header/typeahead.bundle.min.js',
'packages/components/header/nhs.typeahead.js',
'dist/nhsuk.js',
])
.pipe(concat('nhsuk.js'))
.pipe(gulp.dest(['dist/']));
}
/* Delete the Webpack nhsuk.bundle.js after its been concatenated. */
function cleanJS() {
return del('dist/nhsuk.bundle.js');
}
/* Copy jQuery dependency into dist folder for release */
function thirdPartyAssets() {
return gulp.src('./node_modules/jquery/dist/jquery.min.js')
.pipe(rename({
basename: 'jquery-3.3.1.min',
}))
.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: `-${package.version}.min`
}))
.pipe(gulp.dest('dist/'))
}
/**
* Assets tasks
*/
/**
* Copy assets such as icons and images into the distribution
*/
function assets() {
return gulp.src('packages/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/nhsuk.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/nhsuk.min.js'], { base: 'dist' })
.pipe(zip(`nhsuk-frontend-${package.version}.zip`))
.pipe(gulp.dest('dist'))
}
/**
* Development tasks
*/
/* Recompile CSS, JS and docs when there are any changes */
var watch = function() {
gulp.watch(['packages/**/*', 'app/**/*'], gulp.series(['build', 'docs:build']));
}
gulp.task('clean', cleanDist);
gulp.task('style', compileCSS);
gulp.task('build', gulp.series([
compileCSS,
webpackJS,
concatJS,
cleanJS,
]));
gulp.task('bundle', gulp.series([
cleanDist,
'build',
minifyCSS,
minifyJS,
versionJS,
]))
gulp.task('zip', gulp.series([
'bundle',
assets,
thirdPartyAssets,
jsFolder,
cssFolder,
createZip
]));
gulp.task('watch', watch);
/**
* The default task is to build everything, serve the docs and watch for changes
*/
gulp.task('default', gulp.series([
cleanDist,
'build',
gulp.parallel(['docs:serve', watch])
]));