-
Notifications
You must be signed in to change notification settings - Fork 0
fix: resolve TypeScript errors in Vite config #515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
78f7ac7
ce364ba
aff4790
025964f
00ec6d8
88be5ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { defineConfig, Plugin, version as viteVersion } from "vite"; | ||
| import type { PluginContext, ViteDevServer } from "vite"; | ||
| import type { ViteDevServer } from "vite"; | ||
| import type { PluginContext } from "rollup"; | ||
| import react from "@vitejs/plugin-react"; | ||
| import { simpleGit } from "simple-git"; | ||
| import os from "os"; | ||
|
|
@@ -77,7 +78,7 @@ const getGitInfo = async (): Promise<{ | |
| // Fallback to environment variables or defaults | ||
| return { | ||
| sha: getEnv("VITE_GIT_SHA", "unknown"), | ||
| isClean: getEnv("VITE_GIT_CLEAN", "unknown"), | ||
| isClean: getEnv("VITE_GIT_CLEAN", "false") === "true", | ||
| branch: getEnv("VITE_GIT_BRANCH", "unknown"), | ||
| }; | ||
| } | ||
|
|
@@ -128,8 +129,8 @@ function buildInfoPlugin(): Plugin { | |
| function dataValidationPlugin(dir: string = "src/data") { | ||
| return { | ||
| name: "data-validation", | ||
| apply: "build", | ||
| enforce: "pre", | ||
| apply: "build" as const, | ||
| enforce: "pre" as const, | ||
| async buildStart(this: PluginContext) { | ||
| const names = (await fs.readdir(dir)).filter((f) => f.endsWith(".json")); | ||
| const entries: FileEntry[] = []; | ||
|
|
@@ -184,12 +185,52 @@ function dataValidationPlugin(dir: string = "src/data") { | |
| }; | ||
| } | ||
|
|
||
| // Create a new plugin for serving schema files | ||
jdhoffa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Used to access the schema files with the dev server | ||
| function schemaServePlugin(): Plugin { | ||
| return { | ||
| name: "schema-serve", | ||
|
||
| configureServer(server: ViteDevServer) { | ||
| server.middlewares.use( | ||
| "/schema", | ||
| ( | ||
| req: IncomingMessage, | ||
| res: ServerResponse, | ||
| next: (err?: unknown) => void, | ||
| ): void => { | ||
| try { | ||
| const baseDir = resolve("src/schema"); | ||
| const relPath = decodeURIComponent( | ||
| (req.url || "").replace(/^\/schema\/?/, ""), | ||
| ); | ||
| if (!relPath) return next(); // no file requested, let Vite handle | ||
| const absPath = resolve(baseDir, relPath); | ||
| // prevent path traversal | ||
| if (!absPath.startsWith(baseDir)) return next(); | ||
|
||
| // only serve JSON files | ||
| if (!absPath.endsWith(".json")) return next(); | ||
| readFile(absPath, "utf8") | ||
| .then((json) => { | ||
| res.setHeader("Content-Type", "application/json"); | ||
| res.end(json); | ||
| }) | ||
| .catch(next); | ||
| } catch (err) { | ||
| next(err); | ||
| } | ||
| }, | ||
| ); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| // https://vitejs.dev/config/ | ||
| export default defineConfig({ | ||
| plugins: [ | ||
| react(), | ||
| buildInfoPlugin(), | ||
| dataValidationPlugin("src/data"), | ||
| schemaServePlugin(), | ||
| viteStaticCopy({ | ||
| // Copy the entire schema dir to /schema in the built output | ||
| targets: [{ src: "src/schema/**/*", dest: "schema" }], | ||
|
|
@@ -199,36 +240,4 @@ export default defineConfig({ | |
| open: true, | ||
| port: 3000, | ||
| }, | ||
| configureServer(server: ViteDevServer) { | ||
| // Serve any file under src/schema at /schema/* during dev | ||
| server.middlewares.use( | ||
| "/schema", | ||
| ( | ||
| req: IncomingMessage, | ||
| res: ServerResponse, | ||
| next: (err?: unknown) => void, | ||
| ): void => { | ||
| try { | ||
| const baseDir = resolve("src/schema"); | ||
| const relPath = decodeURIComponent( | ||
| (req.url || "").replace(/^\/schema\/?/, ""), | ||
| ); | ||
| if (!relPath) return next(); // no file requested, let Vite handle | ||
| const absPath = resolve(baseDir, relPath); | ||
| // prevent path traversal | ||
| if (!absPath.startsWith(baseDir)) return next(); | ||
| // only serve JSON files | ||
| if (!absPath.endsWith(".json")) return next(); | ||
| readFile(absPath, "utf8") | ||
| .then((json) => { | ||
| res.setHeader("Content-Type", "application/json"); | ||
| res.end(json); | ||
| }) | ||
| .catch(next); | ||
| } catch (err) { | ||
| next(err); | ||
| } | ||
| }, | ||
| ); | ||
| }, | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.