tsc-esm-fix
Make tsc-compiled es2020/esnext
bundles compatible with esm/mjs requirements
Motivation
This workaround is aimed to bypass a pair of tsc and ts-jest issues right here and right now.
Hope one day this library will not be needed.
Features
- Injects extensions to imports/re-exports statements.
- Follows
outDir
found in tsconfig.json. - Finds and replaces
__dirname
and __filename
refs with import.meta
. - Changes file extensions (applied to local deps only).
- Supports Windows-based runtimes.
Requirements
Node.js >= 14
Install
yarn add -D tsc-esm-fix
Usage
tsc-esm-fix [options]
import { fix } from 'tsc-esm-fix'
await fix({
dirnameVar: true,
filenameVar: true,
ext: true
})
API
CLI
tsc-esm-fix [opts]
Option | Description | Default |
---|
--tsconfig | Path to project's ts-config(s) | tsconfig.json |
--target | Entry points where compiled files are placed for modification | If not specified inherited from tsconfig.json compilerOptions.outDir |
--dirnameVar | Replace __dirname usages with import.meta | true |
--filenameVar | Replace __filename var references import.meta | true |
--ext | Append extension to relative imports/re-exports | .js |
--cwd | cwd | process.cwd() |
--out | Output dir. Defaults to cwd , so files would be overwritten | process.cwd() |
--debug | Prints debug notes | |
JS/TS
import { fix, IFixOptions } from 'tsc-esm-fix'
const fixOptions: IFixOptions = {
tsconfig: 'tsconfig.build.json',
dirnameVar: true,
filenameVar: true,
ext: true
}
await fix(fixOptions)
export interface IFixOptions {
cwd: string
out?: string,
target?: string | string[]
tsconfig: string | string[]
dirnameVar: boolean
filenameVar: boolean
ext: boolean | string
}
Alternatives
License
MIT