Swetank
Swetank

Reputation: 141

Is it possible to add environment variables to a tsconfig.json file?

I have a tsconfig.json file containing

{
  "extends": "../../zlux-app-manager/virtual-desktop/plugin-config/tsconfig.base.json",
  "include": [
    "src/**/*.ts",
    "../../zlux-platform/interface/src/mvd-hosting.d.ts"
  ],
  "compilerOptions": {
    "skipLibCheck": true
  }
}

I want to replace ../../ with an environment variable. What are the possible solutions?

Upvotes: 5

Views: 9855

Answers (1)

frunkad
frunkad

Reputation: 2543

tsconfig.json is a simple JSON document lacking any way to refer to environment variables. You can check all possible options in the tsconfig Schema.

This does not restrict your use-case though. You can use a data templating language such as Jsonnet. It is a 20% project that can generate dynamic json (or other notation formats). You can use it to generate secondary tsconfig.json file and pass it as arguments to tsc.

Or

Just use Python/JS/Any programming language. At the end of the day you need to produce a json file to tsc, generate it! The following Python code with the below script runs perfectly.

import os, json
r ={
  "extends": os.environ['FOO'] + "/zlux-app-manager/virtual-desktop/plugin-config/tsconfig.base.json",
  "include": [
    "src/**/*.ts",
    os.environ['FOO'] + "/zlux-platform/interface/src/mvd-hosting.d.ts"
  ],
  "compilerOptions": {
    "skipLibCheck": True
  }
}
print(json.dumps(r))

Bash Command

$ FOO=mydir python3 tsconfig.py > tsconfig.json && tsc tsconfig.json

Upvotes: 1

Related Questions