Skip to content

Transformer cannot be used programmatically #259

Open
@asnaeb

Description

@asnaeb

I'm trying to use this transformer along with other transformers in a plugin but I couldn't seem to be able to make it work. I'm using ts-patch and other transformers (typia) work. This is how I'm plugging it in

// ./path/to/file.ts
import {Something} from "@/my-module.ts";
import _tsTransformPaths from "typescript-transform-paths";
import ts from "typescript";
import fs from "fs";

const tsTransformPaths = _tsTransformPaths as unknown as typeof import("typescript-transform-paths").default;

const fileName = "./path/to/file.ts";
const source = fs.readFileSync(fileName, "utf8");

const compilerOptions = {
  paths: {
    "@/*": ["./*"]
  },
  target: ts.ScriptTarget.ES2020
};

const mySourceFile = ts.createSourceFile(fileName, source, compilerOptions.target);

const {transformed: [transformedSource]} = ts.transform(
  mySourceFile,
  [
    tsTransformPaths(
      undefined,
      undefined,
      undefined,
      {compilerOptions, fileNames: [mySourceFile.fileName]},
    )
  ],
  compilerOptions
);

ts.createPrinter().printFile(transformedSource)) === source // true

The transformer doesn't seem to do anything to the input. Am I doing something wrong?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions