-
Notifications
You must be signed in to change notification settings - Fork 5
/
gulpfile.js
198 lines (170 loc) · 5.35 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
const gulp = require('gulp')
const uglify = require('gulp-uglify')
const del = require('del')
const sass = require('gulp-sass')(require('sass'))
const filelog = require('gulp-filelog')
const include = require('gulp-include')
const path = require('path')
// Paths
let environment
const repoRoot = path.join(__dirname)
const npmRoot = path.join(repoRoot, 'node_modules')
const govukFrontendRoot = path.join(npmRoot, 'govuk-frontend')
const assetsFolder = path.join(repoRoot, 'app', 'assets')
const staticFolder = path.join(repoRoot, 'app', 'static')
const govukFrontendFontsFolder = path.join(govukFrontendRoot, 'govuk', 'assets', 'fonts')
const govukFrontendImageFolder = path.join(govukFrontendRoot, 'govuk', 'assets', 'images')
// JavaScript paths
const jsSourceFile = path.join(assetsFolder, 'javascripts', 'application.js')
const jsDistributionFolder = path.join(staticFolder, 'javascripts')
const jsDistributionFile = 'application.js'
// CSS paths
const cssSourceGlob = path.join(assetsFolder, 'scss', 'application*.scss')
const cssDistributionFolder = path.join(staticFolder, 'stylesheets')
// Configuration
const sassOptions = {
development: {
outputStyle: 'expanded',
lineNumbers: true,
includePaths: [
assetsFolder + '/scss',
govukFrontendRoot
],
sourceComments: true,
errLogToConsole: true
},
production: {
outputStyle: 'compressed',
lineNumbers: true,
includePaths: [
assetsFolder + '/scss',
govukFrontendRoot
]
}
}
const uglifyOptions = {
development: {
mangle: false,
output: {
beautify: true,
semicolons: true,
comments: true,
indent_level: 2
},
compress: false
},
production: {
mangle: true
}
}
const logErrorAndExit = function logErrorAndExit (err) {
// coloured text: https://coderwall.com/p/yphywg/printing-colorful-text-in-terminal-when-run-node-js-script
console.log('\x1b[41m\x1b[37m Error: ' + err.message + '\x1b[0m')
process.exit(1)
}
gulp.task('clean:js', function () {
return del(jsDistributionFolder + '/**/*').then(function (paths) {
console.log('💥 Deleted the following JavaScript files:\n', paths.join('\n'))
})
})
gulp.task('clean:css', function () {
return del(cssDistributionFolder + '/**/*').then(function (paths) {
console.log('💥 Deleted the following CSS files:\n', paths.join('\n'))
})
})
gulp.task('clean', gulp.parallel('clean:js', 'clean:css'))
gulp.task('sass', function () {
const stream = gulp.src(cssSourceGlob)
.pipe(filelog('Compressing SCSS files'))
.pipe(
sass(sassOptions[environment]))
.on('error', logErrorAndExit)
.pipe(gulp.dest(cssDistributionFolder))
stream.on('end', function () {
console.log('💾 Compressed CSS saved as .css files in ' + cssDistributionFolder)
})
return stream
})
gulp.task('js', function () {
const stream = gulp.src(jsSourceFile, { sourcemaps: true })
.pipe(filelog('Compressing JavaScript files'))
.pipe(include({ hardFail: true }))
.pipe(uglify(
uglifyOptions[environment]
))
.pipe(gulp.dest(jsDistributionFolder, { sourcemaps: 'maps' }))
stream.on('end', function () {
console.log('💾 Compressed JavaScript saved as ' + jsDistributionFolder + '/' + jsDistributionFile)
})
return stream
})
function copyFactory (resourceName, sourceFolder, targetFolder) {
return function () {
return gulp
.src(sourceFolder + '/**/*', { base: sourceFolder })
.pipe(gulp.dest(targetFolder))
.on('end', function () {
console.log('📂 Copied ' + resourceName)
})
}
}
gulp.task(
'copy:images',
copyFactory(
'image assets from app to static folder',
assetsFolder + '/images',
staticFolder + '/images'
)
)
gulp.task(
'copy:svg',
copyFactory(
'image assets from app to static folder',
assetsFolder + '/svg',
staticFolder + '/svg'
)
)
gulp.task(
'copy:govuk_frontend_assets:fonts',
copyFactory(
'fonts from the GOVUK frontend assets',
govukFrontendFontsFolder,
staticFolder + '/fonts'
)
)
gulp.task(
'copy:govuk_frontend_assets:images',
copyFactory(
'images from the GOVUK frontend',
govukFrontendImageFolder,
path.join(staticFolder, 'images')
)
)
gulp.task('set_environment_to_development', function (cb) {
environment = 'development'
cb()
})
gulp.task('set_environment_to_production', function (cb) {
environment = 'production'
cb()
})
gulp.task('copy', gulp.parallel(
'copy:images',
'copy:svg',
'copy:govuk_frontend_assets:fonts',
'copy:govuk_frontend_assets:images'
))
gulp.task('compile', gulp.series('copy', gulp.parallel('sass', 'js')))
gulp.task('build:development', gulp.series(gulp.parallel('set_environment_to_development', 'clean'), 'compile'))
gulp.task('build:production', gulp.series(gulp.parallel('set_environment_to_production', 'clean'), 'compile'))
gulp.task('watch', gulp.series('build:development', function () {
const jsWatcher = gulp.watch([assetsFolder + '/**/*.js'], gulp.series('js'))
const cssWatcher = gulp.watch([assetsFolder + '/**/*.scss'], gulp.series('sass'))
const dmWatcher = gulp.watch([npmRoot + '/digitalmarketplace-frameworks/**'], gulp.series('copy:frameworks'))
const notice = function (event) {
console.log('File ' + event.path + ' was ' + event.type + ' running tasks...')
}
cssWatcher.on('change', notice)
jsWatcher.on('change', notice)
dmWatcher.on('change', notice)
}))