Skip to content

Commit

Permalink
feat(dev-server): add files to exclude (#13)
Browse files Browse the repository at this point in the history
* feat(dev-server): add files to exclude

* update readme
  • Loading branch information
yusukebe authored Oct 23, 2023
1 parent 52fb46f commit fcbd440
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
13 changes: 10 additions & 3 deletions packages/dev-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,14 @@ Default values:
export const defaultOptions: Required<Omit<DevServerOptions, 'cf'>> = {
entry: './src/index.ts',
injectClientScript: true,
exclude: ['.*.ts', '.*.tsx', '/@.+', '/node_modules/.*'],
exclude: [
/.*\.ts$/,
/.*\.tsx$/,
/^\/@.+$/,
/^\/favicon\.ico$/,
/^\/static\/.+/,
/^\/node_modules\/.*/,
],
}
```

Expand All @@ -135,7 +142,7 @@ If it's `true` and the response content-type is "HTML", inject the script that e

The paths which are not served by the dev-server.

If you have static files in `public/static/*` and want to return them, exclude `/static/*` as follows:
If you have static files in `public/assets/*` and want to return them, exclude `/assets/*` as follows:

```ts
import devServer, { defaultOptions } from '@hono/vite-dev-server'
Expand All @@ -144,7 +151,7 @@ import { defineConfig } from 'vite'
export default defineConfig({
plugins: [
devServer({
exclude: ['^/static/.*', ...defaultOptions.exclude],
exclude: ['/assets/.*', ...defaultOptions.exclude],
}),
],
})
Expand Down
9 changes: 8 additions & 1 deletion packages/dev-server/src/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ export type DevServerOptions = {
export const defaultOptions: Required<Omit<DevServerOptions, 'cf'>> = {
entry: './src/index.ts',
injectClientScript: true,
exclude: [/.*\.ts$/, /.*\.tsx$/, /^\/@.+$/, /^\/node_modules\/.*/],
exclude: [
/.*\.ts$/,
/.*\.tsx$/,
/^\/@.+$/,
/^\/favicon\.ico$/,
/^\/static\/.+/,
/^\/node_modules\/.*/,
],
}

interface ExecutionContext {
Expand Down
6 changes: 6 additions & 0 deletions packages/dev-server/test/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ test('Should exclude the file specified in the config file', async ({ page }) =>

response = await page.goto('/app/foo')
expect(response?.status()).toBe(404)

response = await page.goto('/favicon.ico')
expect(response?.status()).toBe(404)

response = await page.goto('/static/foo.png')
expect(response?.status()).toBe(404)
})
6 changes: 6 additions & 0 deletions packages/dev-server/test/mock/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@ app.get('/app/foo', (c) => {
app.get('/ends-in-ts', (c) => {
return c.text('this should not be excluded')
})
app.get('/favicon.ico', (c) => {
return c.text('a good favicon')
})
app.get('/static/foo.png', (c) => {
return c.text('a good image')
})

export default app

0 comments on commit fcbd440

Please sign in to comment.