Skip to content

Commit ba509dc

Browse files
committed
fix paraglide reroute type
added `Reroute` type for the reroute hooks.[ts|js] Signed-off-by: Kiizuha Kanazawa <kiizuha@gnuweeb.org>
1 parent 8f3c27c commit ba509dc

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

.changeset/brave-months-greet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv': patch
3+
---
4+
5+
paraglide: add a type annotation for the reroute hooks.

packages/sv/src/addons/paraglide.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ export default defineAddon({
6767
const vitePluginName = 'paraglideVitePlugin';
6868
js.imports.addNamed(ast, { imports: [vitePluginName], from: '@inlang/paraglide-js' });
6969
js.vite.addPlugin(ast, {
70-
code: `${vitePluginName}({
71-
project: './project.inlang',
72-
outdir: './${paraglideOutDir}'
70+
code: `${vitePluginName}({
71+
project: './project.inlang',
72+
outdir: './${paraglideOutDir}'
7373
})`
7474
});
7575

@@ -78,7 +78,8 @@ export default defineAddon({
7878

7979
// reroute hook
8080
sv.file(`src/hooks.${language}`, (content) => {
81-
const { ast, generateCode } = parse.script(content);
81+
const { ast, generateCode, comments } = parse.script(content);
82+
8283
js.imports.addNamed(ast, {
8384
from: '$lib/paraglide/runtime',
8485
imports: ['deLocalizeUrl']
@@ -87,9 +88,10 @@ export default defineAddon({
8788
const expression = js.common.parseExpression(
8889
'(request) => deLocalizeUrl(request.url).pathname'
8990
);
91+
9092
const rerouteIdentifier = js.variables.declaration(ast, {
9193
kind: 'const',
92-
name: 'reroute',
94+
name: `reroute${language === 'ts' ? ': Reroute' : ''}`,
9395
value: expression
9496
});
9597

@@ -101,6 +103,18 @@ export default defineAddon({
101103
log.warn('Adding the reroute hook automatically failed. Add it manually');
102104
}
103105

106+
if (language === 'ts') {
107+
js.imports.addNamed(ast, {
108+
from: '@sveltejs/kit',
109+
imports: ['Reroute'],
110+
isType: true
111+
});
112+
} else {
113+
js.common.addJsDocTypeComment(existingExport, comments, {
114+
type: "import('@sveltejs/kit').Reroute"
115+
});
116+
}
117+
104118
return generateCode();
105119
});
106120

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Reroute } from '@sveltejs/kit';
12
import { deLocalizeUrl } from '$lib/paraglide/runtime';
23

3-
export const reroute = (request) => deLocalizeUrl(request.url).pathname;
4+
export const reroute: Reroute = (request) => deLocalizeUrl(request.url).pathname;

0 commit comments

Comments
 (0)