Skip to content

Commit 23c7898

Browse files
Simplify generate.mjs using @typescript/vfs
1 parent 5244de2 commit 23c7898

4 files changed

Lines changed: 15 additions & 18 deletions

File tree

apps/site/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"@tailwindcss/postcss": "~4.3.0",
4949
"@types/node": "catalog:",
5050
"@types/react": "catalog:",
51+
"@typescript/vfs": "^1.6.4",
5152
"@vcarl/remark-headings": "~0.1.0",
5253
"@vercel/analytics": "~2.0.1",
5354
"@vercel/otel": "~2.1.2",

apps/site/scripts/twoslash-fsmap/generate.mjs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
import { readdirSync, readFileSync } from 'node:fs';
44
import { createRequire } from 'node:module';
5-
import { dirname, join, resolve } from 'node:path';
5+
import { join, resolve } from 'node:path';
6+
7+
import { createDefaultMapFromNodeModules } from '@typescript/vfs';
8+
import ts from 'typescript';
69

710
const require = createRequire(import.meta.url);
811

912
/**
1013
* Recursively collects all `.d.ts` files from a directory into the fsMap.
1114
*
12-
* @param {Record<string, string>} fsMap The map to populate
15+
* @param {Map<string, string>} fsMap The map to populate
1316
* @param {string} dir The directory to walk
1417
* @param {string} virtualPrefix The virtual path prefix (e.g., "/node_modules/@types/node")
1518
* @param {string} baseDir The base directory for computing relative paths
@@ -28,7 +31,7 @@ function collectDtsFiles(fsMap, dir, virtualPrefix, baseDir) {
2831
const relativePath = fullPath.slice(baseDir.length).replace(/\\/g, '/');
2932
const virtualPath = `${virtualPrefix}${relativePath}`;
3033

31-
fsMap[virtualPath] = readFileSync(fullPath, 'utf8');
34+
fsMap.set(virtualPath, readFileSync(fullPath, 'utf8'));
3235
}
3336
}
3437
}
@@ -38,22 +41,12 @@ function collectDtsFiles(fsMap, dir, virtualPrefix, baseDir) {
3841
* declaration files and `@types/node` declarations needed for Twoslash
3942
* to run without real filesystem access (e.g., on Cloudflare Workers).
4043
*
41-
* @returns {Record<string, string>} A map of virtual paths to file contents
44+
* @returns {Map<string, string>} A map of virtual paths to file contents
4245
*/
4346
export default function generateTwoslashFsMap() {
44-
const fsMap = {};
45-
46-
// 1. Collect TypeScript lib .d.ts files
47-
// These are keyed as "/lib.es5.d.ts", "/lib.dom.d.ts", etc.
48-
// (matching the convention used by @typescript/vfs)
49-
const tsLibDir = dirname(require.resolve('typescript/lib/lib.d.ts'));
50-
const tsLibFiles = readdirSync(tsLibDir)
51-
.filter(f => f.startsWith('lib.') && /\.d\.([^.]+\.)?[cm]?ts$/i.test(f))
52-
.sort();
53-
54-
for (const file of tsLibFiles) {
55-
fsMap[`/${file}`] = readFileSync(join(tsLibDir, file), 'utf8');
56-
}
47+
// 1. Collect TypeScript lib .d.ts files using @typescript/vfs
48+
// This returns a Map keyed as "/lib.es5.d.ts", "/lib.dom.d.ts", etc.
49+
const fsMap = createDefaultMapFromNodeModules({}, ts);
5750

5851
// 2. Collect @types/node .d.ts files
5952
// These are keyed as "/node_modules/@types/node/index.d.ts", etc.

apps/site/scripts/twoslash-fsmap/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ const outputPath = new URL(
1212
);
1313

1414
mkdirSync(new URL('.', outputPath), { recursive: true });
15-
writeFileSync(outputPath, JSON.stringify(fsMap), 'utf8');
15+
writeFileSync(outputPath, JSON.stringify(Object.fromEntries(fsMap)), 'utf8');

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)