npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fix-tsc-es-imports

v0.1.6

Published

fix-tsc-es-imports uses shelljs sed to fix default extensionless typescript ECMAScript compiled code relative imports and exports, properly adding .js extensions.

Downloads

1,318

Readme

fix-tsc-es-imports

fix-tsc-es-imports uses ShellJS sed to fix default extensionless typescript ECMAScript compiled code relative imports and exports, properly adding .js extensions.

Installing

npm install --save-dev fix-tsc-es-imports

Running

npx fix-tsc-es-imports

Or add it to your package.json module build script after your tsc run like this:

{
  "scripts": {
    "clean": "shx rm -rf lib",
    "tsc": "tsc --listEmittedFiles",
    "build": "npm run clean && npm run tsc && fix-tsc-es-imports -y -V"
  }
}

How it works

fix-tsc-es-imports looks for every .js file at the compileOptions.outDir folder found on the default tsconfig.json or another provided .json config file. Then it fixes all extensionless typescript relative imports and exports, adding .js extensions to them.

Usage

Usage:

fix_tsc_imports [-h|--help] [-y] [alternative_tsconfig.json]

  -h --help     usage info
  -y --yes      ignore confirmation and proceed straight away
  -V --verbose  verbose, outputs sed changed strings
  -d --dry      dry run, do not change anything and output sed changed strings (implied -V and -y)

An alternative tsconfig.json can be provided. It must have a .json extension. For example:

fix_tsc_imports -y ./dev/dev_tsconfig.json

See test/tsconfig_sample.json file.

Safe measures

fix-tsc-es-imports does a few safe checks to avoid touching the wrong code. It will check if outDir is a subfolder of the current folder and will not accept this folder names: src, node_modules, app.

Why do we need fix-tsc-es-imports?

Because when compiling ES6 modules with the current Typescript Compiler it generates .js files from .ts and .tsx files without properly fixing the imports expected from a ECMAScript module.

In face of that situation, our options are:

  1. Forget about TSC and use Babel, losing type checking. Undesirable.
  2. Keep TSC type checking and types generation and use Babel alongside to build our modules. Slower and mostly unnecessary.
  3. Use Webpack, Babel and TSC altogether to produce a compiled packed module. We do not need to do that because modules will mostly certainly be included in another package that will be transpiled and packed down the development chain. So, also unnecessary.
  4. Config Prettier to not complain about imports with extensions and manually review all our imports to have a fake .js extension while the source code has a .ts or .tsx extension. That does not seem to be the smartest option.
  5. Use the .mjs and .cjs files extensions. Very overwhelming.
  6. Use TSC to compile our Typescript code to ES6 or another module code, and generate maps and types, and got that module code properly imported in our other projects. That is mostly certain the best way to go.

So, this small script was built to add a .js extension to every extensionless Typescript generated import found in the compiled code. As current tsc compiler versions does not do that.

References

License

MIT. See LICENSE.md file.

Credits

I borrowed the RegEx proposed by wesbos here. And borrowed the subfolder verification code from Ilya Kozhevnikov here. Thank you.