2

I have multiple TypeScript projects where I separated the source code from the tests to their directories (src and test). In the final package I would like to include only the source code without the tests since the runtime does not need to know anything about the test files, fixtures, etc.

If I have the following settings in the tsconfig.json:

{
  "compilerOptions": {
    // compiler options
    "outDir": "dist"
  },
  "include": ["src"]
}

And these in the package.json:

{
  // usual settings
  "main": "dist/index.js",
  "files": [
    "dist/**/*.js",
    "dist/**/*.d.ts"
  ]
}

Then the final package only includes the source code in the desired directory structure, which is what I want to see, but my environment is having issues. I am using Doom Emacs and for the tests tide throws errors like this:

Error from syntax checker typescript-tide: Error processing request. No Project.
Error: No Project.
    at Object.ThrowNoProject (/Users/ikaraszi/.../node_modules/typescript/lib/tsserver.js:152133:23)

If I change the tsconfig.json settings to include the test directory, then the tide errors go away:

{
  "compilerOptions": {
    // compiler options
    "outDir": "dist",
    "rootDirs": ["src", "test"]
  }
}

But then the directory structure for the distribution changes and there will be a dist/src and dist/test that I would like to avoid since then the users of my libraries would need to use strange import statements:

import { foo } from 'library/dist/src/foo';

I would like to avoid the extra src if possible, the dist is ugly enough but that is given.

I tried with multiple settings change the include property to have src and test, but the builds ends up in the dist directory with the same nested structure:

{
  "compilerOptions": {
    // compiler options
    "outDir": "dist"
  },
  "include": ["src", "test"]
}

I also tried to play with the package.json settings without any luck. Is there anything I can do without adding an extra step to the build process to remove the unnecessary extra directories?

2
  • 2
    Often you want different types in the tests, too, so you have a second tsconfig.test.json that extends tsconfig.json and brings in the extra types and files. I'm not familiar with tide (or emacs!), though, and a quick scan of the docs doesn't seem to indicate you can configure it to look at anything other than tsconfig.json. Commented Mar 13, 2021 at 10:48
  • This solution could work if I would switch it. I would have a tsconfig.build.json with the build settings and a tsconfig.json where I include both the source code and the tests. Commented Mar 13, 2021 at 11:01

1 Answer 1

3

Based on @jonrsharpe's comment I ended up with two tsconfig files:

tsconfig.json:

{
  "extends": "@tsconfig/node14/tsconfig.json",
  "compilerOptions": {
    "declaration": true,
    "outDir": "dist",
    "sourceMap": false,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictBindCallApply": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "allowSyntheticDefaultImports": true,
    "noUnusedLocals": false,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true
  },
  "include": ["src", "test"]
}

And a tsconfig.build.json:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "target": "es2018",
    "noUnusedLocals": true
  },
  "include": ["src"]
}

And in the package.json:

{
  // usual settings
  "main": "dist/index.js",
  "files": [
    "dist/**/*.js",
    "dist/**/*.d.ts"
  ],
  "scripts": {
    "build": "tsc --build tsconfig.build.json",
    // other scripts
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.