3

I have two projects, which are using typescript. One is core project: moduleA, other one is business project: moduleB. ProjectB uses projectA's modules(ts files) located in /scripts folder. I want to specify module resolution root folder for projectB by projectA/scripts. Btw: Outs of the projects will be merged in production.

Here is my file structure

├───projectA
│   │   index.html
│   │   systemjs.config.js
│   │   tsconfig.json
│   │
│   └───scripts
│       └───core
│           │   app.ts
│           │   components.ts
│           │
│           └───components
│                   dropdown.ts
│
└───projectB
    │   tsconfig.json
    │
    └───scripts
        └───pages
                page.ts

For example: In projectB's page.ts I want to import core modules like below.

import { DropdownList } from 'core/components';

Actually I'm doing it that way. JavaScript output of page.ts works fine. But I need to fix typescript compile error: cannot find module "core/components".

I can do it by using npm install/link projectA/scripts in moduleB, or symlink. But I need to solve it by option of tsconfig.json or some kind of declaration file/references.d.ts/. Because, We have hundreds of projects kind of projectB. npm link or symlink will be awkward work for us.

Is there any easy way to do that?

1 Answer 1

3

Is there any easy way to do that

You need to use two things in your tsconfig.json :

  • rootDirs to tell that projectA/scripts and projectB/scripts are right next to each other. (WARNING: does your compile time environment support it?).
  • baseUrl if you want to use core/components instead of full relative paths ./core/components (WARNING: does your module loader support it?).

Both of these are support in http://alm.tools/ 🌹

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @basarat. I heard about it. That is exactly what I need. I'm using [email protected]. Which version of typescript support rootDirs and baseUrl ?. Official document of typescript compiler options doesn't contain these options.
For more information in the mean time while these features are not yet documented in the official documentation, see this issue: github.com/Microsoft/TypeScript/issues/5039

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.