Create source map from typescript api transformation

Viewed 83

I'm using typescript API to create a custom syntax file preprocessor, just like svelte and vue do.

So, I input my custom file with a custom syntax and use ts.transform function to walk through the AST tree and change what I need to change.

But what I didn't find in ts docs is, how can I generate source maps from my input file string to the output result string?

const sourceFile = ts.createSourceFile(
  "index.ts",
  fileContent, // file input string
  ts.ScriptTarget.ESNext,
  false,
  ts.ScriptKind.TSX
);
const result = ts.transform(sourceFile, [transformer]); // my custom transformer function
const resultStr = printer.printFile(result.transformed[0]); // the output file string
// how to get sourcemaps for "resultStr"?
1 Answers
Related