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

@workshift/ts-transformer-imports

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@workshift/ts-transformer-imports

Transform import paths in TypeScript

  • 0.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

ts-transformer-imports

A TypeScript transformer which enables compilation of absolute imports (using baseUrl or paths) so they can be required as modules from Javascript or TypeScript, without additional configuration or path mapping.

Build Status NPM version Downloads

Read more about why this exists and what problem it solves

How to use this package

If you're using the TypeScript compiler and using TypeScript's native "absolute imports" mapping (specifying baseUrl or paths in your config), you've found none of the compiled code is usable in Javascript or unconfigured TypeScript.

ttypescript

First, install ttypescript:

npm install --save-dev ttypescript

See examples/ttypescript for detail, and ttypescript's README for how to set up in your project.

Now, specify ts-transfomer-imports in your tsconfig.json:

{
  "compilerOptions": {
    // ...
    "plugins": [
      { "transform": "ts-transformer-imports" }
    ]
  },
}

Using with ts-node, webpack, etc

See ttypescript's README for usage information with other compilers. It should work!

TypeScript API

You probably won't need this. See test for more detail.

const ts = require('typescript');
const importsTransformer = require('ts-transformer-imports').default;

const program = ts.createProgram([/* your files to compile */], {
  strict: true,
  noEmitOnError: true,
  target: ts.ScriptTarget.ES5
});

const transformers = {
  before: [importsTransformer(program)]
};
const { emitSkipped, diagnostics } = program.emit(undefined, undefined, undefined, false, transformers);

TL;DR:

I don't want to write:

// ugh, annoying on bigger projects
import isTruthy from '../../utils/isTruthy'

so TypeScript allows me write:

import isTruthy from 'isTruthy'

but tsc compiles it to:

const isTruthy = require('isTruthy')
// This makes NO SENSE to normal Javascript, or even downstream TypeScript!

So ts-transformer-import rewrites to this at compile time:

const isTruthy = require('../../utils/isTruthy')

Compatibility

TypeScript >= 2.4.1

License

MIT

Thanks to

Keywords

FAQs

Package last updated on 02 Oct 2019

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