UNPKG

3.06 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 (relative).
46 * @returns {?*}
47 */
48const eventHandler = function(bs, event, filePath) {
49
50 const fileName = path.parse(filePath).base
51 const fileExtension = path.extname(filePath)
52
53 // Ignore change when filePath is junk
54 if (junk.is(fileName)===true) return
55
56 // Flush the cache no matter what event was send by Chokidar.
57 // This ensures that we serve the latest files when the user reloads the site.
58 cache.flush(filePath)
59
60 const styleExtensions = [
61 '.css',
62 '.scss',
63 '.sass',
64 '.less'
65 ]
66
67 // Reload stylesheets when the file extension is a known style extension
68 if (styleExtensions.includes(fileExtension)===true) return bs.reload('*.css')
69
70 const imageExtensions = [
71 '.png',
72 '.jpg',
73 '.jpeg',
74 '.svg',
75 '.gif',
76 '.webp'
77 ]
78
79 // Reload images when the file extension is a known image extension supported by Browsersync
80 if (imageExtensions.includes(fileExtension)===true) return bs.reload(`*${ fileExtension }`)
81
82 bs.reload()
83
84}
85
86/**
87 * Serve a directory and reload the page when files change.
88 * @public
89 * @param {String} srcPath - Path to the source folder.
90 * @param {Function} rewrite - URL rewrite middleware.
91 * @param {Function} redirect - URL redirect middleware.
92 * @param {Object} opts - Additional optional options.
93 * @param {Function} next - The callback that handles the response. Receives the following properties: err.
94 * @returns {?*}
95 */
96module.exports = function(srcPath, rewrite, redirect, opts, next) {
97
98 if (browserSync==null) {
99 return next(new Error('Rosid has been installed without optionalDependencies. Make sure that all optionalDependencies are installed before serving a site.'))
100 }
101
102 const bs = browserSync.create()
103
104 const server = {
105 baseDir: srcPath,
106 middleware: [
107 rewrite,
108 redirect
109 ]
110 }
111
112 const files = {
113 match: getFiles(),
114 fn: eventHandler.bind(null, bs),
115 options: {
116 ignoreInitial: true
117 }
118 }
119
120 const snippetOptions = {
121 blacklist: opts.static
122 }
123
124 const defaults = {
125 logPrefix: 'Rosid',
126 server: server,
127 files: [ files ],
128 notify: false,
129 ghostMode: false,
130 open: opts.open,
131 startPath: opts.path,
132 snippetOptions: snippetOptions
133 }
134
135 bs.init(defaults, next)
136
137}
\No newline at end of file