Fix some stuff

pull/8452/head
Puru Vijay 3 years ago
parent 9ce5d9636d
commit a42a769b51

@ -109,7 +109,6 @@ Out of `onMount`, `beforeUpdate`, `afterUpdate` and `onDestroy`, this is the onl
## `tick`
```js
/** @type {Promise<void>} */
promise = tick();
```

@ -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

@ -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();

@ -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);
// {
// 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;
// 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 ?? '');
// // @ts-ignore
// const comment = strip_origin(statement.jsDoc?.[0].comment ?? '');
modules.push({
name,
comment,
// @ts-ignore
...get_types(code, statement.body?.statements)
});
}
}
}
// modules.push({
// name,
// comment,
// // @ts-ignore
// ...get_types(code, statement.body?.statements)
// });
// }
// }
// }
modules.sort((a, b) => (a.name < b.name ? -1 : 1));

@ -11,6 +11,22 @@
<meta name="twitter:site" content="@sveltejs" />
<meta name="twitter:creator" content="@sveltejs" />
<!-- add inline style and blocking script to prevent content flash/jump -->
<style>
.ts-version {
display: none;
}
.prefers-ts .js-version {
display: none;
}
.prefers-ts .ts-version {
display: block;
}
.no-js .ts-toggle {
display: none;
}
</style>
%sveltekit.head%
</head>
<body data-sveltekit-prefetch>

Loading…
Cancel
Save