diff --git a/sites/svelte.dev/package-lock.json b/sites/svelte.dev/package-lock.json index dd1a1eaee5..bc5b5b86b0 100644 --- a/sites/svelte.dev/package-lock.json +++ b/sites/svelte.dev/package-lock.json @@ -21,7 +21,7 @@ "@resvg/resvg-js": "^2.4.1", "@sveltejs/adapter-vercel": "^2.4.1", "@sveltejs/kit": "^1.15.4", - "@sveltejs/site-kit": "^4.0.2", + "@sveltejs/site-kit": "^4.1.0", "@types/marked": "^4.0.8", "@types/prismjs": "^1.26.0", "degit": "^2.8.4", @@ -42,6 +42,7 @@ "shiki-twoslash": "^3.1.1", "svelte": "^3.58.0", "svelte-check": "^3.2.0", + "ts-morph": "^18.0.0", "typescript": "^5.0.4", "vite": "^4.2.1", "vite-imagetools": "^4.0.18" @@ -1339,9 +1340,9 @@ "integrity": "sha512-HBLtfNdLr5Ykl8i8CvJcYjib7zMIJupg4T/omplp3ccpgpiUh26tk71vRG1+a6yMkfbfy0ShoPb9uwNril5cnw==" }, "node_modules/@sveltejs/site-kit": { - "version": "4.0.2", - "resolved": "https://registry.npmjs.org/@sveltejs/site-kit/-/site-kit-4.0.2.tgz", - "integrity": "sha512-gqxBMZW5ZiO61m7Bn4x0fMUJC1M27ku1u7u0KJrg0zRSN3Zu30VdwE1SwWO4ueKxOxLXD2gzfUMdvoez7ZoBuQ==", + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/@sveltejs/site-kit/-/site-kit-4.1.0.tgz", + "integrity": "sha512-DRX3BSyuDvTWcqP5KucomcU90uHKgOcXgTF5mlPcZvoJ4Ekyt9+SbOMR+FU1CBNv1UzYkEtvMuGuQwc1+DCaTQ==", "dev": true, "dependencies": { "svelte-local-storage-store": "^0.4.0" @@ -1378,6 +1379,42 @@ "integrity": "sha512-OvjF+z51L3ov0OyAU0duzsYuvO01PH7x4t6DJx+guahgTnBHkhJdG7soQeTSFLWN3efnHyibZ4Z8l2EuWwJN3A==", "dev": true }, + "node_modules/@ts-morph/common": { + "version": "0.19.0", + "resolved": "https://registry.npmjs.org/@ts-morph/common/-/common-0.19.0.tgz", + "integrity": "sha512-Unz/WHmd4pGax91rdIKWi51wnVUW11QttMEPpBiBgIewnc9UQIX7UDLxr5vRlqeByXCwhkF6VabSsI0raWcyAQ==", + "dev": true, + "dependencies": { + "fast-glob": "^3.2.12", + "minimatch": "^7.4.3", + "mkdirp": "^2.1.6", + "path-browserify": "^1.0.1" + } + }, + "node_modules/@ts-morph/common/node_modules/brace-expansion": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dev": true, + "dependencies": { + "balanced-match": "^1.0.0" + } + }, + "node_modules/@ts-morph/common/node_modules/minimatch": { + "version": "7.4.6", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-7.4.6.tgz", + "integrity": "sha512-sBz8G/YjVniEz6lKPNpKxXwazJe4c19fEfV2GDMX6AjFz+MX9uDWIZW8XreVhkFW3fkIdTv/gxWr/Kks5FFAVw==", + "dev": true, + "dependencies": { + "brace-expansion": "^2.0.1" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, "node_modules/@types/cookie": { "version": "0.5.1", "resolved": "https://registry.npmjs.org/@types/cookie/-/cookie-0.5.1.tgz", @@ -1804,6 +1841,12 @@ "node": ">=10" } }, + "node_modules/code-block-writer": { + "version": "12.0.0", + "resolved": "https://registry.npmjs.org/code-block-writer/-/code-block-writer-12.0.0.tgz", + "integrity": "sha512-q4dMFMlXtKR3XNBHyMHt/3pwYNA69EDk00lloMOaaUMKPUXBw6lpXtbu3MMVG6/uOihGnRDOlkyqsONEUj60+w==", + "dev": true + }, "node_modules/codemirror": { "version": "5.65.1", "resolved": "https://registry.npmjs.org/codemirror/-/codemirror-5.65.1.tgz", @@ -3275,6 +3318,12 @@ "integrity": "sha512-ft3iAoLOB/MlwbNXgzy43SWGP6sQki2jQvAyBg/zDFAgr9bfNWZIUj42Kw2eJIl8kEi4PbgE6U1Zau/HwI75HA==", "dev": true }, + "node_modules/path-browserify": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz", + "integrity": "sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g==", + "dev": true + }, "node_modules/path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", @@ -4391,6 +4440,16 @@ "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" }, + "node_modules/ts-morph": { + "version": "18.0.0", + "resolved": "https://registry.npmjs.org/ts-morph/-/ts-morph-18.0.0.tgz", + "integrity": "sha512-Kg5u0mk19PIIe4islUI/HWRvm9bC1lHejK4S0oh1zaZ77TMZAEmQC0sHQYiu2RgCQFZKXz1fMVi/7nOOeirznA==", + "dev": true, + "dependencies": { + "@ts-morph/common": "~0.19.0", + "code-block-writer": "^12.0.0" + } + }, "node_modules/tunnel-agent": { "version": "0.6.0", "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", diff --git a/sites/svelte.dev/package.json b/sites/svelte.dev/package.json index f6a9a3573a..b011219c8a 100644 --- a/sites/svelte.dev/package.json +++ b/sites/svelte.dev/package.json @@ -30,7 +30,7 @@ "@resvg/resvg-js": "^2.4.1", "@sveltejs/adapter-vercel": "^2.4.1", "@sveltejs/kit": "^1.15.4", - "@sveltejs/site-kit": "^4.0.2", + "@sveltejs/site-kit": "^4.1.0", "@types/marked": "^4.0.8", "@types/prismjs": "^1.26.0", "degit": "^2.8.4", @@ -51,6 +51,7 @@ "shiki-twoslash": "^3.1.1", "svelte": "^3.58.0", "svelte-check": "^3.2.0", + "ts-morph": "^18.0.0", "typescript": "^5.0.4", "vite": "^4.2.1", "vite-imagetools": "^4.0.18" diff --git a/sites/svelte.dev/scripts/type-gen/compile-types.js b/sites/svelte.dev/scripts/type-gen/compile-types.js index 08dd58b9bb..a829e4dac8 100644 --- a/sites/svelte.dev/scripts/type-gen/compile-types.js +++ b/sites/svelte.dev/scripts/type-gen/compile-types.js @@ -3,10 +3,11 @@ import MagicString from 'magic-string'; import fs from 'node:fs'; import { rollup } from 'rollup'; import dts from 'rollup-plugin-dts'; -import ts from 'typescript'; import { VERSION } from 'svelte/compiler'; +import { Project, SyntaxKind } from 'ts-morph'; +import ts from 'typescript'; -get_bundled_types(); +// get_bundled_types(); export async function get_bundled_types() { const dtsSources = fs.readdirSync(new URL('./dts-sources', import.meta.url)); @@ -40,69 +41,57 @@ export async function get_bundled_types() { * @returns {[string, ts.SourceFile]} */ function useExportDeclarations(str) { - const magicStr = new MagicString(str); - - const sourceFile = ts.createSourceFile( - 'index.d.ts', - str, - ts.ScriptTarget.ESNext, - true, - ts.ScriptKind.TS - ); + const project = new Project(); + const source_file = project.createSourceFile('index.d.ts', str, { overwrite: true }); // There's only gonna be one because of the output of dts-plugin - const exportDeclaration = sourceFile.statements.find((statement) => - ts.isExportDeclaration(statement) - ); - - if (exportDeclaration && !ts.isExportDeclaration(exportDeclaration)) return [str, sourceFile]; - - // @ts-ignore Why does TS not identify `elements` - const exportedSymbols = exportDeclaration?.exportClause?.elements.map( - (element) => element.name.text - ); - - // // find the elements who are being exported as alias, like `export { VERSION as __VERSION__ }` - // const aliasedExportedSymbols = new Map( - // // @ts-ignore Why does TS not identify `elements` - // exportDeclaration?.exportClause?.elements - // .filter((element) => element.propertyName) - // .map((element) => [element.name.text, element.propertyName.text]) - // ); - - for (const statement of sourceFile.statements) { - if ( - !( - ts.isFunctionDeclaration(statement) || - ts.isInterfaceDeclaration(statement) || - ts.isTypeAliasDeclaration(statement) || - ts.isVariableStatement(statement) - ) - ) - continue; - - // // Now rewrite the aliased exports to be inline. This risk collision, so hope that doesn't happen - // if ( - // (ts.isVariableStatement(statement) && - // aliasedExportedSymbols.has(statement.declarationList.declarations[0].name.getText())) || - // // @ts-ignore - // aliasedExportedSymbols.has(statement.name?.getText()) - // ) - // for (const [og, exported] of aliasedExportedSymbols) { - // // Modify the current statement, change the variable name - // magicStr.overwrite(); - // } - - for (const exportedSymbol of exportedSymbols) { - if ( - (ts.isVariableStatement(statement) && - statement.declarationList.declarations[0].name.getText() === exportedSymbol) || - // @ts-ignore - statement.name?.getText() === exportedSymbol - ) { - magicStr.appendLeft(statement.getStart(), 'export '); - } + const exportDeclaration = source_file.getExportDeclarations()[0]; + const exportedSymbols = exportDeclaration + .getNamedExports() + .map((e) => e.getAliasNode()?.getText() ?? e.getNameNode().getText()); + + // console.log(exportedSymbols); + if (exportedSymbols.length === 0) + return [ + str, + ts.createSourceFile('index.d.ts', str, ts.ScriptTarget.ESNext, true, ts.ScriptKind.TS) + ]; + + const aliasedExportedSymbols = new Map(); + const namedExports = exportDeclaration.getNamedExports(); + + namedExports.forEach((namedExport) => { + if (namedExport.getAliasNode()) { + const alias = namedExport.getAliasNode()?.getText(); + const originalName = namedExport.getNameNode().getText(); + aliasedExportedSymbols.set(alias, originalName); } + }); + + for (const [exported, og] of aliasedExportedSymbols) { + source_file.forEachDescendant((node) => { + if (node.getKind() === ts.SyntaxKind.Identifier && node.getText() === og) { + node.replaceWithText(exported); + } + }); + } + + const magicStr = new MagicString(source_file.getFullText()); + + // Find all the identifiers from ewport declaration and prefix export before them + const identifiers = [ + ...new Set( + source_file + .getDescendantsOfKind(SyntaxKind.Identifier) + .filter((identifier) => exportedSymbols.includes(identifier.getText())) + .filter( + (value, index, self) => index === self.findIndex((t) => t.getText() === value.getText()) + ) + ) + ]; + + for (const identifier of identifiers) { + magicStr.appendLeft(identifier?.getFirstAncestor()?.getStartLinePos() ?? 0, 'export '); } magicStr.remove(exportDeclaration?.getStart() ?? 0, exportDeclaration?.getEnd() ?? 0);