forked from digitalnsw/nsw-design-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
413 lines (377 loc) · 11.2 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
const {
src, dest, watch, series,
} = require('gulp')
const sass = require('gulp-sass')(require('sass'))
const postcss = require('gulp-postcss')
const autoprefixer = require('autoprefixer')
const cssnano = require('cssnano')
const sourcemaps = require('gulp-sourcemaps')
const browsersync = require('browser-sync')
const surge = require('gulp-surge')
const zip = require('gulp-zip')
const del = require('del')
const postcssNormalize = require('postcss-normalize')
const sassGlob = require('gulp-sass-glob')
const Metalsmith = require('metalsmith')
const layouts = require('metalsmith-layouts')
const inplace = require('metalsmith-in-place')
const collections = require('metalsmith-collections')
const ignore = require('metalsmith-ignore')
const discoverPartials = require('metalsmith-discover-partials')
const dataLoader = require('metalsmith-data-loader')
const dynamicCollections = require('metalsmith-dynamic-collections')
// const debug = require('metalsmith-debug-ui')
const discoverHelpers = require('metalsmith-discover-helpers')
const rollup = require('gulp-better-rollup')
const nodeResolve = require('@rollup/plugin-node-resolve')
const sitemap = require('gulp-sitemap')
const babel = require('@rollup/plugin-babel')
const eslint = require('gulp-eslint-new')
const gulpStylelint = require('gulp-stylelint')
const replace = require('gulp-replace')
const inject = require('gulp-inject-string')
const fs = require('fs')
const { argv } = require('yargs')
const bump = require('gulp-bump')
const config = require('./config')
const package = require('./package')
const server = browsersync.create()
sass.compiler = require('sass')
const postcssProcessors = [
postcssNormalize({ forceImport: true }),
autoprefixer({ grid: true }),
cssnano,
]
function moveImages() {
return src(config.images.src)
.pipe(dest(config.images.build))
}
function moveBrand() {
return src(config.brand.src)
.pipe(dest(config.brand.build))
}
function buildStyles() {
return src(config.scss.src)
.pipe(sourcemaps.init())
.pipe(sassGlob())
.pipe(sass().on('error', sass.logError))
.pipe(postcss(postcssProcessors))
.pipe(sourcemaps.write('.'))
.pipe(dest(config.scss.build))
}
function buildCoreStyles() {
return src(config.scssCore.src)
.pipe(sourcemaps.init())
.pipe(sassGlob())
.pipe(sass().on('error', sass.logError))
.pipe(postcss(postcssProcessors))
.pipe(sourcemaps.write('.'))
.pipe(dest(config.scssCore.build))
}
function buildDocStyles() {
return src(config.scssDocs.src)
.pipe(sourcemaps.init())
.pipe(sassGlob())
.pipe(sass().on('error', sass.logError))
.pipe(postcss(postcssProcessors))
.pipe(sourcemaps.write('.'))
.pipe(dest(config.scssDocs.build))
}
function lintStyles() {
return src(config.scss.watch)
.pipe(gulpStylelint({
reporters: [
{ formatter: 'string', console: true },
],
}))
}
function generateSitemap() {
return src(['./dist/**/*.html', '!**/blank.html'], { read: false })
.pipe(sitemap({ siteUrl: config.baseUrl.full }))
.pipe(dest(config.dir.build))
}
function browserSync(done) {
server.init(config.browsersync)
done()
}
function reload(done) {
server.reload()
done()
}
function surgeDeploy() {
return surge(config.surge)
}
function cleanUp() {
return del([config.dir.build, config.dir.temp], { force: true })
}
function cleanBuild(files, metalsmith, done) {
const fileNames = Object.keys(files)
fileNames.forEach((path) => {
const file = path.split('.')
if (
file[0].indexOf('assets') > -1
|| file[0].indexOf('partials') > -1
|| file[0].indexOf('partials') > -1
|| file[0].indexOf('_') > -1
) {
// eslint-disable-next-line no-param-reassign
delete files[path]
}
})
done(null, files)
}
function sortByAlpha(a, b) {
const nameA = a.title.toLowerCase()
const nameB = b.title.toLowerCase()
if (nameA < nameB) { return -1 }
if (nameA > nameB) { return 1 }
return 0
}
function metalsmithBuild(callback) {
const metalsmith = new Metalsmith(__dirname)
// debug.patch(metalsmith)
metalsmith.metadata(package)
metalsmith.source(config.metalSmith.src)
metalsmith.destination(config.metalSmith.build)
metalsmith.use(ignore(config.metalSmith.ignoreFiles))
metalsmith.clean(false)
metalsmith.use(discoverHelpers(config.metalSmith.helpers))
metalsmith.use(discoverPartials(config.metalSmith.partials))
metalsmith.use(dataLoader(config.metalSmith.data))
metalsmith.use(collections({
components: {
pattern: config.metalSmith.collection.components.pattern,
sortBy: sortByAlpha,
},
core: {
pattern: config.metalSmith.collection.core.pattern,
sortBy: sortByAlpha,
},
templates: {
pattern: config.metalSmith.collection.templates.pattern,
sortBy: sortByAlpha,
},
}))
metalsmith.use(dynamicCollections({
componentsnav: {
pattern: config.metalSmith.collection.componentsnav.pattern,
refer: false,
sortBy: sortByAlpha,
},
corenav: {
pattern: config.metalSmith.collection.corenav.pattern,
refer: false,
sortBy: 'order',
},
templatesnav: {
pattern: config.metalSmith.collection.templatesnav.pattern,
refer: false,
sortBy: 'order',
},
guidancetab: {
pattern: config.metalSmith.collection.guidancetab.pattern,
refer: false,
sortBy: sortByAlpha,
},
about: {
pattern: config.metalSmith.collection.contentnav.about,
refer: false,
sortBy: 'order',
},
design: {
pattern: config.metalSmith.collection.contentnav.design,
refer: false,
sortBy: 'order',
},
develop: {
pattern: config.metalSmith.collection.contentnav.develop,
refer: false,
sortBy: 'order',
},
utilities: {
pattern: config.metalSmith.collection.contentnav.utilities,
refer: false,
sortBy: 'order',
},
contribute: {
pattern: config.metalSmith.collection.contentnav.contribute,
refer: false,
sortBy: 'order',
},
methods: {
pattern: config.metalSmith.collection.contentnav.methods,
refer: false,
sortBy: 'order',
},
}))
metalsmith.use(inplace(config.metalSmith.inplace))
metalsmith.use(layouts(config.metalSmith.layouts))
metalsmith.use(cleanBuild)
metalsmith.build((err) => {
if (err) {
throw err
}
callback()
})
}
function compileJS() {
return src(config.js.src)
.pipe(
rollup(
{
plugins: [babel({ babelHelpers: 'bundled' }), nodeResolve()],
},
{
name: 'NSW',
format: 'umd',
},
),
)
.pipe(dest(config.js.build))
}
function compileDocsJS() {
return src(config.jsDocs.src)
.pipe(
rollup(
{
plugins: [babel()],
},
),
)
.pipe(dest(config.jsDocs.build))
}
function lintJavascript() {
return src(config.js.watch)
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failOnError())
}
function copyFavicon() {
return src(config.favicon.src)
.pipe(dest(config.favicon.build))
}
function moveSitemap() {
return src(config.sitemap.src)
.pipe(dest(config.sitemap.build))
}
function moveSearch() {
return src(config.search.src)
.pipe(dest(config.search.build))
}
function zipDistFolder() {
return src(config.zipfile.src)
.pipe(zip(config.zipfile.filename))
.pipe(dest(config.zipfile.build))
}
function renamePath() {
return src(`${config.dir.build}index.html`)
.pipe(replace('/css/main.css', './css/main.css'))
.pipe(replace('/js/main.js', './js/main.js'))
.pipe(replace('/favicon.ico', './favicon.ico'))
.pipe(dest(config.dir.build))
.pipe(src([`${config.dir.build}**/*.html`, `!${config.dir.build}index.html`]))
.pipe(replace('/css/main.css', '../../css/main.css'))
.pipe(replace('/js/main.js', '../../js/main.js'))
.pipe(replace('/favicon.ico', '../../favicon.ico'))
.pipe(dest(config.dir.build))
}
function renamePathForProd() {
return src(`${config.dir.build}/**/*.html`)
.pipe(replace('href="/', `href="${config.baseUrl.prod}/`))
.pipe(replace('src="/', `src="${config.baseUrl.prod}/`))
.pipe(dest(config.dir.build))
}
function addAnalytics() {
return src(`${config.dir.build}/**/*.html`)
.pipe(inject.after('<head>', `<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-P2NKBBJZ');</script>
`))
.pipe(inject.after('<head>', `<script async src="https://www.googletagmanager.com/gtag/js?id=G-49T9M12F86"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-49T9M12F86');
</script>
`))
.pipe(inject.after('<body>', `<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-P2NKBBJZ"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
`))
.pipe(dest(config.dir.build))
}
function bumping() {
return src('./package.json')
.pipe(bump({ type: argv.type }))
.pipe(dest('./'))
}
const styles = series(lintStyles, buildStyles, buildCoreStyles, buildDocStyles)
const javascript = series(lintJavascript, compileJS, compileDocsJS)
function watchFiles(done) {
watch(config.scss.watch, series(styles, reload))
watch(config.js.watch, series(javascript, reload))
watch(config.jsDocs.watch, series(javascript, reload))
watch(config.images.watch, series(moveImages, reload))
watch(config.brand.watch, series(moveBrand, reload))
watch(config.metalSmith.watch, series(metalsmithBuild, reload))
done()
}
const buildprod = series(
cleanUp,
copyFavicon,
moveSitemap,
moveSearch,
metalsmithBuild,
styles,
javascript,
moveImages,
moveBrand,
renamePathForProd,
addAnalytics,
zipDistFolder,
generateSitemap,
)
const build = series(
cleanUp,
copyFavicon,
moveSitemap,
moveSearch,
metalsmithBuild,
styles,
javascript,
moveImages,
moveBrand,
zipDistFolder,
)
const dev = series(
cleanUp,
copyFavicon,
moveSitemap,
moveSearch,
metalsmithBuild,
styles,
javascript,
moveImages,
moveBrand,
watchFiles,
browserSync,
)
const deploy = series(
surgeDeploy,
)
// Export commands.
exports.scss = buildStyles // gulp sass - compiles the sass
exports.watch = watchFiles // gulp watch - watches the files
exports.lint = lintStyles // gulp lint - lints the sass
exports.images = moveImages // gulp images - moves images
exports.brand = moveBrand // gulp images - moves brand files
exports.buildprod = buildprod // gulp build - builds the files
exports.build = build // gulp build - builds the files
exports.surge = deploy // gulp surge - builds the files and deploys to surge
exports.clean = cleanUp // gulp clean - clean the dist directory
exports.metal = metalsmithBuild // gulp metal - generates static site of components
exports.js = javascript // gulp js - compiles the js
exports.bumping = bumping // gulp bump - bumps the version number in specific files - used for releases
exports.default = dev // gulp - default gulp task