Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tsc-esm-fix

Package Overview
Dependencies
Maintainers
1
Versions
77
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsc-esm-fix

Make tsc-compiled `es2020/esnext` bundles compatible with esm/mjs requirements

  • 2.4.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16K
increased by5.11%
Maintainers
1
Weekly downloads
 
Created
Source

tsc-esm-fix

Make tsc-compiled es2020/esnext bundles compatible with esm/mjs requirements

CI David Maintainability Test Coverage

Motivation

This workaround is aimed to bypass a pair of tsc and ts-jest issues right here and right now.

  • TS/13422 / TS/16577: tsc should add .js extensions for relative module paths.
  • ts-jest/1174: import.meta is not allowed.

Hope one day this library will not be needed.

Features

  • Finds and replaces __dirname and __filename refs with import.meta.
  • Injects extensions to imports/re-exports statements.
    • import {foo} from './foo'import {foo} from './foo.js'
    • import {baz} from 'external/baz'import {baz} from 'external/baz.js'
    • Pays attention to index files: import {bar} from './bar'import {bar} from './bar/index.js'
  • Follows outDir found in tsconfig.json.
  • Changes file extensions if specified by opts.
  • 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]
OptionDescriptionDefault
--tsconfigPath to project's ts-config(s)tsconfig.json
--targetEntry points where compiled files are placed for modificationIf not specified inherited from tsconfig.json compilerOptions.outDir
--dirnameVarReplace __dirname usages with import.metatrue
--filenameVarReplace __filename var references import.metatrue
--extAppend extension to relative imports/re-exports.js
--cwdcwdprocess.cwd()
--outOutput dir. Defaults to cwd, so files would be overwrittenprocess.cwd()
--debugPrints 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

Keywords

FAQs

Package last updated on 15 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc