Sinane
Sinane

Reputation: 2010

Can i exclude files dynamically from ts compiler?

I'm setting up a node/typescript server to build a real time application. I have my server and my client on the same folder.

What i want to do is to exclude "src/client" from the typescript compiler when i run the "server:dev" script end exclude "src/server" when i run "client:dev".

I already tried to find a way to exclude files from command line, but i didn't find solutions for that.

that's how my tsconfig.json look like

{
  "compilerOptions": {
    "target": "es6",                          
    "module": "commonjs",                     
    "lib": ["dom","es2017"],                  
    "sourceMap": true,
    "outDir": "dist",                         
    "strict": true,                           
    "noImplicitAny": true,                     
    "moduleResolution": "node",                  
    "esModuleInterop": true,                     
    "resolveJsonModule": true                 
  },
  "exclude": [
      "src/client"
  ]
}

but i need to include "src/client" and exclude "src/server" when i run the client.

Upvotes: 6

Views: 6257

Answers (2)

t7yang
t7yang

Reputation: 754

tsconfig.json support extends field, in your case, you should put the common config in a base tsconfig.json, then create tsconfig with extends for client and server respectively.

// tsconfig.json
{
  "compilerOptions": {
    ...               
  }
}

// tsconfig.client.json
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    ...               
  },
  "exclude": ["src/server"]
}

// tsconfig.server.json
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    ...               
  },
  "exclude": ["src/client"]
}

For npm scripts:

// package.json
{
  "scripts": {
    "server:dev": "tsc --build tsconfig.server.json",
    "client:dev": "tsc --build tsconfig.client.json",
  }
}

Upvotes: 10

Yilmaz
Yilmaz

Reputation: 49709

The general approach to change the compiler options dynamically:

since fs module can be used in node.js, you could add this util function to your node.j sproject:

const fs = require("fs")
const prettier = require("prettier")

function changeTs(){
    // this guarantees that I am on server
    if(typeof window === "undefined"){
      // process.cwd returns base folder, current working directory
      // whatever path is
      const tsPath = path.join(process.cwd(), "tsconfig.json")
      const tsConfig = require(tsPath)
      tsConfig.compilerOptions.exclude = ["src/client"]
       fs.writeFileSync(
           tsPath,
           prettier.format(
           JSON.stringify(tsConfig), { parser: "json" }
             )
           )
    
      }  
  }

call the above function, top-level when node.js app loads, so it tsconfig.json will be updated. default setting should be "exclude": ["src/server"]. When you are on server, it will be changed to "exclude": ["src/client"]

With this approach, you can even pass an argument to the function, so you can change the ts compiler options based on a specific setting that your app needs.

Upvotes: 0

Related Questions