|
| 1 | +import { readFile } from 'fs' |
| 2 | +import { createServer } from 'http' |
| 3 | +import { resolve } from 'path' |
| 4 | + |
| 5 | +import mime from 'mime' |
| 6 | +import opener from 'opener' |
| 7 | + |
| 8 | +export default function serve (options = {}) { |
| 9 | + options.contentBase = options.contentBase || '' |
| 10 | + options.host = options.host || 'localhost' |
| 11 | + options.port = options.port || 10001 |
| 12 | + const url = 'http://' + options.host + ':' + options.port |
| 13 | + |
| 14 | + mime.default_type = 'text/plain' |
| 15 | + |
| 16 | + createServer(function (request, response) { |
| 17 | + // Remove querystring |
| 18 | + var filePath = options.contentBase + request.url.split('?')[0] |
| 19 | + |
| 20 | + // Load index.html in directories |
| 21 | + if (filePath.endsWith('/')) { |
| 22 | + filePath += 'index.html' |
| 23 | + } |
| 24 | + |
| 25 | + readFile('.' + filePath, function (error, content) { |
| 26 | + if (error) { |
| 27 | + if (error.code === 'ENOENT') { |
| 28 | + if (request.url === '/favicon.ico') { |
| 29 | + filePath = resolve(__dirname, '../dist/favicon.ico') |
| 30 | + readFile(filePath, function (error, content) { |
| 31 | + if (error) { |
| 32 | + notFound(response, filePath) |
| 33 | + } else { |
| 34 | + found(response, filePath, content) |
| 35 | + } |
| 36 | + }) |
| 37 | + } else if (options.historyApiFallback) { |
| 38 | + filePath = resolve(options.contentBase, 'index.html') |
| 39 | + readFile(filePath, function (error, content) { |
| 40 | + if (error) { |
| 41 | + notFound(response, filePath) |
| 42 | + } else { |
| 43 | + found(response, filePath, content) |
| 44 | + } |
| 45 | + }) |
| 46 | + } else { |
| 47 | + notFound(response, filePath) |
| 48 | + } |
| 49 | + } else { |
| 50 | + response.writeHead(500) |
| 51 | + response.end('500 Internal Server Error' + |
| 52 | + '\n\n' + filePath + |
| 53 | + '\n\n' + Object.keys(error).map(k => error[k]).join('\n') + |
| 54 | + '\n\n(rollup-plugin-serve)', 'utf-8') |
| 55 | + } |
| 56 | + } else { |
| 57 | + found(response, filePath, content) |
| 58 | + } |
| 59 | + }) |
| 60 | + }).listen(options.port) |
| 61 | + |
| 62 | + var running = false |
| 63 | + |
| 64 | + return { |
| 65 | + name: 'serve', |
| 66 | + ongenerate () { |
| 67 | + if (!running && options.open) { |
| 68 | + running = true |
| 69 | + console.log('Server running at ' + green(url)) |
| 70 | + |
| 71 | + // Open browser |
| 72 | + opener(url) |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +function notFound (response, filePath) { |
| 79 | + response.writeHead(404) |
| 80 | + response.end('404 Not Found' + |
| 81 | + '\n\n' + filePath + |
| 82 | + '\n\n(rollup-plugin-serve)', 'utf-8') |
| 83 | +} |
| 84 | + |
| 85 | +function found (response, filePath, content) { |
| 86 | + response.writeHead(200, { 'Content-Type': mime.lookup(filePath) }) |
| 87 | + response.end(content, 'utf-8') |
| 88 | +} |
| 89 | + |
| 90 | +function green (text) { |
| 91 | + return '\u001b[1m\u001b[32m' + text + '\u001b[39m\u001b[22m' |
| 92 | +} |
0 commit comments