Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export interface RollupServeOptions {
*/
contentBase?: string | string[]

/**
* URL path prefix applied to all requests.
* E.g. If `contentBasePublicPath="/foo/bar"` and a request is made to `/foo/bar/baz.js`, `baz.js` will be returned from `contentBase`.
*/
contentBasePublicPath?: string

/**
* Set to `true` to return index.html (200) instead of error page (404)
* or path to fallback page
Expand Down
22 changes: 16 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function serve (options = { contentBase: '' }) {
options = { contentBase: options }
}
options.contentBase = Array.isArray(options.contentBase) ? options.contentBase : [options.contentBase || '']
options.contentBasePublicPath = options.contentBasePublicPath || ''
options.port = options.port || 10001
options.headers = options.headers || {}
options.https = options.https || false
Expand All @@ -39,7 +40,7 @@ function serve (options = { contentBase: '' }) {
response.setHeader(key, options.headers[key])
})

readFileFromContentBase(options.contentBase, urlPath, function (error, content, filePath) {
readFileFromContentBase(options.contentBase, options.contentBasePublicPath, urlPath, function (error, content, filePath) {
if (!error) {
return found(response, filePath, content)
}
Expand All @@ -53,7 +54,7 @@ function serve (options = { contentBase: '' }) {
}
if (options.historyApiFallback) {
const fallbackPath = typeof options.historyApiFallback === 'string' ? options.historyApiFallback : '/index.html'
readFileFromContentBase(options.contentBase, fallbackPath, function (error, content, filePath) {
readFileFromContentBase(options.contentBase, options.contentBasePublicPath, fallbackPath, function (error, content, filePath) {
if (error) {
notFound(response, filePath)
} else {
Expand Down Expand Up @@ -120,18 +121,27 @@ function serve (options = { contentBase: '' }) {
}
}

function readFileFromContentBase (contentBase, urlPath, callback) {
let filePath = resolve(contentBase[0] || '.', '.' + urlPath)
function readFileFromContentBase (contentBase, contentBasePublicPath, urlPath, callback) {
let internalUrlPath = urlPath
if (contentBasePublicPath) {
if (urlPath === contentBasePublicPath) {
internalUrlPath = '/'
} else if (urlPath.startsWith(contentBasePublicPath)) {
internalUrlPath = internalUrlPath.replace(contentBasePublicPath, '')
}
}

let filePath = resolve(contentBase[0] || '.', '.' + internalUrlPath)

// Load index.html in directories
if (urlPath.endsWith('/')) {
if (internalUrlPath.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)
readFileFromContentBase(contentBase.slice(1), contentBasePublicPath, urlPath, callback)
} else {
// We know enough
callback(error, content, filePath)
Expand Down
28 changes: 28 additions & 0 deletions test/frames.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,32 @@
<td><iframe src="./"></iframe></td>
<td><iframe src="error.html"></iframe></td>
</tr>
<tr>
<td>/foo/bar</td>
<td><iframe src="/foo/bar"></iframe></td>
<td><iframe src="/foo/bar/"></iframe></td>
<td><iframe src="/foo/bar/error.html"></iframe></td>
</tr>
<tr>
<td>./base1</td>
<td><iframe src="./base1"></iframe></td>
<td><iframe src="./base1/"></iframe></td>
</tr>
<tr>
<td>/foo/bar/base1</td>
<td><iframe src="/foo/bar/base1"></iframe></td>
<td><iframe src="/foo/bar/base1/"></iframe></td>
</tr>
<tr>
<td>./base2</td>
<td><iframe src="./base2"></iframe></td>
<td><iframe src="./base2/"></iframe></td>
</tr>
<tr>
<td>/foo/bar/base2</td>
<td><iframe src="/foo/bar/base2"></iframe></td>
<td><iframe src="/foo/bar/base2/"></iframe></td>
</tr>
<tr>
<th></th>
<th>./</th>
Expand All @@ -48,10 +64,22 @@
<td><iframe src="./base1/test1.html"></iframe></td>
<td><iframe src="./base2/test1.html"></iframe></td>
</tr>
<tr>
<td>/foo/bar/test1.html</td>
<td><iframe src="/foo/bar/test1.html"></iframe></td>
<td><iframe src="/foo/bar//base1/test1.html"></iframe></td>
<td><iframe src="/foo/bar//base2/test1.html"></iframe></td>
</tr>
<tr>
<td>test2.html</td>
<td><iframe src="./test2.html"></iframe></td>
<td><iframe src="./base1/test2.html"></iframe></td>
<td><iframe src="./base2/test2.html"></iframe></td>
</tr>
<tr>
<td>/foo/bar/test2.html</td>
<td><iframe src="/foo/bar//test2.html"></iframe></td>
<td><iframe src="/foo/bar//base1/test2.html"></iframe></td>
<td><iframe src="/foo/bar//base2/test2.html"></iframe></td>
</tr>
</table>
1 change: 1 addition & 0 deletions test/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default {
openPage: '/frames.html',
historyApiFallback: '/fallback.html',
contentBase: ['.', 'base1', 'base2'],
contentBasePublicPath: '/foo/bar',
onListening: testOnListening(),
})
]
Expand Down