Skip to content

Commit 84a39ba

Browse files
authored
[fix] make generated type import path end in .js (#5907)
Fixes #5899
1 parent 9b16eb6 commit 84a39ba

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

.changeset/twelve-stingrays-cry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/kit": patch
3+
---
4+
5+
make generated type import path ends in `.js`

packages/kit/src/core/sync/write_types.js

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -390,18 +390,13 @@ function process_node(ts, node, outdir, params, groups) {
390390
const types = [];
391391
for (const method of ['POST', 'PUT', 'PATCH']) {
392392
if (proxy.exports.includes(method)) {
393-
if (proxy.modified) {
394-
types.push(`Kit.AwaitedErrors<typeof import('./proxy${basename}').${method}>`);
395-
} else {
396-
// If the file wasn't tweaked, we can use the return type of the original file.
397-
// The advantage is that type updates are reflected without saving.
398-
types.push(
399-
`Kit.AwaitedErrors<typeof import("${path_to_original(
400-
outdir,
401-
node.server
402-
)}").${method}>`
403-
);
404-
}
393+
// If the file wasn't tweaked, we can use the return type of the original file.
394+
// The advantage is that type updates are reflected without saving.
395+
const from = proxy.modified
396+
? `./proxy${replace_ext_with_js(basename)}`
397+
: path_to_original(outdir, node.server);
398+
399+
types.push(`Kit.AwaitedErrors<typeof import('${from}').${method}>`);
405400
}
406401
}
407402
errors = types.length ? types.join(' | ') : 'null';
@@ -494,18 +489,17 @@ function process_node(ts, node, outdir, params, groups) {
494489
* @param {string} file_path
495490
*/
496491
function path_to_original(outdir, file_path) {
492+
return posixify(path.relative(outdir, path.join(cwd, replace_ext_with_js(file_path))));
493+
}
494+
495+
/**
496+
* @param {string} file_path
497+
*/
498+
function replace_ext_with_js(file_path) {
499+
// Another extension than `.js` (or nothing, but that fails with node16 moduleResolution)
500+
// will result in TS failing to lookup the file
497501
const ext = path.extname(file_path);
498-
return posixify(
499-
path.relative(
500-
outdir,
501-
path.join(
502-
cwd,
503-
// Another extension than `.js` (or nothing, but that fails with node16 moduleResolution)
504-
// will result in TS failing to lookup the file
505-
file_path.slice(0, -ext.length) + '.js'
506-
)
507-
)
508-
);
502+
return file_path.slice(0, -ext.length) + '.js';
509503
}
510504

511505
/**
@@ -722,10 +716,6 @@ export function find_nearest_layout(routes_dir, nodes, start_idx) {
722716
}
723717

724718
// matching parent layout found
725-
// let import_path = posixify(path.relative(path.dirname(start_file), common_path + '/$types.js'));
726-
// if (!import_path.startsWith('.')) {
727-
// import_path = './' + import_path;
728-
// }
729719
let folder_depth_diff =
730720
posixify(path.relative(path.dirname(start_file), common_path + '/$types.js')).split('/')
731721
.length - 1;

0 commit comments

Comments
 (0)