Skip to content

Commit bc8e057

Browse files
authored
Add TypeScript types to the viteFastfiy plugin and utils exports (#352)
1 parent 651d101 commit bc8e057

File tree

7 files changed

+113
-65
lines changed

7 files changed

+113
-65
lines changed

.changeset/dirty-feet-wash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fastify/vite": minor
3+
---
4+
5+
Add TypeScript types to the `viteFastify` plugin and utils exports. #352

packages/fastify-vite/package.json

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
"utils.js",
2626
"mode/development.js",
2727
"mode/production.js",
28-
"types/index.d.ts"
28+
"types/index.d.ts",
29+
"types/plugin.d.ts",
30+
"types/utils.d.ts"
2931
],
3032
"main": "index.js",
3133
"types": "./types/index.d.ts",
@@ -36,15 +38,21 @@
3638
"import": "./index.js",
3739
"require": "./index.js"
3840
},
39-
"./utils": "./utils.js",
40-
"./plugin": "./plugin.mjs"
41+
"./utils": {
42+
"types": "./types/utils.d.ts",
43+
"import": "./utils.js",
44+
"require": "./utils.js"
45+
},
46+
"./plugin": {
47+
"types": "./types/plugin.d.ts",
48+
"import": "./plugin.mjs"
49+
}
4150
},
4251
"publishConfig": {
4352
"access": "public"
4453
},
4554
"scripts": {
46-
"test": "vitest run --no-file-parallelism",
47-
"test:type": "vitest --ui --typecheck"
55+
"test": "vitest run --no-file-parallelism && vitest --typecheck.only --run"
4856
},
4957
"dependencies": {
5058
"@fastify/deepmerge": "^3.1.0",
@@ -58,7 +66,6 @@
5866
},
5967
"devDependencies": {
6068
"@types/node": "^22.19.3",
61-
"@vitest/ui": "4.0.16",
6269
"fastify": "catalog:",
6370
"typescript": "catalog:",
6471
"vitest": "catalog:"

packages/fastify-vite/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"files": ["**/*.ts"],
32
"compilerOptions": {
43
"target": "esnext",
54
"lib": ["ES2020"],
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { Plugin } from 'vite'
2+
3+
export interface ViteFastifyPluginOptions {
4+
/**
5+
* Enable SPA mode (no SSR environment)
6+
*/
7+
spa?: boolean
8+
/**
9+
* Path to the client module entry point
10+
*/
11+
clientModule?: string
12+
/**
13+
* Use paths relative to the application root directory
14+
*/
15+
useRelativePaths?: boolean
16+
}
17+
18+
/**
19+
* Vite plugin for Fastify integration.
20+
* Configures Vite environments for client and SSR builds.
21+
*/
22+
export declare function viteFastify(options?: ViteFastifyPluginOptions): Plugin
23+
24+
/**
25+
* Finds the common path prefix among an array of paths.
26+
*/
27+
export declare function findCommonPath(paths: string[]): string
28+
29+
export default viteFastify
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { Plugin } from 'vite'
2+
import { describe, expectTypeOf, it } from 'vitest'
3+
import viteFastify, { findCommonPath, type ViteFastifyPluginOptions } from './plugin'
4+
5+
describe('plugin types', () => {
6+
it('viteFastify returns a Vite Plugin', () => {
7+
expectTypeOf(viteFastify()).toExtend<Plugin>()
8+
expectTypeOf(viteFastify({})).toExtend<Plugin>()
9+
expectTypeOf(viteFastify({ spa: true })).toExtend<Plugin>()
10+
expectTypeOf(viteFastify({ clientModule: './client.js' })).toExtend<Plugin>()
11+
expectTypeOf(viteFastify({ useRelativePaths: true })).toExtend<Plugin>()
12+
expectTypeOf(
13+
viteFastify({ spa: false, clientModule: './client.js', useRelativePaths: false }),
14+
).toExtend<Plugin>()
15+
})
16+
17+
it('findCommonPath accepts string array and returns string', () => {
18+
expectTypeOf(findCommonPath).parameter(0).toEqualTypeOf<string[]>()
19+
expectTypeOf(findCommonPath(['a', 'b'])).toEqualTypeOf<string>()
20+
})
21+
22+
it('ViteFastifyPluginOptions has correct shape', () => {
23+
const options: ViteFastifyPluginOptions = {}
24+
expectTypeOf(options.spa).toEqualTypeOf<boolean | undefined>()
25+
expectTypeOf(options.clientModule).toEqualTypeOf<string | undefined>()
26+
expectTypeOf(options.useRelativePaths).toEqualTypeOf<boolean | undefined>()
27+
})
28+
})
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export interface RendererInfo {
2+
path: string
3+
}
4+
5+
export interface SetupOptions {
6+
root: string
7+
renderer: RendererInfo
8+
}
9+
10+
/**
11+
* Creates an HTML template function from source HTML.
12+
* The returned function accepts an object with template variables
13+
* and returns the compiled HTML string.
14+
*/
15+
export declare function createHtmlTemplateFunction(
16+
source: string,
17+
): Promise<(context?: Record<string, unknown>) => string>
18+
19+
/**
20+
* Ensures the Vite config file exists, copying from renderer blueprint if needed.
21+
* @returns The path to the config file
22+
*/
23+
export declare function ensureConfigFile(base: string, options: SetupOptions): Promise<string>
24+
25+
/**
26+
* Ejects the renderer blueprint files to the project root.
27+
*/
28+
export declare function ejectBlueprint(base: string, options: SetupOptions): Promise<void>

pnpm-lock.yaml

Lines changed: 10 additions & 58 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)