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