Skip to content

Commit 67c2acf

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 67c2acf

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

.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: 24 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,15 @@ export default defineAddon({
8788
const expression = js.common.parseExpression(
8889
'(request) => deLocalizeUrl(request.url).pathname'
8990
);
91+
92+
let varName = 'reroute';
93+
if (language === 'ts') {
94+
varName += ': Reroute';
95+
}
96+
9097
const rerouteIdentifier = js.variables.declaration(ast, {
9198
kind: 'const',
92-
name: 'reroute',
99+
name: varName,
93100
value: expression
94101
});
95102

@@ -101,6 +108,18 @@ export default defineAddon({
101108
log.warn('Adding the reroute hook automatically failed. Add it manually');
102109
}
103110

111+
if (language === 'ts') {
112+
js.imports.addNamed(ast, {
113+
from: '@sveltejs/kit',
114+
imports: ['Reroute'],
115+
isType: true
116+
});
117+
} else {
118+
js.common.addJsDocTypeComment(existingExport, comments, {
119+
type: "import('@sveltejs/kit').Reroute"
120+
});
121+
}
122+
104123
return generateCode();
105124
});
106125

0 commit comments

Comments
 (0)