How to substitute aliased paths with real ones in generated typescript code?

时间:2016-10-20 13:15:14

标签: typescript typescript2.0

Say, you have following file structure: . ├── package.json ├── src │   └── app.ts ├── test │   └── test.ts └── tsconfig.json

In test.ts we have:

import {App} from 'project/app';
new App().ping();

tsconfig.json config is following:

{
    "compilerOptions": {
        "module": "commonjs",
        "noLib":false,
        "target": "es6",
        "sourceMap": true,
        "outDir": "target/build",
        "baseUrl": "./",
        "moduleResolution": "node",
        "paths": {
            "project/*": ["./src/*"]
        }
    },
    "include": [
        "test/**/*.ts"
    ]
}

This will be compiled without error and we'll get

target/
└── build
    ├── src
    │   ├── app.js
    │   └── app.js.map
    └── test
        ├── test.js
        └── test.js.map

But in generated test.js will still have

const app_1 = require('project/app');

And we get an error if we'll try to run this code since there's no such thing as project folder.

The question is: What is the best way to have aliases in typescript that are rewritten in generated code respectively. If it is impossible to achieve via tsc exclusively, what will be the best approach to rewrite paths in generated js files?

My solution: Introduce second tsconfig file to compile src files into target/build/node_modules/project folder. This works but it actually means that we compile same set of files twice.

Also, this won't help is say I want to have path aliases in src code as well.

0 个答案:

没有答案