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.
Read more about why this exists and what problem it solves
TypeScript >= 2.4.1
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.
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" }
]
},
// ...
}
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'));
}
MIT