Transforms imports to include the file extension of the resolved import, necessary for ES Modules.
This TypeScript plugin is helpful along with typescript-transform-paths. This plugin was inspired by typescript-transform-paths, thank you for all of your hard work!
Usable with ttypescript or ts-patch
npm:
npm i -D typescript-transform-extensions
yarn:
yarn add -D typescript-transform-extensions
Add it to plugins in your tsconfig.json
{
"compilerOptions": {
"plugins": [
{
"transform": "typescript-transform-extensions",
"extensions": {".ts": ".js"}
}
]
}
}
- dest/
- path/
- to/
-import/
- file.js
- subdir/
- main.js
- src/
- path/
- to/
-import/
- file.ts
- subdir/
- main.ts
src/path/to/import/file.ts
export function sum2(first: number, second: number) {
return first + second;
}
src/subdir/main.ts
import {sum2} from "../path/to/import/file";
sum2(2, 3);
Gets compiled to:
If
compilerOptions.module
is "CommonJS"
dest/subdir/main.js
var sum_1 = require("../path/to/import/file.js");
sum_1.sum2(2, 3);
If
compilerOpions.module
is "ES2015", "ES2020", "ES6", or "ESNext"
dest/subdir/main.js
import {sum2} from "../path/to/import/file.js";
sum2(2, 3);
Contributions are welcome!
- make sure to format code with prettier:
npm run format