【问题标题】:How to exclude specific files in typescript only for the build?如何仅在构建中排除打字稿中的特定文件?
【发布时间】:2020-02-16 02:06:58
【问题描述】:

是否可以只为构建排除所有测试文件,但将它们与 nodemon 一起使用以在本地运行测试?当我在 tsconfig.json 文件中排除测试文件时,我收到一个打字稿错误,在我的情况下它找不到测试库的类型,例如 jest。

Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`.ts(2582)

{
  "compilerOptions": {},
  "exclude": [
    "**/*.test.ts"
  ]
}

我想知道,因为我猜临时转译被放到了构建文件夹之外的另一个文件夹中。

【问题讨论】:

    标签: javascript typescript jestjs tsconfig


    【解决方案1】:

    一种可能的解决方案是使用两个不同的 tsconfig 文件,一个用于测试,一个用于生产构建。

    tsconfig.json

    {
      "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "outDir": "./build",
        "baseUrl": ".",
        "paths": {
          "*": ["types/*"]
        },
        "strict": true,
      }
    }
    

    tsconfig.prod.json

    {
      "extends": "./tsconfig",
      "exclude": ["**/*.test.ts", "**/*.mock.ts"]
    }
    

    【讨论】:

    • 并使用新配置tsc -p tsconfig.prod.json
    猜你喜欢
    • 2018-01-06
    • 2016-03-01
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 2019-02-25
    • 2011-07-08
    相关资源
    最近更新 更多