Transform env variables during build time with typescript

Viewed 1143

Trying to find a clean way to statically transform process.env environment variables using tsc. Basically I'm trying to achieve what babel-plugin-transform-inline-environment-variables does using the typescript complier.

Most issues on the web involve getting the compiler to behave with process.env and other solutions recommend tools like dotenv to store env vars for later use.

This is particularly useful when environment variables do not contain sensitive information. Example:

export const VERSION = process.env.VERSION;

Build:

VERSION='1.2.3' npx tsc --project tsconfig.json

Desired output:

export const VERSION = '1.2.3';
2 Answers
Related