From fcbd4406c1b0a4d5f04ef780083892169f962a36 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Tue, 24 Oct 2023 08:06:08 +0900 Subject: [PATCH] feat(dev-server): add files to exclude (#13) * feat(dev-server): add files to exclude * update readme --- packages/dev-server/README.md | 13 ++++++++++--- packages/dev-server/src/dev-server.ts | 9 ++++++++- packages/dev-server/test/e2e.test.ts | 6 ++++++ packages/dev-server/test/mock/worker.ts | 6 ++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/packages/dev-server/README.md b/packages/dev-server/README.md index 0d68dc3..22b5143 100644 --- a/packages/dev-server/README.md +++ b/packages/dev-server/README.md @@ -123,7 +123,14 @@ Default values: export const defaultOptions: Required> = { entry: './src/index.ts', injectClientScript: true, - exclude: ['.*.ts', '.*.tsx', '/@.+', '/node_modules/.*'], + exclude: [ + /.*\.ts$/, + /.*\.tsx$/, + /^\/@.+$/, + /^\/favicon\.ico$/, + /^\/static\/.+/, + /^\/node_modules\/.*/, + ], } ``` @@ -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' @@ -144,7 +151,7 @@ import { defineConfig } from 'vite' export default defineConfig({ plugins: [ devServer({ - exclude: ['^/static/.*', ...defaultOptions.exclude], + exclude: ['/assets/.*', ...defaultOptions.exclude], }), ], }) diff --git a/packages/dev-server/src/dev-server.ts b/packages/dev-server/src/dev-server.ts index 0fadf3b..6381fd7 100644 --- a/packages/dev-server/src/dev-server.ts +++ b/packages/dev-server/src/dev-server.ts @@ -23,7 +23,14 @@ export type DevServerOptions = { export const defaultOptions: Required> = { entry: './src/index.ts', injectClientScript: true, - exclude: [/.*\.ts$/, /.*\.tsx$/, /^\/@.+$/, /^\/node_modules\/.*/], + exclude: [ + /.*\.ts$/, + /.*\.tsx$/, + /^\/@.+$/, + /^\/favicon\.ico$/, + /^\/static\/.+/, + /^\/node_modules\/.*/, + ], } interface ExecutionContext { diff --git a/packages/dev-server/test/e2e.test.ts b/packages/dev-server/test/e2e.test.ts index cf761f8..7c48c6c 100644 --- a/packages/dev-server/test/e2e.test.ts +++ b/packages/dev-server/test/e2e.test.ts @@ -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) }) diff --git a/packages/dev-server/test/mock/worker.ts b/packages/dev-server/test/mock/worker.ts index ff2fb3f..df36ef8 100644 --- a/packages/dev-server/test/mock/worker.ts +++ b/packages/dev-server/test/mock/worker.ts @@ -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