/
index.js
138 lines (118 loc) · 3.97 KB
/
index.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
138
import { readFile } from 'fs'
import { createServer as createHttpsServer } from 'https'
import { createServer } from 'http'
import { resolve } from 'path'
import mime from 'mime'
import opener from 'opener'
let server
export default function serve (options = { contentBase: '' }) {
if (Array.isArray(options) || typeof options === 'string') {
options = { contentBase: options }
}
options.contentBase = Array.isArray(options.contentBase) ? options.contentBase : [options.contentBase]
options.host = options.host || 'localhost'
options.port = options.port || 10001
options.headers = options.headers || {}
options.https = options.https || false
options.openPage = options.openPage || ''
mime.default_type = 'text/plain'
const requestListener = (request, response) => {
// Remove querystring
const urlPath = decodeURI(request.url.split('?')[0])
Object.keys(options.headers).forEach((key) => {
response.setHeader(key, options.headers[key])
})
readFileFromContentBase(options.contentBase, urlPath, function (error, content, filePath) {
if (!error) {
return found(response, filePath, content)
}
if (error.code !== 'ENOENT') {
response.writeHead(500)
response.end('500 Internal Server Error' +
'\n\n' + filePath +
'\n\n' + Object.values(error).join('\n') +
'\n\n(rollup-plugin-serve)', 'utf-8')
return
}
if (options.historyApiFallback) {
var fallbackPath = typeof options.historyApiFallback === 'string' ? options.historyApiFallback : '/index.html'
readFileFromContentBase(options.contentBase, fallbackPath, function (error, content, filePath) {
if (error) {
notFound(response, filePath)
} else {
found(response, filePath, content)
}
})
} else {
notFound(response, filePath)
}
})
}
// release previous server instance if rollup is reloading configuration in watch mode
if (server) {
server.close()
}
// If HTTPS options are available, create an HTTPS server
if (options.https) {
server = createHttpsServer(options.https, requestListener).listen(options.port, options.host)
} else {
server = createServer(requestListener).listen(options.port, options.host)
}
closeServerOnTermination(server)
var running = options.verbose === false
return {
name: 'serve',
generateBundle () {
if (!running) {
running = true
// Log which url to visit
const url = (options.https ? 'https' : 'http') + '://' + options.host + ':' + options.port
options.contentBase.forEach(base => {
console.log(green(url) + ' -> ' + resolve(base))
})
// Open browser
if (options.open) {
opener(url + options.openPage)
}
}
}
}
}
function readFileFromContentBase (contentBase, urlPath, callback) {
let filePath = resolve(contentBase[0] || '.', '.' + urlPath)
// Load index.html in directories
if (urlPath.endsWith('/')) {
filePath = resolve(filePath, 'index.html')
}
readFile(filePath, (error, content) => {
if (error && contentBase.length > 1) {
// Try to read from next contentBase
readFileFromContentBase(contentBase.slice(1), urlPath, callback)
} else {
// We know enough
callback(error, content, filePath)
}
})
}
function notFound (response, filePath) {
response.writeHead(404)
response.end('404 Not Found' +
'\n\n' + filePath +
'\n\n(rollup-plugin-serve)', 'utf-8')
}
function found (response, filePath, content) {
response.writeHead(200, { 'Content-Type': mime.getType(filePath) })
response.end(content, 'utf-8')
}
function green (text) {
return '\u001b[1m\u001b[32m' + text + '\u001b[39m\u001b[22m'
}
function closeServerOnTermination (server) {
const terminationSignals = ['SIGINT', 'SIGTERM']
terminationSignals.forEach((signal) => {
process.on(signal, () => {
server.close()
process.exit()
})
})
}