forked from nhsuk/nhsuk-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
187 lines (164 loc) · 3.84 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
var gulp = require('gulp');
var sass = require('gulp-sass');
var clean = require('gulp-clean');
var rename = require("gulp-rename");
var cleanCSS = require('gulp-clean-css');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var zip = require('gulp-zip');
// Read the package.json so that we can use its metadata such as package.version
var package = require('./package.json');
/**
* Import gulp tasks from other files
*/
require('./tasks/docs.js');
/**
* Remove all compiled files
*/
function cleanDist() {
return gulp.src('dist', { allowEmpty: true})
.pipe(clean())
}
/**
* Build 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 all compiled css and add a .min.css extension
*/
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/'))
}
/**
* Compile javascript into one file
*/
function compileJS() {
return gulp.src([
'packages/components/header/typeahead.bundle.min.js',
'packages/components/header/nhs.typeahead.js',
'packages/components/header/header.js',
'packages/components/feedback-banner/feedback-banner.js',
'packages/components/skip-link/skip-link.js',
'packages/components/details/details.js'
])
.pipe(concat('nhsuk.js'))
.pipe(gulp.dest('dist/'));
}
/**
* Minify all javascript and add a .min.css extension
*/
function minifyJS() {
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/'))
}
/**
* Minify javascript without version number for npm
*/
function packageJS() {
return gulp.src([
'dist/*.js',
'!dist/*.min.js', // don't re-minify minified javascript
])
.pipe(uglify())
.pipe(rename({
suffix: `.min`
}))
.pipe(gulp.dest('packages/'))
}
/**
* Copy assets such as icons and images into the distribution
*/
function assets() {
return gulp.src('packages/assets/**')
.pipe(gulp.dest('dist/assets/'))
}
/**
* Copy 3rd party assets into the dist directory
*/
function thirdPartyAssets() {
return gulp.src('./node_modules/jquery/dist/jquery.min.js')
.pipe(rename({
basename: 'jquery-3.3.1.min',
}))
.pipe(gulp.dest('dist/'));
}
/**
* Copy JS files into their relevant folders
*/
function jsFolder() {
return gulp.src('dist/*.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/**'], { base: 'dist' })
.pipe(zip(`nhsuk-frontend-${package.version}.zip`))
.pipe(gulp.dest('dist'))
}
/**
* Recompile CSS, JS and docs when there are any changes
*/
var watch = function() {
gulp.watch(['packages/**/*', 'docs/**/*'], gulp.series(['build', 'docs:build']));
}
gulp.task('clean', cleanDist);
gulp.task('style', compileCSS);
gulp.task('build', gulp.series([
compileCSS,
compileJS,
]));
gulp.task('bundle', gulp.series([
cleanDist,
'build',
minifyCSS,
minifyJS,
packageJS
]))
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])
]));