-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up Browsersync with partial rebuild support
See: #2750 (comment)
- Loading branch information
1 parent
b457c86
commit 3727d87
Showing
3 changed files
with
63 additions
and
73 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,52 @@ | ||
const { dirname, join } = require('path') | ||
|
||
const browserSync = require('browser-sync') | ||
const slash = require('slash') | ||
|
||
const { paths } = require('../config') // specify paths to main working directories | ||
const metalsmith = require('../lib/metalsmith') // configured static site generator | ||
const browsersync = require('../lib/metalsmith-browser-sync') // setup synchronised browser testing | ||
|
||
// setup synchronised browser testing | ||
metalsmith.use(browsersync({ | ||
ghostMode: false, // Ghost mode tries to check the same input across examples. | ||
open: false, // When making changes to the server, we don't want multiple windows opening. | ||
server: paths.public, // server directory | ||
files: [ | ||
join(paths.source, '**/*'), | ||
join(paths.views, '**/*'), | ||
join(dirname(require.resolve('govuk-frontend/package.json')), '**/*') | ||
] // files to watch | ||
})) | ||
|
||
// build to destination directory | ||
metalsmith.build(function (err, files) { | ||
if (err) { throw err } | ||
}) | ||
|
||
let bs | ||
|
||
metalsmith | ||
|
||
// Configure sources to watch for rebuilds | ||
.watch([ | ||
slash(paths.source), | ||
slash(paths.views), | ||
slash(dirname(require.resolve('govuk-frontend/package.json'))) | ||
]) | ||
|
||
// Build to destination directory | ||
.build((err, files) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
if (metalsmith.watch()) { | ||
if (bs) { | ||
return | ||
} | ||
|
||
// Setup synchronised browser testing | ||
bs = browserSync.create() | ||
|
||
bs.init({ | ||
// Configure output to watch for reloads | ||
files: [ | ||
join(paths.public, '**/*.html'), | ||
join(paths.public, 'javascripts/**/*.js'), | ||
join(paths.public, 'stylesheets/**/*.css') | ||
], | ||
|
||
// Prevent browser mirroring | ||
ghostMode: false, | ||
|
||
// Prevent browser opening | ||
open: false, | ||
|
||
// Serve files from directory | ||
server: paths.public | ||
}) | ||
} | ||
}) |