Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions core/src/misc/transpile-file/imports-transformer.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import * as ts from "typescript";
import * as path from "path";

export function importToGlobalTransformer(): ts.TransformerFactory<ts.SourceFile> {
/**
* Transforms ES module imports into accesses on a global __imports object.
* Additionally, resolves relative import specifiers to absolute paths based on the
* source file location. This ensures that after a file is moved, the import still
* points to the correct module.
*/
export function importToGlobalTransformer(fileName?: string): ts.TransformerFactory<ts.SourceFile> {
return (context) => {
const visit: ts.Visitor = (node) => {
if (ts.isImportDeclaration(node)) {
const moduleName = node.moduleSpecifier.getText().slice(1, -1); // Remove quotes
// Extract the raw module specifier string (without quotes)
let moduleName = node.moduleSpecifier.getText().slice(1, -1);
// Resolve relative paths to absolute paths based on the file location
if (fileName && (moduleName.startsWith("./") || moduleName.startsWith("../"))) {
const dir = path.dirname(fileName);
const resolved = path.resolve(dir, moduleName);
// Use POSIX style for consistency (VS Code works with forward slashes)
moduleName = resolved.split(path.sep).join("/");
}
const importClause = node.importClause;

const declarations: ts.VariableDeclaration[] = [];

if (importClause) {
// Handle default imports
// Skip over 'import type' statements

if (importClause.isTypeOnly) {
return node;
}
Expand Down Expand Up @@ -41,9 +54,7 @@ export function importToGlobalTransformer(): ts.TransformerFactory<ts.SourceFile
if (ts.isNamedImports(namedBindings)) {
namedBindings.elements.forEach((el) => {
const name = el.name.getText();
const propertyName = el.propertyName
? el.propertyName.getText()
: name;
const propertyName = el.propertyName ? el.propertyName.getText() : name;
declarations.push(
ts.factory.createVariableDeclaration(
name,
Expand Down Expand Up @@ -80,10 +91,7 @@ export function importToGlobalTransformer(): ts.TransformerFactory<ts.SourceFile

return ts.factory.createVariableStatement(
undefined,
ts.factory.createVariableDeclarationList(
declarations,
ts.NodeFlags.Const
)
ts.factory.createVariableDeclarationList(declarations, ts.NodeFlags.Const)
);
}
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/misc/transpile-file/transpile-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export function transpileFile(fileName: string, content: string) {
},
transformers: {
before: [
importToGlobalTransformer(),
// Pass the file name so that relative imports can be resolved to absolute paths.
importToGlobalTransformer(fileName),
stripTypesTransformer(),
exportToGlobalTransformer(fileName),
],
Expand Down