typescript-transform-extensions
v1.0.1
Published
Transforms import and export extensions for compatibility with native ES Modules
Downloads
109
Maintainers
Readme
typescript-transform-extensions
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
Install
npm:
npm i -D typescript-transform-extensions
yarn:
yarn add -D typescript-transform-extensions
Example Config
Add it to plugins in your tsconfig.json
{
"compilerOptions": {
"plugins": [
{
"transform": "typescript-transform-extensions",
"extensions": {".ts": ".js"}
}
]
}
}
Example output
- 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);
Contributting
Contributions are welcome!
- make sure to format code with prettier:
npm run format