Skip to main content

Change Named Specifier

Change the specifier of the import module and the identifier that uses it. but local variable is not converted. only convert global value

import { sum } from "utils"

console.log(sum(2,1))

const testFn = () => {
const sum = (a,b,c) => { return a + b + c }
console.log(sum(2,1,0))
}

Options

type Options = {
fromSpecifier: string;
toSpecifier: string;
source: string;
sourceType?: "absolute" | "relative";
};
  • from: change target specifier
  • to: changed target specifier
  • source: change target source
  • sourceType: change target source's type. you can choose absolute or relative

Usage

The usage method differs depending on the source type of the import statement to be converted.

absolute

This is a situation where the source of the import module you want to change is an absolute path.

option.ts
const option = {
source: "utils",
sourceType: "absolute",
fromSpecifier: "sum",
toSpecifier: "calculateSum",
};
import { sum } from "utils"

console.log(sum(2,1))

relative

This is a situation where the source of the import module you want to change is an absolute path.

option.ts
const option = {
source: path.join(process.cwd(), "./utils.ts"),
sourceType: "relative",
fromSpecifier: "sum",
toSpecifier: "calculateSum",
};
import { sum } from  "./utils.ts"

console.log(sum())