-
Notifications
You must be signed in to change notification settings - Fork 6.5k
feat: enable Twoslash on Cloudflare #8837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dario-piotrowicz
wants to merge
4
commits into
nodejs:main
Choose a base branch
from
dario-piotrowicz:dario/two-slash-on-cloudflare
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c569d96
feat: enable Twoslash on Cloudflare
dario-piotrowicz f506e7b
move `createTwoslasher` to its own file
dario-piotrowicz 5244de2
Simplify imports -- remove Promise.all and .then()
dario-piotrowicz 23c7898
Simplify generate.mjs using @typescript/vfs
dario-piotrowicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Creates a Twoslash instance backed by a virtual filesystem for environments | ||
| * without real filesystem access (e.g. Cloudflare Workers). | ||
| * | ||
| * Uses a pre-built JSON map of TypeScript lib declarations and @types/node | ||
| * generated at build time by `scripts/twoslash-fsmap/index.mjs`. | ||
| */ | ||
| export async function createVfsTwoslasher() { | ||
| const { createTwoslasher } = await import('twoslash/core'); | ||
| const ts = (await import('typescript')).default; | ||
| const fsMapJson = ( | ||
| await import('../generated/twoslash-fsmap.json', { with: { type: 'json' } }) | ||
| ).default; | ||
|
|
||
| const fsMap = new Map(Object.entries(fsMapJson)); | ||
|
|
||
| return createTwoslasher({ | ||
| fsMap, | ||
| tsModule: ts, | ||
| vfsRoot: '/', | ||
| compilerOptions: { | ||
| moduleResolution: ts.ModuleResolutionKind.Bundler, | ||
| // Explicitly include @types/node so that the VFS resolves Node.js | ||
| // globals and `node:*` module imports from the bundled declarations. | ||
| types: ['node'], | ||
| }, | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| 'use strict'; | ||
|
|
||
| import { readdirSync, readFileSync } from 'node:fs'; | ||
| import { createRequire } from 'node:module'; | ||
| import { join, resolve } from 'node:path'; | ||
|
|
||
| import { createDefaultMapFromNodeModules } from '@typescript/vfs'; | ||
| import ts from 'typescript'; | ||
|
|
||
| const require = createRequire(import.meta.url); | ||
|
|
||
| /** | ||
| * Recursively collects all `.d.ts` files from a directory into the fsMap. | ||
| * | ||
| * @param {Map<string, string>} fsMap The map to populate | ||
| * @param {string} dir The directory to walk | ||
| * @param {string} virtualPrefix The virtual path prefix (e.g., "/node_modules/@types/node") | ||
| * @param {string} baseDir The base directory for computing relative paths | ||
| */ | ||
| function collectDtsFiles(fsMap, dir, virtualPrefix, baseDir) { | ||
| const entries = readdirSync(dir, { withFileTypes: true }).sort((a, b) => | ||
| a.name.localeCompare(b.name) | ||
| ); | ||
|
|
||
| for (const entry of entries) { | ||
| const fullPath = join(dir, entry.name); | ||
|
|
||
| if (entry.isDirectory()) { | ||
| collectDtsFiles(fsMap, fullPath, virtualPrefix, baseDir); | ||
| } else if (entry.isFile() && /\.d\.([^.]+\.)?[cm]?ts$/i.test(entry.name)) { | ||
| const relativePath = fullPath.slice(baseDir.length).replace(/\\/g, '/'); | ||
| const virtualPath = `${virtualPrefix}${relativePath}`; | ||
|
|
||
| fsMap.set(virtualPath, readFileSync(fullPath, 'utf8')); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Generates a virtual filesystem map containing all TypeScript library | ||
| * declaration files and `@types/node` declarations needed for Twoslash | ||
| * to run without real filesystem access (e.g., on Cloudflare Workers). | ||
| * | ||
| * @returns {Map<string, string>} A map of virtual paths to file contents | ||
| */ | ||
| export default function generateTwoslashFsMap() { | ||
| // 1. Collect TypeScript lib .d.ts files using @typescript/vfs | ||
| // This returns a Map keyed as "/lib.es5.d.ts", "/lib.dom.d.ts", etc. | ||
| const fsMap = createDefaultMapFromNodeModules({}, ts); | ||
|
|
||
| // 2. Collect @types/node .d.ts files | ||
| // These are keyed as "/node_modules/@types/node/index.d.ts", etc. | ||
| const typesNodeDir = resolve( | ||
| require.resolve('@types/node/package.json'), | ||
| '..' | ||
| ); | ||
|
|
||
| collectDtsFiles( | ||
| fsMap, | ||
| typesNodeDir, | ||
| '/node_modules/@types/node', | ||
| typesNodeDir | ||
| ); | ||
|
|
||
| return fsMap; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| 'use strict'; | ||
|
|
||
| import { mkdirSync, writeFileSync } from 'node:fs'; | ||
|
|
||
| import generateTwoslashFsMap from './generate.mjs'; | ||
|
|
||
| const fsMap = generateTwoslashFsMap(); | ||
|
|
||
| const outputPath = new URL( | ||
| '../../generated/twoslash-fsmap.json', | ||
| import.meta.url | ||
| ); | ||
|
dario-piotrowicz marked this conversation as resolved.
|
||
|
|
||
| mkdirSync(new URL('.', outputPath), { recursive: true }); | ||
| writeFileSync(outputPath, JSON.stringify(Object.fromEntries(fsMap)), 'utf8'); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we really need any of this? I'm fairly sue twoslash supports using vfs out of the box? Since they have this CDN thing https://twoslash.netlify.app/packages/cdn (for example)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mh... that is interesting, however I am a bit worried on moving this to the runtime since it would add network calls for fetching from the CDN increasing cold starts 😕
Are we sure that we'd be happy with such tradeoff?