'Tsconfig: Source code root folder not carried over to output folder

I've built countless Node.JS projects with TypeScript where the source code is in a folder, e.g. src or lib, which is carried over into the transpiled output directory so that lib/foo.ts becomes build/lib/foo.js, not build/foo.js. Usually baseUrl or rootUrl ​is required to prevent this behaviour, but now I have a project where the lib folder disappears from the build directory with a very minimal tsconfig.json:

{
 ​"compilerOptions": {
   ​"incremental": true,
   ​"target": "es2017",
   ​"module": "CommonJS",
   ​"lib": [
     ​"es2019",
     ​"dom"
   ​],
   ​"allowJs": true,
   ​"sourceMap": true,
   ​"outDir": "build",
   ​"strict": true,
   ​"moduleResolution": "node",
   ​"esModuleInterop": true,
   ​"resolveJsonModule": false,
   ​"forceConsistentCasingInFileNames": true
 ​},
 ​"include": [
   ​"lib"
 ​]
}

And with a file lib/foo/bar.ts and calling tsc (TS 4.5.4), I get the following emitted output:

build/foo/bar.js

instead of the expected

build/lib/foo/bar.js.

What could be causing this? My tsconfig.json is literally copied from another project with the same setup, where lib/** is transpiled into build/lib/**.

I've now found two ways around this:

  1. Set outDir to build/lib. This doesn't feel right since the same config now gives different results in two projects and I don't understand why. I'm concerned that some change may as a side-effect later cause code to be emitted to build/lib/lib with this config.

  2. Add rootDir: ".". This solves the problem, but I've never needed this before.

What could be different in this project, causing the behaviour without rootDir to differ from other projects with seemingly the same directory structure?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source