UNPKG

2.91 kBJavaScriptView Raw
1'use strict'
2
3const path = require('path')
4const niceTry = require('nice-try')
5const junk = require('junk')
6const browserSync = niceTry(() => require('browser-sync'))
7const cache = require('./cache')
8
9/**
10 * Get a list of files to watch.
11 * @returns {Array} files
12 */
13const getFiles = function() {
14
15 // Exclude the following files
16 const excludedFiles = [
17 '!**/CVS',
18 '!**/.git',
19 '!**/.svn',
20 '!**/.hg',
21 '!**/.lock-wscript',
22 '!**/.wafpickle-N',
23 '!**/node_modules',
24 '!**/bower_components'
25 ]
26
27 // Include the following files
28 const includedFiles = [
29 '**/*'
30 ]
31
32 // Return all ignored files
33 return [
34 ...excludedFiles,
35 ...includedFiles
36 ]
37
38}
39
40/**
41 * Flushes the cache and reloads the site.
42 * Should be executed when a file gets updated.
43 * @param {Object} bs - Browsersync instance.
44 * @param {String} event - Event sent by Chokidar.
45 * @param {String} filePath - File affected by event.
46 */
47const eventHandler = function(bs, event, filePath) {
48
49 const fileName = path.parse(filePath).base
50
51 // Ignore change when filePath is junk
52 if (junk.is(fileName)===true) return
53
54 // Get the extension of the filePath
55 const extension = path.extname(filePath)
56
57 // Flush the cache no matter what event was send by Chokidar.
58 // This ensures that we serve the latest files when the user reloads the site.
59 cache.flush(extension)
60
61 // Chokidar always sends an 'event' property - which could be 'add',
62 // 'unlink' etc so we need to check for that and only respond to 'change'.
63 if (event==='change') {
64
65 const styleExtensions = [
66 '.css',
67 '.scss',
68 '.sass',
69 '.less'
70 ]
71
72 // Reload stylesheets only when the extension is a known style extension
73 if (styleExtensions.includes(extension)===true) return bs.reload('*.css')
74
75 return bs.reload()
76
77 }
78
79}
80
81/**
82 * Serve a directory and reload the page when files change.
83 * @public
84 * @param {String} srcPath - Path to the source folder.
85 * @param {Function} rewrite - URL rewrite middleware.
86 * @param {Function} redirect - URL redirect middleware.
87 * @param {Object} opts - Additional optional options.
88 * @param {Function} next - The callback that handles the response. Receives the following properties: err.
89 */
90module.exports = function(srcPath, rewrite, redirect, opts, next) {
91
92 if (browserSync==null) {
93 return next(new Error('Rosid has been installed without optionalDependencies. Make sure that all optionalDependencies are installed before serving a site.'))
94 }
95
96 const bs = browserSync.create()
97
98 const server = {
99 baseDir : srcPath,
100 middleware : [
101 rewrite,
102 redirect
103 ]
104 }
105
106 const files = {
107 match : getFiles(),
108 fn : eventHandler.bind(null, bs),
109 options : {
110 usePolling: opts.polling
111 }
112 }
113
114 const defaults = {
115 logPrefix : 'Rosid',
116 server : server,
117 files : [ files ],
118 notify : false,
119 ghostMode : false,
120 open : opts.open
121 }
122
123 bs.init(defaults, next)
124
125}
\No newline at end of file