Skip to content

Commit

Permalink
Update to v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
thgh committed Sep 29, 2016
1 parent 7371f30 commit a08dea5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 47 deletions.
14 changes: 10 additions & 4 deletions CHANGELOG.md
Expand Up @@ -4,13 +4,19 @@ All notable changes to `rollup-plugin-serve` will be documented in this file.

## [Unreleased]

## [0.0.2]
## [0.1.0] - 2016-09-29
### Added
- Option `open` open the project in browser
- Option `historyApiFallback`
- Default green favicon

## [0.0.1] - 2016-09-24
## Changed
- Option `root` is now `contentBase`

## [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
[Unreleased]: https://github.com/thgh/rollup-plugin-serve/compare/v0.1.0...HEAD
[0.1.0]: https://github.com/thgh/rollup-plugin-serve/compare/v0.0.1...v0.1.0
[0.0.1]: https://github.com/thgh/rollup-plugin-serve/releases
31 changes: 23 additions & 8 deletions README.md
Expand Up @@ -30,17 +30,32 @@ 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
})
serve('dist')
]
}
```

### Options

By default it serves the current project folder. Change it by passing a string:
```
serve('public') // will be used as contentBase
// Default options
serve({
// Folder to serve files from,
contentBase: '',
// Set to true to return index.html instead of 404
historyApiFallback: false,
// Options used in setting up server
host: 'localhost',
port: 10001
})
```

>>>>>>> master
## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Expand All @@ -65,4 +80,4 @@ 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
[rollup-plugin-serve]: https://www.npmjs.com/package/rollup-plugin-serve
4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "rollup-plugin-serve",
"version": "0.0.1",
"version": "0.1.0",
"description": "Serve your rolled up bundle",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
Expand Down Expand Up @@ -36,7 +36,7 @@
"opener": "^1.4.2"
},
"devDependencies": {
"rollup": "^0.36.0",
"rollup": "^0.36.1",
"rollup-plugin-buble": "^0.14.0",
"rollup-watch": "^2.5.0"
}
Expand Down
66 changes: 33 additions & 33 deletions src/index.js
Expand Up @@ -15,46 +15,46 @@ export default function serve (options = {}) {

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

// Load index.html in directories
if (filePath.endsWith('/')) {
filePath += 'index.html'
if (urlPath.endsWith('/')) {
filePath = resolve(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)
}
})
readFile(filePath, function (error, content) {
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.keys(error).map(function (k) { return error[k]; }).join('\n') +
'\n\n(rollup-plugin-serve)', 'utf-8')
return
}
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 {
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)
notFound(response, filePath)
}
})
}).listen(options.port)
Expand All @@ -66,7 +66,7 @@ export default function serve (options = {}) {
ongenerate () {
if (!running && options.open) {
running = true
console.log('Server running at ' + green(url))
console.log(green(url) + ' -> ' + resolve(options.contentBase))

// Open browser
opener(url)
Expand Down

0 comments on commit a08dea5

Please sign in to comment.