typescript - 为了将 main 与测试编译分开,需要设置哪些正确的 typescript 编译器 (tsc) 选项?

标签 typescript tsc

注意:此问题的公共(public) git 存储库位于 https://github.com/matthewadams/ts-test如果您想查看完整的项目。

我试图在我的项目中将主要源代码编译与测试源编译分开。这是源目录结构:

src
  main
    ...typescript source files and possibly child directories with sources
  test
    unit
      ...unit test sources
    integration
      ...integration test sources

我的目标是只将主要源代码编译成 lib/main , 并且只测试源到 lib/test .

我正在尝试将主编译和测试编译应该通用的所有编译器选项放在 tsconfig.json 中。 , 然后使用命令行参数 tsc提供特定于每个编译的选项。这是我当前的 tsconfig.json :
{
  "compilerOptions": {
    "strict": true,
    "alwaysStrict": true,
    "diagnostics": true,
    "disableSizeLimit": true,
    "esModuleInterop": true,
    "extendedDiagnostics": true,
    "forceConsistentCasingInFileNames": true,
    "inlineSourceMap": true,
    "inlineSources": true,
    "listEmittedFiles": true,
    "listFiles": true,
    "module": "commonjs",
    "pretty": true,
    "target": "es2015"
  }
}

来自 package.json 的片段scripts部分如下(我以复杂性为由拒绝 Gulp、Grunt 等,故意牺牲可移植性):
  "scripts": {
    "transpile-main": "rm -rf lib/main && tsc --outDir lib/main --rootDir src/main -d --declarationDir lib/main",
    "transpile-test": "rm -rf lib/test && tsc --outDir lib/test --rootDir src/test --typeRoots lib/main",
    ...other scripts here...
  }

我可以毫无问题地编译主要源代码,它们出现在 lib/main正确。但是,当我编译测试源时,出现以下错误:
$ npm run transpile-test

> @matthewadams/ts-test@0.1.0-pre.0 transpile-test /Users/matthewadams/dev/me/ts-test
> rm -rf lib/test && tsc --outDir lib/test --rootDir src/test --typeRoots src/main

error TS6059: File '/Users/matthewadams/dev/me/ts-test/src/main/Nameable.ts' is not under 'rootDir' 'src/test'. 'rootDir' is expected to contain all source files.

error TS6059: File '/Users/matthewadams/dev/me/ts-test/src/main/Person.ts' is not under 'rootDir' 'src/test'. 'rootDir' is expected to contain all source files.

error TS6059: File '/Users/matthewadams/dev/me/ts-test/src/main/PersonImpl.ts' is not under 'rootDir' 'src/test'. 'rootDir' is expected to contain all source files.

让我困惑的是消息 'rootDir' is expected to contain all source files.我正在尝试根据 lib/main 中的内容编译测试源。 .我不希望所有资源都在一个目录下。
tsconfig.json 的正确组合是什么选项和 tsc cli 选项来实现我的单独主要和测试编译的目标?

最佳答案

错误:“rootDir”应包含所有源文件。rootDir预计将是所有源/输入文件的根。 TS 编译器是这样进行的:

collect all input files ("files" / "include" / "exclude" / imports) 
  --> for each included input file
    --> chop off the "rootDir" from the input 
    --> prepend the "outDir"
src/test文件从 src/main 导入, 所以目前 rootDir只能设置为 src或以上。
解决方案:项目引用(TS 3.0+)
Project references可以解决您描述的所有问题:
  • rootDir将设置为 src/mainsrc/test分别
  • src文件夹不显示为 lib 的一部分输出
  • src/main无法导入 src/test (反之亦然)

  • 生成的项目结构:
    |   tsconfig-base.json            // other config options go here
    |   tsconfig.json                 // solution config containing references
    |   
    +---lib                           // output folder
    |   +---main
    |   |       mod.d.ts
    |   |       mod.js
    |   |       tsconfig.tsbuildinfo
    |   |       
    |   \---test
    |           mod-test.js     
    \---src
        +---main                      // referenced sub-project main 
        |       mod.ts
        |       tsconfig.json
        |       
        \---test                      // referenced sub-project test 
                mod-test.ts
                tsconfig.json
    
    ./tsconfig-base.json:
    {
      "compilerOptions": {
        // just some sample values, set your own as needed
        "target": "ESNext",
        "module": "ESNext",
        "moduleResolution": "node",
        "strict": true,
        "esModuleInterop": true,
        // other compiler options
      }
    }
    
    ./tsconfig.json:
    {
      "files": [],
      "references": [
        {
          "path": "./src/main"
        },
        {
          "path": "./src/test"
        }
      ]
    }
    
    ./src/main/mod.ts:
    export const foo = "foo";
    
    ./src/main/tsconfig.json:
    {
      "extends": "../../tsconfig-base.json",
      "compilerOptions": {
        "outDir": "../../lib/main",
        "composite": true // flag to signal referenced project      
      }
    }
    
    ./src/test/mod-test.ts:
    import { foo } from "../main/mod";
    
    // use your test framework here
    export function testMod() {
      if (foo !== "foo") throw new Error("foo expected.");
    }
    
    testMod()
    
    ./src/test/tsconfig.json:
    {
      "extends": "../../tsconfig-base.json",
      "compilerOptions": {
        "outDir": "../../lib/test"
      },
      "references": [
        {
          "path": "../main" // test project depends on main
        }
      ]
    }
    
    您可以使用以下 scripts 构建和清理项目:
    "scripts": {
        "build": "tsc -b -v",
        "clean": "tsc -b --clean"
    },
    
    更多链接
  • TypeScript Project References Demo
  • 关于typescript - 为了将 main 与测试编译分开,需要设置哪些正确的 typescript 编译器 (tsc) 选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57257910/

    相关文章:

    angular - 将 typescript 项目编译为本地模块

    c# - 是否有等同于 C# 属性的 TypeScript

    typescript - TypeScript 中的 IDisposable/RAII?

    typescript - 允许没有未使用的变量规则的 typescript 编译

    typescript /karma : Making CommonJS available to Karma during test runs

    typescript - 我可以使用 typescript 在生成的 javascript 中添加额外的评论吗?

    javascript - 我如何将 ts 代码编译为 'require([' somemodule'])

    reactjs - 如何将 Relay 查询从 TypeScript 转换为 ES5?

    Typescript "error TS2532: Object is possibly ' undefined'"即使在未定义检查之后

    typescript - 如何为 SystemJS 导出 TypeScript 模块?