Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared tsconfig.json and relative paths

My team is maintaining multiple packages and we are trying to create a shared tsconfig.json with common configuration for all of them. It contains our preferences about compile target, JSX, output directory and included files, for example:

{
  "compilerOptions": {
    "module": "esnext",
    "target": "es5",
    "lib": ["es5", "dom"],
    "jsx": "react",
    "outDir": "build/dist"
  },
  "include": ["./src/**/*.ts", "./src/**/*.tsx"]
}

We publish this file as a shared npm-module, like my-build-config and then use extends in every project:

{
   "extends": "./node_modules/my-build-config/tsconfig.json"
}

The issue here is that all paths in the shared config are relative to the file location. E.g. it will try to include ".ts" files not from the current project, but inside my-build-config. For example, instead of ./src/index.ts it looks for ./node_modules/my-build-config/src/index.ts.

Is there a way to share file locations, so they will not be repeated in 10+ packages? Maybe something similar what Jest does with rootDir property.

like image 898
just-boris Avatar asked Aug 28 '18 17:08

just-boris


People also ask

What should I put in Tsconfig json?

The tsconfig.json file specifies the root files and the compiler options required to compile the project. JavaScript projects can use a jsconfig.json file instead, which acts almost the same but has some JavaScript-related compiler flags enabled by default.

What is Lib in Tsconfig json?

TypeScript includes a default set of type definitions for built-in JS APIs (like Math ), as well as type definitions for things found in browser environments (like document ).

Does TS node use Tsconfig?

ts-node supports a variety of options which can be specified via tsconfig.


1 Answers

If you know my-build-config will always be installed at node_modules/my-build-config relative to each project without symlinks, you could just prepend ../../ to each affected path.

Alternatively, use a different tool to copy your tsconfig.json into all the projects, such as Braid (disclosure: I'm a Braid contributor).

like image 193
Matt McCutchen Avatar answered Nov 22 '22 10:11

Matt McCutchen