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

(fix) add .js to $types imports to support node16/nodenext resolution #1967

Merged
merged 1 commit into from
Mar 27, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/svelte2tsx/src/helpers/sveltekit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function upserKitRouteFile(
if (load?.type === 'function' && load.node.parameters.length === 1 && !load.hasTypeDefinition) {
const pos = load.node.parameters[0].getEnd();
const inserted = surround(
`: import('./$types').${basename.includes('layout') ? 'Layout' : 'Page'}${
`: import('./$types.js').${basename.includes('layout') ? 'Layout' : 'Page'}${
basename.includes('server') ? 'Server' : ''
}LoadEvent`
);
Expand All @@ -175,7 +175,7 @@ function upserKitRouteFile(
const actions = exports.get('actions');
if (actions?.type === 'var' && !actions.hasTypeDefinition && actions.node.initializer) {
const pos = actions.node.initializer.getEnd();
const inserted = surround(` satisfies import('./$types').Actions`);
const inserted = surround(` satisfies import('./$types.js').Actions`);
insert(pos, inserted);
}

Expand All @@ -192,7 +192,7 @@ function upserKitRouteFile(
surround,
insert,
name,
`import('./$types').RequestEvent`,
`import('./$types.js').RequestEvent`,
`Response | Promise<Response>`
);
};
Expand Down
5 changes: 3 additions & 2 deletions packages/svelte2tsx/src/svelte2tsx/nodes/ExportedNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export class ExportedNames {
internalHelpers.isKitRouteFile(this.basename) &&
n.name.getText() === 'snapshot';
// TS types are not allowed in JS files, but TS will still pick it up and the ignore comment will filter out the error
const kitType = isKitExport && !type ? `: import('./$types').Snapshot` : '';
const kitType =
isKitExport && !type ? `: import('./$types.js').Snapshot` : '';
const nameEnd = n.name.end + this.astOffset;
if (kitType) {
preprendStr(this.str, nameEnd, surroundWithIgnoreComments(kitType));
Expand Down Expand Up @@ -132,7 +133,7 @@ export class ExportedNames {
// TS types are not allowed in JS files, but TS will still pick it up and the ignore comment will filter out the error
const kitType =
isKitExport && !type
? `: import('./$types').${
? `: import('./$types.js').${
name === 'data'
? this.basename.includes('layout')
? 'LayoutData'
Expand Down
6 changes: 3 additions & 3 deletions packages/svelte2tsx/test/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Internal Helpers - upsertKitFile', () => {
upsert(
'+page.ts',
`export function load(e) { return e; }`,
`export function load(e: import('./$types').PageLoadEvent) { return e; }`
`export function load(e: import('./$types.js').PageLoadEvent) { return e; }`
);
});

Expand All @@ -38,15 +38,15 @@ describe('Internal Helpers - upsertKitFile', () => {
upsert(
'+server.ts',
`export async function GET(e) {}`,
`export async function GET(e: import('./$types').RequestEvent) : Response | Promise<Response> {}`
`export async function GET(e: import('./$types.js').RequestEvent) : Response | Promise<Response> {}`
);
});

it('upserts load const with paranthesis', () => {
upsert(
'+page.ts',
`export const load = (async (e) => {});`,
`export const load = (async (e: import('./$types').PageLoadEvent) => {});`
`export const load = (async (e: import('./$types.js').PageLoadEvent) => {});`
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
///<reference types="svelte" />
;function render() {

let data/*Ωignore_startΩ*/: import('./$types').PageData;data = __sveltets_2_any(data);/*Ωignore_endΩ*/;
let form/*Ωignore_startΩ*/: import('./$types').ActionData;form = __sveltets_2_any(form);/*Ωignore_endΩ*/;
const snapshot/*Ωignore_startΩ*/: import('./$types').Snapshot/*Ωignore_endΩ*/ = {};
let data/*Ωignore_startΩ*/: import('./$types.js').PageData;data = __sveltets_2_any(data);/*Ωignore_endΩ*/;
let form/*Ωignore_startΩ*/: import('./$types.js').ActionData;form = __sveltets_2_any(form);/*Ωignore_endΩ*/;
const snapshot/*Ωignore_startΩ*/: import('./$types.js').Snapshot/*Ωignore_endΩ*/ = {};

let nope/*Ωignore_startΩ*/;nope = __sveltets_2_any(nope);/*Ωignore_endΩ*/;
let form/*Ωignore_startΩ*/: import('./$types').ActionData/*Ωignore_endΩ*/ = {}
let form/*Ωignore_startΩ*/: import('./$types.js').ActionData/*Ωignore_endΩ*/ = {}
let data: number/*Ωignore_startΩ*/;data = __sveltets_2_any(data);/*Ωignore_endΩ*/;
;
async () => {};
Expand Down