Skip to content

Commit 7fdeb3c

Browse files
committed
js -> ts
1 parent b238152 commit 7fdeb3c

File tree

1 file changed

+29
-62
lines changed

1 file changed

+29
-62
lines changed

apps/svelte.dev/src/lib/tutorial/adapters/webcontainer/index.svelte.js renamed to apps/svelte.dev/src/lib/tutorial/adapters/webcontainer/index.svelte.ts

Lines changed: 29 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,33 @@
1-
import { WebContainer } from '@webcontainer/api';
1+
import { WebContainer, type DirectoryNode, type FileSystemTree } from '@webcontainer/api';
22
import base64 from 'base64-js';
33
import AnsiToHtml from 'ansi-to-html';
44
// @ts-ignore package exports don't have types
55
import * as yootils from 'yootils';
6-
import { get_depth } from '../../../utils/path';
7-
import { escape_html } from '../../../utils/escape';
6+
import { get_depth } from '../../../utils/path.js';
7+
import { escape_html } from '../../../utils/escape.js';
88
import { ready } from '../common/index.js';
9+
import type { Adapter, FileStub, Stub, Warning } from '$lib/tutorial';
910

1011
const converter = new AnsiToHtml({
1112
fg: 'var(--sk-text-3)'
1213
});
1314

14-
/** @type {import('@webcontainer/api').WebContainer} Web container singleton */
15-
let vm;
15+
/** Web container singleton */
16+
let vm: WebContainer;
1617

1718
export const state = new (class WCState {
1819
progress = $state.raw({ value: 0, text: 'initialising' });
19-
/** @type {string | null} */
20-
base = $state.raw(null);
21-
/** @type {Error | null} */
22-
error = $state.raw(null);
23-
/** @type {string[]} */
24-
logs = $state.raw([]);
25-
/** @type {Record<string, import('$lib/tutorial').Warning[]>} */
26-
warnings = $state.raw({});
20+
base = $state.raw<string | null>(null);
21+
error = $state.raw<Error | null>(null);
22+
logs = $state.raw<string[]>([]);
23+
warnings = $state.raw<Record<string, Warning[]>>({});
2724
})();
2825

29-
/**
30-
* @returns {Promise<import('$lib/tutorial').Adapter>}
31-
*/
32-
export async function create() {
26+
export async function create(): Promise<Adapter> {
3327
state.progress = { value: 0, text: 'loading files' };
3428

3529
const q = yootils.queue(1);
36-
/** @type {Map<string, Array<import('$lib/tutorial').FileStub>>} */
37-
const q_per_file = new Map();
30+
const q_per_file = new Map<string, Array<FileStub>>();
3831

3932
/** Paths and contents of the currently loaded file stubs */
4033
let current_stubs = stubs_to_map([]);
@@ -53,14 +46,10 @@ export async function create() {
5346
}
5447
});
5548

56-
/** @type {Record<string, import('$lib/tutorial').Warning[]>} */
57-
let warnings = {};
49+
let warnings: Record<string, import('$lib/tutorial').Warning[]> = {};
50+
let timeout: any;
5851

