Skip to content

Commit 7371f30

Browse files
committed
Initial commit
0 parents  commit 7371f30

File tree

8 files changed

+270
-0
lines changed

8 files changed

+270
-0
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Dependency directory
7+
node_modules
8+
9+
# Unwanted
10+
.idea
11+
.DS_Store
12+
13+
# Build files
14+
dist/*
15+
!dist/favicon.ico

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Changelog
2+
3+
All notable changes to `rollup-plugin-serve` will be documented in this file.
4+
5+
## [Unreleased]
6+
7+
## [0.0.2]
8+
9+
## [0.0.1] - 2016-09-24
10+
11+
### Added
12+
- Initial version
13+
14+
[Unreleased]: https://github.com/thgh/rollup-plugin-serve/compare/v0.0.1...HEAD
15+
[0.0.2]: https://github.com/thgh/rollup-plugin-serve/compare/v0.0.1...v0.0.2
16+
[0.0.1]: https://github.com/thgh/rollup-plugin-serve/releases

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Thomas Ghysels
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Rollup plugin to serve the bundle
2+
3+
<a href="LICENSE">
4+
<img src="https://img.shields.io/badge/license-MIT-brightgreen.svg" alt="Software License" />
5+
</a>
6+
<a href="https://github.com/thgh/rollup-plugin-serve/issues">
7+
<img src="https://img.shields.io/github/issues/thgh/rollup-plugin-serve.svg" alt="Issues" />
8+
</a>
9+
<a href="http://standardjs.com/">
10+
<img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg" alt="JavaScript Style Guide" />
11+
</a>
12+
<a href="https://npmjs.org/package/rollup-plugin-serve">
13+
<img src="https://img.shields.io/npm/v/rollup-plugin-serve.svg?style=flat-squar" alt="NPM" />
14+
</a>
15+
<a href="https://github.com/thgh/rollup-plugin-serve/releases">
16+
<img src="https://img.shields.io/github/release/thgh/rollup-plugin-serve.svg" alt="Latest Version" />
17+
</a>
18+
19+
## Installation
20+
```
21+
npm install --save-dev rollup-plugin-serve
22+
```
23+
24+
## Usage
25+
```js
26+
// rollup.config.js
27+
import serve from 'rollup-plugin-serve'
28+
29+
export default {
30+
entry: 'entry.js',
31+
dest: 'bundle.js',
32+
plugins: [
33+
serve({
34+
// Where to serve files from
35+
contentBase: 'dist/',
36+
37+
// Return index.html instead of 404
38+
historyApiFallback: false
39+
})
40+
]
41+
}
42+
```
43+
44+
## Changelog
45+
46+
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
47+
48+
## Contributing
49+
50+
Contributions and feedback are very welcome.
51+
52+
To get it running:
53+
1. Clone the project.
54+
2. `npm install`
55+
3. `npm run build`
56+
57+
## Credits
58+
59+
- [Thomas Ghysels](https://github.com/thgh)
60+
- [All Contributors][link-contributors]
61+
62+
## License
63+
64+
The MIT License (MIT). Please see [License File](LICENSE) for more information.
65+
66+
[link-author]: https://github.com/thgh
67+
[link-contributors]: ../../contributors
68+
[serve]: https://www.npmjs.com/package/serve

dist/favicon.ico

318 Bytes
Binary file not shown.

package.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "rollup-plugin-serve",
3+
"version": "0.0.1",
4+
"description": "Serve your rolled up bundle",
5+
"main": "dist/index.cjs.js",
6+
"module": "dist/index.es.js",
7+
"jsnext:main": "dist/index.es.js",
8+
"scripts": {
9+
"build": "rollup -c -f cjs -o dist/index.cjs.js && rollup -c -f es -o dist/index.es.js",
10+
"dev": "rollup -cw -f cjs -o dist/index.cjs.js",
11+
"lint": "standard rollup.config.js src/**",
12+
"prepublish": "npm run build"
13+
},
14+
"keywords": [
15+
"rollup",
16+
"rollup-plugin",
17+
"serve",
18+
"dev-server",
19+
"static"
20+
],
21+
"license": "MIT",
22+
"author": "Thomas Ghysels <[email protected]>",
23+
"homepage": "https://github.com/thgh/rollup-plugin-serve",
24+
"bugs": {
25+
"url": "https://github.com/thgh/rollup-plugin-serve/issues"
26+
},
27+
"repository": {
28+
"type": "git",
29+
"url": "https://github.com/thgh/rollup-plugin-serve"
30+
},
31+
"files": [
32+
"dist"
33+
],
34+
"dependencies": {
35+
"mime": "^1.3.4",
36+
"opener": "^1.4.2"
37+
},
38+
"devDependencies": {
39+
"rollup": "^0.36.0",
40+
"rollup-plugin-buble": "^0.14.0",
41+
"rollup-watch": "^2.5.0"
42+
}
43+
}

rollup.config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import buble from 'rollup-plugin-buble'
2+
3+
export default {
4+
entry: 'src/index.js',
5+
dest: 'dist/index.cjs.js',
6+
plugins: [
7+
buble()
8+
],
9+
// Cleaner console
10+
onwarn (msg) {
11+
if (msg && msg.startsWith('Treating')) {
12+
return
13+
}
14+
}
15+
}

src/index.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)