Skip to content

Commit 5fee6a0

Browse files
authored
feat(demo): include file extensions (#757)
* step 1 * update build script * adding a test * changeset
1 parent 4718465 commit 5fee6a0

File tree

8 files changed

+28
-4
lines changed

8 files changed

+28
-4
lines changed

.changeset/poor-carpets-take.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+
feat(demo): include file extensions for local imports

packages/create/scripts/build-templates.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ async function convert_typescript(content) {
2121
// sucrase leaves invalid class fields intact
2222
code = code.replace(/^\s*[a-z]+;$/gm, '');
2323

24+
// Replace "local import" that ends with ".ts" to ".js"
25+
code = code.replace(/import (.+?) from ['"](.+?)\.ts['"]/g, 'import $1 from "$2.js"');
26+
2427
// Prettier strips 'unnecessary' parens from .ts files, we need to hack them back in
2528
code = code.replace(/(\/\*\* @type.+? \*\/) (.+?) \/\*\*\*\//g, '$1($2)');
2629

packages/create/shared/+library+typescript/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "./.svelte-kit/tsconfig.json",
33
"compilerOptions": {
4+
"allowImportingTsExtensions": true,
45
"allowJs": true,
56
"checkJs": true,
67
"esModuleInterop": true,

packages/create/shared/+typescript/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "./.svelte-kit/tsconfig.json",
33
"compilerOptions": {
4+
"allowImportingTsExtensions": true,
45
"allowJs": true,
56
"checkJs": true,
67
"esModuleInterop": true,

packages/create/templates/demo/src/routes/sverdle/+page.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { fail } from '@sveltejs/kit';
2-
import { Game } from './game';
2+
import { Game } from './game.ts';
33
import type { PageServerLoad, Actions } from './$types';
44

55
/** @satisfies {import('./$types').PageServerLoad} */

packages/create/templates/demo/src/routes/sverdle/game.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { words, allowed } from './words.server';
1+
import { words, allowed } from './words.server.ts';
22

33
export class Game {
44
index: number;
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"extends": "./.svelte-kit/tsconfig.json"
2+
"extends": "./.svelte-kit/tsconfig.json",
3+
"compilerOptions": {
4+
"allowImportingTsExtensions": true
5+
}
36
}

packages/create/test/check.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'node:path';
33
import { promisify } from 'node:util';
44
import { fileURLToPath } from 'node:url';
55
import { exec, type PromiseWithChild } from 'node:child_process';
6-
import { beforeAll, describe, test } from 'vitest';
6+
import { beforeAll, describe, expect, test } from 'vitest';
77
import { create, type LanguageType, type TemplateType } from '../index.ts';
88

99
// Resolve the given path relative to the current file
@@ -57,6 +57,17 @@ for (const template of templates) {
5757
tests.push([`${template}-${types}`, () => exec_async(`pnpm ${script}`, { cwd })]);
5858
script_test_map.set(script, tests);
5959
}
60+
61+
if (template === 'demo') {
62+
describe(`local import with extentions`, () => {
63+
test(`${template}-${types}`, () => {
64+
const ending = types === 'typescript' ? 'ts' : 'js';
65+
const gameFile = path.join(cwd, `src/routes/sverdle/game.${ending}`);
66+
const gameFileContent = fs.readFileSync(gameFile, 'utf-8');
67+
expect(gameFileContent).toContain(`./words.server.${ending}`);
68+
});
69+
});
70+
}
6071
}
6172
}
6273

0 commit comments

Comments
 (0)