Skip to content
Open
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/poor-carpets-take.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sv': patch
---

feat(demo): include file extensions for local imports
3 changes: 3 additions & 0 deletions packages/create/scripts/build-templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ async function convert_typescript(content) {
// sucrase leaves invalid class fields intact
code = code.replace(/^\s*[a-z]+;$/gm, '');

// Replace "local import" that ends with ".ts" to ".js"
code = code.replace(/import (.+?) from ['"](.+?)\.ts['"]/g, 'import $1 from "$2.js"');

// Prettier strips 'unnecessary' parens from .ts files, we need to hack them back in
code = code.replace(/(\/\*\* @type.+? \*\/) (.+?) \/\*\*\*\//g, '$1($2)');

Expand Down
1 change: 1 addition & 0 deletions packages/create/shared/+library+typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowImportingTsExtensions": true,
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
Expand Down
1 change: 1 addition & 0 deletions packages/create/shared/+typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowImportingTsExtensions": true,
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fail } from '@sveltejs/kit';
import { Game } from './game';
import { Game } from './game.ts';
import type { PageServerLoad, Actions } from './$types';

/** @satisfies {import('./$types').PageServerLoad} */
Expand Down
2 changes: 1 addition & 1 deletion packages/create/templates/demo/src/routes/sverdle/game.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { words, allowed } from './words.server';
import { words, allowed } from './words.server.ts';

export class Game {
index: number;
Expand Down
5 changes: 4 additions & 1 deletion packages/create/templates/demo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "./.svelte-kit/tsconfig.json"
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowImportingTsExtensions": true
}
}
13 changes: 12 additions & 1 deletion packages/create/test/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path';
import { promisify } from 'node:util';
import { fileURLToPath } from 'node:url';
import { exec, type PromiseWithChild } from 'node:child_process';
import { beforeAll, describe, test } from 'vitest';
import { beforeAll, describe, expect, test } from 'vitest';
import { create, type LanguageType, type TemplateType } from '../index.ts';

// Resolve the given path relative to the current file
Expand Down Expand Up @@ -57,6 +57,17 @@ for (const template of templates) {
tests.push([`${template}-${types}`, () => exec_async(`pnpm ${script}`, { cwd })]);
script_test_map.set(script, tests);
}

if (template === 'demo') {
describe(`local import with extentions`, () => {
test(`${template}-${types}`, () => {
const ending = types === 'typescript' ? 'ts' : 'js';
const gameFile = path.join(cwd, `src/routes/sverdle/game.${ending}`);
const gameFileContent = fs.readFileSync(gameFile, 'utf-8');
expect(gameFileContent).toContain(`./words.server.${ending}`);
});
});
}
}
}

Expand Down
Loading