-
-
Notifications
You must be signed in to change notification settings - Fork 3k
fix(mcp): emit Node-resolvable ESM imports #861
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
Merged
+93
−4
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2b1e350
fix(mcp): emit Node-resolvable ESM imports
Aymericr 4382fd5
Merge remote-tracking branch 'origin/main' into codex/fix-mcp-node-esm
Aymericr 3893951
fix(mcp): rewrite referenced core output
Aymericr 6fbcc6e
fix(build): rewrite declaration imports for Node ESM
Aymericr 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
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,81 @@ | ||
| import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs' | ||
| import { dirname, join, resolve } from 'node:path' | ||
| import ts from 'typescript' | ||
|
|
||
| const distDir = resolve(process.cwd(), process.argv[2] ?? 'dist') | ||
|
|
||
| function emittedModuleFiles(directory: string): string[] { | ||
| return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => { | ||
| const path = join(directory, entry.name) | ||
| if (entry.isDirectory()) return emittedModuleFiles(path) | ||
| return entry.isFile() && (entry.name.endsWith('.js') || entry.name.endsWith('.d.ts')) | ||
| ? [path] | ||
| : [] | ||
| }) | ||
| } | ||
|
|
||
| function moduleSpecifiers(sourceFile: ts.SourceFile): ts.StringLiteral[] { | ||
| const specifiers: ts.StringLiteral[] = [] | ||
| const visit = (node: ts.Node) => { | ||
| if ( | ||
| (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) && | ||
| node.moduleSpecifier && | ||
| ts.isStringLiteral(node.moduleSpecifier) | ||
| ) { | ||
| specifiers.push(node.moduleSpecifier) | ||
| } else if ( | ||
| ts.isCallExpression(node) && | ||
| node.expression.kind === ts.SyntaxKind.ImportKeyword && | ||
| node.arguments.length === 1 && | ||
| ts.isStringLiteral(node.arguments[0]!) | ||
| ) { | ||
| specifiers.push(node.arguments[0]!) | ||
| } else if ( | ||
| ts.isImportTypeNode(node) && | ||
| ts.isLiteralTypeNode(node.argument) && | ||
| ts.isStringLiteral(node.argument.literal) | ||
| ) { | ||
| specifiers.push(node.argument.literal) | ||
| } | ||
| ts.forEachChild(node, visit) | ||
| } | ||
| visit(sourceFile) | ||
| return specifiers | ||
| } | ||
|
|
||
| function resolvedSpecifier(file: string, specifier: string): string { | ||
| if (!specifier.startsWith('.') || /\.(?:[cm]?js|json|node)$/.test(specifier)) return specifier | ||
| const target = resolve(dirname(file), specifier) | ||
| const emittedExtension = file.endsWith('.d.ts') ? '.d.ts' : '.js' | ||
| if (existsSync(`${target}${emittedExtension}`)) return `${specifier}.js` | ||
| if (existsSync(join(target, `index${emittedExtension}`))) { | ||
| return `${specifier.replace(/\/$/, '')}/index.js` | ||
| } | ||
| throw new Error(`Cannot resolve extensionless ESM import ${specifier} from ${file}`) | ||
| } | ||
|
|
||
| for (const file of emittedModuleFiles(distDir)) { | ||
| const source = readFileSync(file, 'utf8') | ||
| const parsed = ts.createSourceFile( | ||
| file, | ||
| source, | ||
| ts.ScriptTarget.Latest, | ||
| true, | ||
| file.endsWith('.d.ts') ? ts.ScriptKind.TS : ts.ScriptKind.JS, | ||
| ) | ||
| const replacements = moduleSpecifiers(parsed) | ||
| .map((node) => ({ | ||
| start: node.getStart(parsed) + 1, | ||
| end: node.getEnd() - 1, | ||
| value: resolvedSpecifier(file, node.text), | ||
| })) | ||
| .filter((replacement) => source.slice(replacement.start, replacement.end) !== replacement.value) | ||
| .sort((a, b) => b.start - a.start) | ||
|
|
||
| let rewritten = source | ||
| for (const replacement of replacements) { | ||
| rewritten = | ||
| rewritten.slice(0, replacement.start) + replacement.value + rewritten.slice(replacement.end) | ||
| } | ||
| if (rewritten !== source) writeFileSync(file, rewritten) | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.