Alexandre-Herve / ts-transformer-imports

Transforms your TypeScript absolute imports to be relative

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ts-transformer-imports

A TypeScript transformer which enables transformation of absolute imports (using baseUrl or paths) to relative ones, so they can be required from other projects or regular ol' node.

NPM version Downloads

Read more about why this exists and what problem it solves

Requirement

TypeScript >= 2.4.1

How to use this package

How to use the custom transformer

Unfortunately, TypeScript itself does not currently provide any easy way to use custom transformers (See microsoft/TypeScript#14419). The followings are the example usage of the custom transformer.

ttypescript

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

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

TypeScript API

See test for detail. You can try it with $ npm test.

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: [],
  after: [importsTransformer(program)]
};
const { emitSkipped, diagnostics } = program.emit(undefined, undefined, undefined, false, transformers);

if (emitSkipped) {
  throw new Error(diagnostics.map(diagnostic => diagnostic.messageText).join('\n'));
}

License

MIT

Thanks to

About

Transforms your TypeScript absolute imports to be relative

License:MIT License


Languages

Language:TypeScript 100.0%