Skip to content
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

make generated type import path ends in .js #5907

Merged
merged 4 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/twelve-stingrays-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

make generated type import path ends in `.js`
44 changes: 17 additions & 27 deletions packages/kit/src/core/sync/write_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,18 +390,13 @@ function process_node(ts, node, outdir, params, groups) {
const types = [];
for (const method of ['POST', 'PUT', 'PATCH']) {
if (proxy.exports.includes(method)) {
if (proxy.modified) {
types.push(`Kit.AwaitedErrors<typeof import('./proxy${basename}').${method}>`);
} else {
// If the file wasn't tweaked, we can use the return type of the original file.
// The advantage is that type updates are reflected without saving.
types.push(
`Kit.AwaitedErrors<typeof import("${path_to_original(
outdir,
node.server
)}").${method}>`
);
}
// If the file wasn't tweaked, we can use the return type of the original file.
// The advantage is that type updates are reflected without saving.
const from = proxy.modified
? `./proxy${replace_ext_with_js(basename)}`
: path_to_original(outdir, node.server);

types.push(`Kit.AwaitedErrors<typeof import('${from}').${method}>`);
}
}
errors = types.length ? types.join(' | ') : 'null';
Expand Down Expand Up @@ -494,18 +489,17 @@ function process_node(ts, node, outdir, params, groups) {
* @param {string} file_path
*/
function path_to_original(outdir, file_path) {
return posixify(path.relative(outdir, path.join(cwd, replace_ext_with_js(file_path))));
}

/**
* @param {string} file_path
*/
function replace_ext_with_js(file_path) {
// Another extension than `.js` (or nothing, but that fails with node16 moduleResolution)
// will result in TS failing to lookup the file
const ext = path.extname(file_path);
return posixify(
path.relative(
outdir,
path.join(
cwd,
// Another extension than `.js` (or nothing, but that fails with node16 moduleResolution)
// will result in TS failing to lookup the file
file_path.slice(0, -ext.length) + '.js'
)
)
);
return file_path.slice(0, -ext.length) + '.js';
}

/**
Expand Down Expand Up @@ -722,10 +716,6 @@ export function find_nearest_layout(routes_dir, nodes, start_idx) {
}

// matching parent layout found
// let import_path = posixify(path.relative(path.dirname(start_file), common_path + '/$types.js'));
// if (!import_path.startsWith('.')) {
// import_path = './' + import_path;
// }
let folder_depth_diff =
posixify(path.relative(path.dirname(start_file), common_path + '/$types.js')).split('/')
.length - 1;
Expand Down