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 |
|
48 | const eventHandler = function(bs, event, filePath) {
|
49 |
|
50 | const fileName = path.parse(filePath).base
|
51 | const fileExtension = path.extname(filePath)
|
52 |
|
53 |
|
54 | if (junk.is(fileName)===true) return
|
55 |
|
56 |
|
57 |
|
58 | cache.flush(filePath)
|
59 |
|
60 | const styleExtensions = [
|
61 | '.css',
|
62 | '.scss',
|
63 | '.sass',
|
64 | '.less'
|
65 | ]
|
66 |
|
67 |
|
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 |
|
80 | if (imageExtensions.includes(fileExtension)===true) return bs.reload(`*${ fileExtension }`)
|
81 |
|
82 | bs.reload()
|
83 |
|
84 | }
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | module.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 |