Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
thgh committed Sep 24, 2016
0 parents commit 7371f30
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Logs
logs
*.log
npm-debug.log*

# Dependency directory
node_modules

# Unwanted
.idea
.DS_Store

# Build files
dist/*
!dist/favicon.ico
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Changelog

All notable changes to `rollup-plugin-serve` will be documented in this file.

## [Unreleased]

## [0.0.2]

## [0.0.1] - 2016-09-24

### Added
- Initial version

[Unreleased]: https://github.com/thgh/rollup-plugin-serve/compare/v0.0.1...HEAD
[0.0.2]: https://github.com/thgh/rollup-plugin-serve/compare/v0.0.1...v0.0.2
[0.0.1]: https://github.com/thgh/rollup-plugin-serve/releases
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Thomas Ghysels

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Rollup plugin to serve the bundle

<a href="LICENSE">
<img src="https://img.shields.io/badge/license-MIT-brightgreen.svg" alt="Software License" />
</a>
<a href="https://github.com/thgh/rollup-plugin-serve/issues">
<img src="https://img.shields.io/github/issues/thgh/rollup-plugin-serve.svg" alt="Issues" />
</a>
<a href="http://standardjs.com/">
<img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg" alt="JavaScript Style Guide" />
</a>
<a href="https://npmjs.org/package/rollup-plugin-serve">
<img src="https://img.shields.io/npm/v/rollup-plugin-serve.svg?style=flat-squar" alt="NPM" />
</a>
<a href="https://github.com/thgh/rollup-plugin-serve/releases">
<img src="https://img.shields.io/github/release/thgh/rollup-plugin-serve.svg" alt="Latest Version" />
</a>

## Installation
```
npm install --save-dev rollup-plugin-serve
```

## Usage
```js
// rollup.config.js
import serve from 'rollup-plugin-serve'

export default {
entry: 'entry.js',
dest: 'bundle.js',
plugins: [
serve({
// Where to serve files from
contentBase: 'dist/',

// Return index.html instead of 404
historyApiFallback: false
})
]
}
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

## Contributing

Contributions and feedback are very welcome.

To get it running:
1. Clone the project.
2. `npm install`
3. `npm run build`

## Credits

- [Thomas Ghysels](https://github.com/thgh)
- [All Contributors][link-contributors]

## License

The MIT License (MIT). Please see [License File](LICENSE) for more information.

[link-author]: https://github.com/thgh
[link-contributors]: ../../contributors
[serve]: https://www.npmjs.com/package/serve
Binary file added dist/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "rollup-plugin-serve",
"version": "0.0.1",
"description": "Serve your rolled up bundle",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
"jsnext:main": "dist/index.es.js",
"scripts": {
"build": "rollup -c -f cjs -o dist/index.cjs.js && rollup -c -f es -o dist/index.es.js",
"dev": "rollup -cw -f cjs -o dist/index.cjs.js",
"lint": "standard rollup.config.js src/**",
"prepublish": "npm run build"
},
"keywords": [
"rollup",
"rollup-plugin",
"serve",
"dev-server",
"static"
],
"license": "MIT",
"author": "Thomas Ghysels <info@thomasg.be>",
"homepage": "https://github.com/thgh/rollup-plugin-serve",
"bugs": {
"url": "https://github.com/thgh/rollup-plugin-serve/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/thgh/rollup-plugin-serve"
},
"files": [
"dist"
],
"dependencies": {
"mime": "^1.3.4",
"opener": "^1.4.2"
},
"devDependencies": {
"rollup": "^0.36.0",
"rollup-plugin-buble": "^0.14.0",
"rollup-watch": "^2.5.0"
}
}
15 changes: 15 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import buble from 'rollup-plugin-buble'

export default {
entry: 'src/index.js',
dest: 'dist/index.cjs.js',
plugins: [
buble()
],
// Cleaner console
onwarn (msg) {
if (msg && msg.startsWith('Treating')) {
return
}
}
}
92 changes: 92 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { readFile } from 'fs'
import { createServer } from 'http'
import { resolve } from 'path'

import mime from 'mime'
import opener from 'opener'

export default function serve (options = {}) {
options.contentBase = options.contentBase || ''
options.host = options.host || 'localhost'
options.port = options.port || 10001
const url = 'http://' + options.host + ':' + options.port

mime.default_type = 'text/plain'

createServer(function (request, response) {
// Remove querystring
var filePath = options.contentBase + request.url.split('?')[0]

// Load index.html in directories
if (filePath.endsWith('/')) {
filePath += 'index.html'
}

readFile('.' + filePath, function (error, content) {
if (error) {
if (error.code === 'ENOENT') {
if (request.url === '/favicon.ico') {
filePath = resolve(__dirname, '../dist/favicon.ico')
readFile(filePath, function (error, content) {
if (error) {
notFound(response, filePath)
} else {
found(response, filePath, content)
}
})
} else if (options.historyApiFallback) {
filePath = resolve(options.contentBase, 'index.html')
readFile(filePath, function (error, content) {
if (error) {
notFound(response, filePath)
} else {
found(response, filePath, content)
}
})
} else {
notFound(response, filePath)
}
} else {
response.writeHead(500)
response.end('500 Internal Server Error' +
'\n\n' + filePath +
'\n\n' + Object.keys(error).map(k => error[k]).join('\n') +
'\n\n(rollup-plugin-serve)', 'utf-8')
}
} else {
found(response, filePath, content)
}
})
}).listen(options.port)

var running = false

return {
name: 'serve',
ongenerate () {
if (!running && options.open) {
running = true
console.log('Server running at ' + green(url))

// Open browser
opener(url)
}
}
}
}

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.lookup(filePath) })
response.end(content, 'utf-8')
}

function green (text) {
return '\u001b[1m\u001b[32m' + text + '\u001b[39m\u001b[22m'
}

0 comments on commit 7371f30

Please sign in to comment.