Skip to content
/ tsfix Public
generated from 2BAD/ts-lib-starter

Fix import statements in files generated by TypeScript compiler.

License

Notifications You must be signed in to change notification settings

2BAD/tsfix

Repository files navigation

TSFIX

NPM version License GitHub Build Status Code coverage Written in TypeScript

This tool ensures that your compiled JavaScript files are valid ESM by fixing extensions in the output files.

Features

  • Automatically detects typescript configuration and paths.
  • Handles all types of imports (absolute, relative, internal, dynamic etc.)

Install

npm install --save-dev @2bad/tsfix

Usage

You can use tsfix as a post-build script in your project. Add the following script to your package.json:

{
  "scripts": {
    "build": "tsc",
    "postbuild": "tsfix"
  }
}

This will run tsfix automatically after your TypeScript compilation. Alternatively, you can run tsfix manually:

npx @2bad/tsfix

Example

Consider the following TypeScript files:

// src/main.ts
import { helper } from './utils/helper.ts'

// src/utils/helper.ts
export const helper = () => 'helper function'

After running tsc, the compiled JavaScript files might look like this:

// dist/main.js
import { helper } from './utils/helper.ts'

// dist/utils/helper.js
export const helper = () => 'helper function'

Running tsfix will transform the import paths to:

// dist/main.js
import { helper } from './utils/helper.js'

Troubleshooting

The DEBUG environment variable can be used to figure out why it's working as you may have expected.

DEBUG=* tsfix

Contributing

We welcome contributions! If you find a bug or want to request a new feature, please open an issue. If you want to submit a bug fix or new feature, please open a pull request.