59-
/** @type {any} */
60-
let timeout;
61-
62-
/** @param {number} msec */
63-
function schedule_to_update_warning(msec) {
52+
function schedule_to_update_warning(msec: number) {
6453
clearTimeout(timeout);
6554
timeout = setTimeout(() => (state.warnings = { ...warnings }), msec);
6655
}
@@ -72,8 +61,7 @@ export async function create() {
7261
// clear screen
7362
state.logs = [];
7463
} else if (chunk?.startsWith('svelte:warnings:')) {
75-
/** @type {import('$lib/tutorial').Warning} */
76-
const warn = JSON.parse(chunk.slice(16));
64+
const warn: Warning = JSON.parse(chunk.slice(16));
7765
const filename = warn.filename.startsWith('/') ? warn.filename : '/' + warn.filename;
7866
const current = warnings[filename];
7967

@@ -150,8 +138,7 @@ export async function create() {
150138
return {
151139
reset: (stubs) => {
152140
return q.add(async () => {
153-
/** @type {import('$lib/tutorial').Stub[]} */
154-
const to_write = [];
141+
const to_write: Stub[] = [];
155142

156143
const force_delete = [];
157144

@@ -164,9 +151,7 @@ export async function create() {
164151
continue;
165152
}
166153

167-
const current = /** @type {import('$lib/tutorial').FileStub} */ (
168-
current_stubs.get(stub.name)
169-
);
154+
const current = current_stubs.get(stub.name) as FileStub;
170155

171156
if (current?.contents !== stub.contents) {
172157
to_write.push(stub);
@@ -235,32 +220,30 @@ export async function create() {
235220
q_per_file.set(file.name, (queue = [file]));
236221

237222
return q.add(async () => {
238-
/** @type {import('@webcontainer/api').FileSystemTree} */
239-
const root = {};
223+
const root: FileSystemTree = {};
240224

241225
let tree = root;
242226

243227
const path = file.name.split('/').slice(1);
244-
const basename = /** @type {string} */ (path.pop());
228+
const basename = path.pop()!;
245229

246230
for (const part of path) {
247231
if (!tree[part]) {
248-
/** @type {import('@webcontainer/api').FileSystemTree} */
249-
const directory = {};
232+
const directory: FileSystemTree = {};
250233

251234
tree[part] = {
252235
directory
253236
};
254237
}
255238

256-
tree = /** @type {import('@webcontainer/api').DirectoryNode} */ (tree[part]).directory;
239+
tree = (tree[part] as DirectoryNode).directory;
257240
}
258241

259242
const will_restart = is_config(file);
260243

261244
while (queue && queue.length > 0) {
262245
// if the file is updated many times rapidly, get the most recently updated one
263-
const file = /** @type {import('$lib/tutorial').FileStub} */ (queue.pop());
246+
const file = queue.pop()!;
264247
queue.length = 0;
265248

266249
tree[basename] = to_file(file);
@@ -289,17 +272,11 @@ export async function create() {
289272
};
290273
}
291274

292-
/**
293-
* @param {import('$lib/tutorial').Stub} file
294-
*/
295-
function is_config(file) {
275+
function is_config(file: Stub) {
296276
return file.type === 'file' && is_config_path(file.name);
297277
}
298278

299-
/**
300-
* @param {string} path
301-
*/
302-
function is_config_path(path) {
279+
function is_config_path(path: string) {
303280
return ['/vite.config.js', '/svelte.config.js', '/.env'].includes(path);
304281
}
305282

@@ -322,13 +299,8 @@ function wait_for_restart_vite() {
322299
});
323300
}
324301

325-
/**
326-
* @param {import('$lib/tutorial').Stub[]} stubs
327-
* @returns {import('@webcontainer/api').FileSystemTree}
328-
*/
329-
function convert_stubs_to_tree(stubs, depth = 1) {
330-
/** @type {import('@webcontainer/api').FileSystemTree} */
331-
const tree = {};
302+
function convert_stubs_to_tree(stubs: Stub[], depth = 1) {
303+
const tree: FileSystemTree = {};
332304

333305
for (const stub of stubs) {
334306
if (get_depth(stub.name) === depth) {
@@ -347,8 +319,7 @@ function convert_stubs_to_tree(stubs, depth = 1) {
347319
return tree;
348320
}
349321

350-
/** @param {import('$lib/tutorial').FileStub} file */
351-
function to_file(file) {
322+
function to_file(file: FileStub) {
352323
// special case
353324
if (file.name === '/src/app.html' || file.name === '/src/error.html') {
354325
const contents = file.contents + '<script type="module" src="/src/__client.js"></script>';
@@ -365,11 +336,7 @@ function to_file(file) {
365336
};
366337
}
367338

368-
/**
369-
* @param {import('$lib/tutorial').Stub[]} files
370-
* @returns {Map<string, import('$lib/tutorial').Stub>}
371-
*/
372-
function stubs_to_map(files, map = new Map()) {
339+
function stubs_to_map(files: Stub[], map = new Map<string, Stub>()) {
373340
for (const file of files) {
374341
map.set(file.name, file);
375342
}

0 commit comments

Comments
 (0)