Ilja Denisovs
Ilja Denisovs

Reputation: 316

TypeScript compiler is not emitting imported modules

I have the project (on GitHub), which uses the Project References functionality of TypeScript compiler, so it have the root tsconfig.json file and the tsconfig.json file of subfolder named shared.

Shared subfolder have 2 files:

The main index.ts file imports the User module, like

import User from './shared/User';

Transpiled files is placed under the target folder. The issue I met is that as result of tsc the target folder has Point .d.ts and .js files (not imported by index.ts) and doesn't contains the User files (imported by index.ts).

The output of tsc -b --listEmittedFiles:

TSFILE: /home/user/Projects/test/target/index.js
TSFILE: /home/user/Projects/test/target/index.d.ts
TSFILE: /home/user/Projects/test/target/shared/Point.js
TSFILE: /home/user/Projects/test/target/shared/Point.d.ts
TSFILE: /home/user/Projects/test/target/shared/User.js
TSFILE: /home/user/Projects/test/target/shared/User.d.ts

If I remove the import expression from index.ts, the target folder will have both User and Point scripts, as expected.

My question is: Is it bug of tsc or I'm doing something wrong?

Tested on both 3.3 and 3.4 versions of tsc, with same result.

The root tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./target",
    "rootDir": "./source",
    "strict": true,
    "esModuleInterop": true
  },
  "references": [
    {
      "path": "./source/shared"
    }
  ]
}

The tsconfig.json of shared folder:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": true,
    "composite": true,
    "strict": true,
    "esModuleInterop": true
  }
}

Upvotes: 0

Views: 1354

Answers (1)

Ilja Denisovs
Ilja Denisovs

Reputation: 316

Finally, I got the solution by myself!

tscconfig.json files of subprojects should point output to the target/ directory of main project!

So, let say, you have general project with source/ directory defined as rootDir and target/ directory as outDir.

In the source directory you have two subprojects: core and geometry (as it shown here).

So, both of them should have outDir set to the main target/ directory, like that: ../../target/core, ../../target/geometry.

Or, use configuration inheritance with "extends" property. In this case the config files of subprojects may be rewritten like that (example):

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "composite": true
  }
}

Upvotes: 2

Related Questions