1 | 'use strict'
|
2 |
|
3 | const path = require('path')
|
4 | const niceTry = require('nice-try')
|
5 | const junk = require('junk')
|
6 | const browserSync = niceTry(() => require('browser-sync'))
|
7 | const cache = require('./cache')
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const getFiles = function() {
|
14 |
|
15 |
|
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 |
|
28 | const includedFiles = [
|
29 | '**/*'
|
30 | ]
|
31 |
|
32 |
|
33 | return [
|
34 | ...excludedFiles,
|
35 | ...includedFiles
|
36 | ]
|
37 |
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | const eventHandler = function(bs, event, filePath) {
|
48 |
|
49 | const fileName = path.parse(filePath).base
|
50 |
|
51 |
|
52 | if (junk.is(fileName)===true) return
|
53 |
|
54 |
|
55 | const extension = path.extname(filePath)
|
56 |
|
57 |
|
58 |
|
59 | cache.flush(extension)
|
60 |
|
61 |
|
62 |
|
63 | if (event==='change') {
|
64 |
|
65 | const styleExtensions = [
|
66 | '.css',
|
67 | '.scss',
|
68 | '.sass',
|
69 | '.less'
|
70 | ]
|
71 |
|
72 |
|
73 | if (styleExtensions.includes(extension)===true) return bs.reload('*.css')
|
74 |
|
75 | return bs.reload()
|
76 |
|
77 | }
|
78 |
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | module.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 |