TypeScript compiler: generated sources, root dirs, excludes and symlinks
Asked Answered
H

1

7

I have this setup of a TypeScript 2.0.3 project:

src/main/webapp/tsconfig.json
src/main/webapp/app -- contains .ts files
src/main/webapp/app/tsModels -- a symlink to ../../../../target/tsModels
target/tsModels/   -- contains .ts files

The goal is to get the .ts files in target/tsModels compiled.

First, with "rootDir": ".", tsc complained:

error TS6059: File '[...]/target/tsModels/AboutWindupModel.ts' is not under 'rootDir' '[...]/src/main/webapp'. 'rootDir' is expected to contain all source files.

So I tried

{
  "compilerOptions": {  ...,
    //"rootDir": ".",
    "rootDirs": [".", "../../../target/tsModels"],
  },
} 

Still the same. Tried:

{
  "compilerOptions": {
    //"rootDir": ".",
    "rootDirs": [".", "../../../target/tsModels"],
  },
  "exclude": [
    "node_modules",
    "target/**",
    "../../../target/**", 
    "../../../target/**/*",
    "../../../target/tsModels/*", 
    "typings/main",
    "typings/main.d.ts"
  ]
}

But still getting that error.

So it seems that tsc can't handle symlinks properly, always checks the canonical path.

So I thought, ok let's compile the tsModels files directly. So I removed the symlink and tried:

  "compilerOptions": {
    //"rootDir": ".",
    "rootDirs": [".", "../../../target/tsModels"],
  },

But this doesn't actually compile the files in the tsModels dir.
I have even tried

    "rootDirs": "../../../target/tsModels",

but tsc didn't compile that, instead, it compiled the .. Does it obey that at all?

What should I do to compile files from both dirs?
Is there a way to use a symlink leading to dir outside rootDir?

Full config with all the garbage I was trying:

{ "compilerOptions": { "target": "es5", "module": "system", "moduleResolution": "node", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, "noImplicitAny": false, //"rootDir": ".", //"rootDirs": [".", "../../../target/tsModels"], "rootDirs": ["../../../target/tsModels"], //"rootDir": "../../../target/tsModels", "outDir": "../../../target/windup-web", "sourceRoot": "/windup-web/" }, "exclude": [ "node_modules", //"target/**", //"../../../target/**", //"../../../target/**/*", //"../../../target/tsModels/*", //"target/tsModels/**", "typings/main", "typings/main.d.ts" ] }

Herbst answered 18/10, 2016 at 23:48 Comment(0)
S
4

I think the preserveSymlinks compiler option could help you:

Do not resolve symlinks to their real path; treat a symlinked file like a real one.

https://www.typescriptlang.org/docs/handbook/compiler-options.html

Example:

{
    "compilerOptions": {
        "preserveSymlinks": true
    }
}
Spleeny answered 18/11, 2018 at 12:34 Comment(4)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewHaldan
This is not a link-only answer. The important information - "use preserveSymlinks compiler option" - is in the answer text. The link by itself doesn't answer the question, it's a list of all compiler options and i added it just for reference.Spleeny
And in case you feel there wasn't enough information, i added an example.Spleeny
Actually I can fail with my review, this is natural. In SO several reviewers should agree before make a decision. And reviewer who fail recieves penalties. Just in my opinion SO is system to create content where "answer" is a unit of content (not thread as on other forums). Asnwer should be an intersting "story" :) do not worry to much if I have failed - other reviewers/moderators review me :) that how it work.Haldan

© 2022 - 2024 — McMap. All rights reserved.