typescript 如何确定tsc编译子目录的原因

4si2a6ki  于 2023-03-13  发布在  TypeScript
关注(0)|答案(1)|浏览(183)

我有一个项目是这样安排的:

project/
  client/
    src/
      client.ts
    tsconfig.client.json
  src/
    index.ts
  tsconfig.json

当我在project文件夹中运行tsc -b时,它也试图编译client子目录,现在我明白了,如果子文件夹中有tsconfig.json,那么--build将编译该文件夹,但在本例中,子目录的tsconfig被重命名。
结果,tsc输出错误:

error TS5083: Cannot read file 'project/client/tsconfig.json'.

如何跟踪tsc尝试加载此文件夹的原因?client未列在顶层tsconfig的references中,并且includerootDir选项设置为仅构建src
我尝试过--dry build和--traceResolution,但都没有提供有用的答案。

编辑以显示我的tsconfigs:

project/tsconfig.json

{
  "extends": "../../../tsconfig.base.json",
  "compilerOptions": {
    "outDir": "dist",
    "rootDir": "src",
    "target": "es6",
    "lib": ["es2019","dom"],
    "jsx": "react",
    "noUnusedLocals": false
  },
  "include": ["./src/**/*"],
  "references": [
    { "path": "shared/api" },
    { "path": "../../lib/logger" },
  ]
}

../../../tsconfig.base.json

{
    "compilerOptions": {
      "target": "es2020",
      "module": "commonjs",
      "moduleResolution": "node",
      "rootDir": "packages",
      "baseUrl": "./packages",
      "lib": ["es2019"],
  
      "strict": true,
      "noUnusedLocals": true,
      "noFallthroughCasesInSwitch": true,
      "noImplicitReturns": true,
      "esModuleInterop": true,
      "allowJs": true,
  
      "composite": true,
      "inlineSources": true,
      "sourceMap": true,
      "incremental": true,

      "skipLibCheck": true,
      "emitDeclarationOnly": true,
  
      "allowSyntheticDefaultImports": true,
      "experimentalDecorators": true,
  
      "newLine": "lf",
  
      "noEmitOnError": true,
    },
    "exclude": ["**/jest.config.js", "**/dist", "**/__mocks__/**/*", "**/coverage", "**/gulpfile.js"]
}

client/tsconfig.client.json-如果相关,因为tsc甚至没有使用此文件

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "types": [
      "webpack-env",
      "jest"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost",
      "webworker"
    ]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ],
  "references": [
    { "path": "../shared/api" },
    { "path": "../" }
  ]
}
qojgxg4l

qojgxg4l1#

我发现我正在使用npx运行tsc命令,因为我在monorepo中设置了工作区,所以子文件夹中有一个package.json,npm在子文件夹中拾取并第二次运行tsc命令。
将我的npx命令更新为npx --workspaces=false tsc -b解决了这个问题。

相关问题