|
1 | 1 | const { dirname, join } = require('path')
|
2 | 2 |
|
| 3 | +const browserSync = require('browser-sync') |
| 4 | +const slash = require('slash') |
| 5 | + |
3 | 6 | const { paths } = require('../config') // specify paths to main working directories
|
4 | 7 | 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