Skip to content

Commit 033cc74

Browse files
Set up Browsersync with partial rebuild support
See: #2750 (comment)
1 parent 44e6eba commit 033cc74

File tree

3 files changed

+63
-73
lines changed

3 files changed

+63
-73
lines changed

lib/metalsmith-browser-sync.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

lib/metalsmith.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ const titleChecker = require('./metalsmith-title-checker.js')
3131
const navigation = require('./navigation.js') // navigation plugin
3232
const rollup = require('./rollup') // used to build GOV.UK Frontend JavaScript
3333

34+
// Flag production mode (to skip plugins in development)
35+
const isProduction = process.env.NODE_ENV !== 'development'
36+
3437
// Nunjucks engine options
3538
const nunjucksOptions = {
3639
noCache: true, // never use a cache and recompile templates each time
@@ -66,14 +69,19 @@ const nunjucksOptions = {
6669
// static site generator
6770
module.exports = metalsmith(resolve(__dirname, '../'))
6871

72+
// notify build starting
73+
.use((files, metalsmith) => metalsmith.watch() &&
74+
console.log('Metalsmith build running')
75+
)
76+
6977
// source directory
7078
.source(paths.source)
7179

7280
// destination directory
7381
.destination(paths.public)
7482

75-
// clean destination before build
76-
.clean(true)
83+
// clean destination for production builds
84+
.clean(isProduction)
7785

7886
// global variables used in layout files
7987
.metadata({
@@ -236,5 +244,10 @@ module.exports = metalsmith(resolve(__dirname, '../'))
236244
pattern: ['**/*.html', '!**/default/*.html']
237245
}))
238246

247+
// notify build starting
248+
.use((files, metalsmith) => metalsmith.watch() &&
249+
console.log('Metalsmith build complete')
250+
)
251+
239252
// Debug
240253
// .use(debug())

tasks/start.js

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,52 @@
11
const { dirname, join } = require('path')
22

3+
const browserSync = require('browser-sync')
4+
const slash = require('slash')
5+
36
const { paths } = require('../config') // specify paths to main working directories
47
const metalsmith = require('../lib/metalsmith') // configured static site generator
5-
const browsersync = require('../lib/metalsmith-browser-sync') // setup synchronised browser testing
6-
7-
// setup synchronised browser testing
8-
metalsmith.use(browsersync({
9-
ghostMode: false, // Ghost mode tries to check the same input across examples.
10-
open: false, // When making changes to the server, we don't want multiple windows opening.
11-
server: paths.public, // server directory
12-
files: [
13-
join(paths.source, '**/*'),
14-
join(paths.views, '**/*'),
15-
join(dirname(require.resolve('govuk-frontend/package.json')), '**/*')
16-
] // files to watch
17-
}))
18-
19-
// build to destination directory
20-
metalsmith.build(function (err, files) {
21-
if (err) { throw err }
22-
})
8+
9+
let bs
10+
11+
metalsmith
12+
13+
// Configure sources to watch for rebuilds
14+
.watch([
15+
slash(paths.source),
16+
slash(paths.views),
17+
slash(dirname(require.resolve('govuk-frontend/package.json')))
18+
])
19+
20+
// Build to destination directory
21+
.build((err, files) => {
22+
if (err) {
23+
throw err
24+
}
25+
26+
if (metalsmith.watch()) {
27+
if (bs) {
28+
return
29+
}
30+
31+
// Setup synchronised browser testing
32+
bs = browserSync.create()
33+
34+
bs.init({
35+
// Configure output to watch for reloads
36+
files: [
37+
join(paths.public, '**/*.html'),
38+
join(paths.public, 'javascripts/**/*.js'),
39+
join(paths.public, 'stylesheets/**/*.css')
40+
],
41+
42+
// Prevent browser mirroring
43+
ghostMode: false,
44+
45+
// Prevent browser opening
46+
open: false,
47+
48+
// Serve files from directory
49+
server: paths.public
50+
})
51+
}
52+
})

0 commit comments

Comments
 (0)