-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added experimental dependency resolving for ts
- Loading branch information
Showing
9 changed files
with
166 additions
and
136 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,13 +1,48 @@ | ||
import ts, { TranspileOptions } from 'typescript'; | ||
import ts, { CustomTransformerFactory, ModuleResolutionKind, SourceFile, TransformationContext, Transformer, TransformerFactory, TranspileOptions } from 'typescript'; | ||
console.log('creating ts service'); | ||
// @ts-ignore | ||
return class { | ||
static transformer: TransformerFactory<SourceFile> | CustomTransformerFactory; | ||
static compileTypeScript(code: string, options?: TranspileOptions) { | ||
options ??= {}; | ||
options.compilerOptions ??= { | ||
module: ts.ModuleKind.Preserve, | ||
target: ts.ScriptTarget.ESNext, | ||
experimentalDecorators: true, | ||
moduleResolution: ModuleResolutionKind.Classic | ||
}; | ||
return ts.transpileModule(code, options).outputText; | ||
this.transformer = <T extends ts.Node>(context: TransformationContext) => { | ||
return (node: T) => { | ||
function visit(node: ts.Node): ts.Node { | ||
// Check if the node is an import declaration | ||
if (ts.isImportDeclaration(node)) { | ||
const importPath = node.moduleSpecifier.getText().slice(1, -1); // Remove quotes around the path | ||
console.log('visiting', importPath, !importPath.startsWith("/") && !importPath.startsWith(".") && !importPath.startsWith('http:') && !importPath.startsWith('https:')); | ||
// Only transform if the path does not contain '/', '.', or a protocol | ||
if (!importPath.startsWith("/") && !importPath.startsWith(".") && !importPath.startsWith('http:') && !importPath.startsWith('https:')) { | ||
|
||
// Replace the import path with "https://esm.sh/..." | ||
const newImportPath = ts.factory.createStringLiteral(`https://esm.sh/${importPath}`); | ||
console.log(newImportPath); | ||
return ts.factory.updateImportDeclaration( | ||
node, | ||
node.modifiers, | ||
node.importClause, | ||
newImportPath, | ||
node.attributes | ||
); | ||
} | ||
} | ||
return node; | ||
} | ||
return ts.visitEachChild(node, visit, context); | ||
} | ||
} | ||
|
||
const result = ts.transpileModule(code, { | ||
...options, | ||
transformers: { before: [this.transformer] } | ||
}).outputText; | ||
return result; | ||
} | ||
} |
This file contains 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