Environment specific tsconfig.json

Andree picture Andree · Oct 25, 2017 · Viewed 7k times · Source

Is there a way how to set tsconfig.json environmentally specific such as tsconfig.prod.json and tsconfig.dev.json?

I want to have sourceMap and comments on dev environment, but I want to remove these on production.

Answer

Ogglas picture Ogglas · May 19, 2018

You can use extends for this.

https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#configuration-inheritance-with-extends

Example: tsconfig.prod.json

{
  "extends": "./tsconfig",
  "compilerOptions": {
    "sourceMap": "false"
  }
}

If you use webpack and ts-loader you can then set it like this:

https://github.com/TypeStrong/ts-loader#configfile-string-defaulttsconfigjson

module: {
    rules: [
        // All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'.
        {
            test: /\.tsx?$/,
            use: [
                {
                    loader: 'babel-loader',
                    options: {
                        babelrc: false,
                        plugins: ['react-hot-loader/babel'],
                    },
                },
                {
                    loader: 'ts-loader',// (or awesome-typescript-loader)
                    options: {
                        configFile: 'tsconfig.prod.json'
                    },
                },

            ],
         },