diff --git a/site/content/docs/03-runtime/01-svelte.md b/site/content/docs/03-runtime/01-svelte.md index 584afc5b81..8df7f93936 100644 --- a/site/content/docs/03-runtime/01-svelte.md +++ b/site/content/docs/03-runtime/01-svelte.md @@ -109,7 +109,6 @@ Out of `onMount`, `beforeUpdate`, `afterUpdate` and `onDestroy`, this is the onl ## `tick` ```js -/** @type {Promise} */ promise = tick(); ``` diff --git a/site/content/docs/04-compiler-and-api/01-svelte-compiler.md b/site/content/docs/04-compiler-and-api/01-svelte-compiler.md index a83b142291..09964d2b8c 100644 --- a/site/content/docs/04-compiler-and-api/01-svelte-compiler.md +++ b/site/content/docs/04-compiler-and-api/01-svelte-compiler.md @@ -340,3 +340,7 @@ The current version, as set in package.json. import svelte from 'svelte/compiler'; console.log(`running svelte version ${svelte.VERSION}`); ``` + +## Types + +> TYPES: svelte/compiler diff --git a/sites/svelte.dev/scripts/type-gen/compile-types.js b/sites/svelte.dev/scripts/type-gen/compile-types.js index 4ad75cd358..c6a35cf709 100644 --- a/sites/svelte.dev/scripts/type-gen/compile-types.js +++ b/sites/svelte.dev/scripts/type-gen/compile-types.js @@ -4,6 +4,7 @@ import fs from 'node:fs'; import { rollup } from 'rollup'; import dts from 'rollup-plugin-dts'; import ts from 'typescript'; +import { VERSION } from 'svelte/compiler'; export async function get_bundled_types() { const dtsSources = fs.readdirSync(new URL('./dts-sources', import.meta.url)); @@ -14,10 +15,14 @@ export async function get_bundled_types() { for (const file of dtsSources) { const bundle = await rollup({ input: new URL(`./dts-sources/${file}`, import.meta.url).pathname, - plugins: [dts({ respectExternal: true })], - external: ['estree-walker'] + plugins: [dts({ respectExternal: true })] + // external: ['estree-walker'] }); + try { + fs.mkdirSync(new URL(`../../node_modules/.type-gen`, import.meta.url)); + } catch {} + codes.set( (file === 'index.d.ts' ? 'svelte' : `svelte/${file}`).replace('.d.ts', ''), useExportDeclarations( @@ -29,7 +34,9 @@ export async function get_bundled_types() { return codes; } -/** @param {string} str */ +/** + * @param {string} str + */ function useExportDeclarations(str) { const magicStr = new MagicString(str); @@ -42,8 +49,8 @@ function useExportDeclarations(str) { ); // There's only gonna be one because of the output of dts-plugin - const exportDeclaration = sourceFile.statements.find( - (statement) => statement.kind === ts.SyntaxKind.ExportDeclaration + const exportDeclaration = sourceFile.statements.find((statement) => + ts.isExportDeclaration(statement) ); if (exportDeclaration && !ts.isExportDeclaration(exportDeclaration)) return str; @@ -59,13 +66,18 @@ function useExportDeclarations(str) { ts.isFunctionDeclaration(statement) || ts.isInterfaceDeclaration(statement) || ts.isTypeAliasDeclaration(statement) || - ts.isVariableDeclaration(statement) + ts.isVariableStatement(statement) ) ) continue; for (const exportedSymbol of exportedSymbols) { - if (statement.name?.getText() === exportedSymbol) { + if ( + (ts.isVariableStatement(statement) && + statement.declarationList.declarations[0].name.getText() === exportedSymbol) || + // @ts-ignore + statement.name?.getText() === exportedSymbol + ) { magicStr.appendLeft(statement.getStart(), 'export '); } } @@ -73,5 +85,10 @@ function useExportDeclarations(str) { magicStr.remove(exportDeclaration?.getStart() ?? 0, exportDeclaration?.getEnd() ?? 0); + // In case it is export declare VERSION = '__VERSION__', replace it with svelte's real version + magicStr.replace('__VERSION__', VERSION); + return magicStr.toString() ?? str; } + +get_bundled_types(); diff --git a/sites/svelte.dev/scripts/type-gen/index.js b/sites/svelte.dev/scripts/type-gen/index.js index 2ec3a3910f..e23833c9dc 100644 --- a/sites/svelte.dev/scripts/type-gen/index.js +++ b/sites/svelte.dev/scripts/type-gen/index.js @@ -205,16 +205,17 @@ function read_d_ts_file(str) { const bundled_types = await get_bundled_types(); -// { -// const code = bundled_types.get('svelte/compiler') ?? ''; -// const node = ts.createSourceFile('compiler/index.d.ts', code, ts.ScriptTarget.Latest, true); +{ + const code = bundled_types.get('svelte/compiler') ?? ''; + // console.log(code); + const node = ts.createSourceFile('compiler/index.d.ts', code, ts.ScriptTarget.Latest, true); -// modules.push({ -// name: 'svelte/compiler', -// comment: '', -// ...get_types(code, node.statements) -// }); -// } + modules.push({ + name: 'svelte/compiler', + comment: '', + ...get_types(code, node.statements) + }); +} { const code = bundled_types.get('svelte/runtime') ?? ''; @@ -336,27 +337,27 @@ const bundled_types = await get_bundled_types(); // }); // } -{ - const code = read_d_ts_file('types/ambient.d.ts'); - const node = ts.createSourceFile('ambient.d.ts', code, ts.ScriptTarget.Latest, true); - - for (const statement of node.statements) { - if (ts.isModuleDeclaration(statement)) { - // @ts-ignore - const name = statement.name.text || statement.name.escapedText; - - // @ts-ignore - const comment = strip_origin(statement.jsDoc?.[0].comment ?? ''); - - modules.push({ - name, - comment, - // @ts-ignore - ...get_types(code, statement.body?.statements) - }); - } - } -} +// { +// const code = read_d_ts_file('types/ambient.d.ts'); +// const node = ts.createSourceFile('ambient.d.ts', code, ts.ScriptTarget.Latest, true); + +// for (const statement of node.statements) { +// if (ts.isModuleDeclaration(statement)) { +// // @ts-ignore +// const name = statement.name.text || statement.name.escapedText; + +// // @ts-ignore +// const comment = strip_origin(statement.jsDoc?.[0].comment ?? ''); + +// modules.push({ +// name, +// comment, +// // @ts-ignore +// ...get_types(code, statement.body?.statements) +// }); +// } +// } +// } modules.sort((a, b) => (a.name < b.name ? -1 : 1)); diff --git a/sites/svelte.dev/src/app.html b/sites/svelte.dev/src/app.html index 395fdfe1df..7ad34b9933 100644 --- a/sites/svelte.dev/src/app.html +++ b/sites/svelte.dev/src/app.html @@ -11,6 +11,22 @@ + + + %sveltekit.head%