我有一个相当简单的单次回购。它可以在GitLab 这里上使用。这使用纱线工作区,TypeScript,Jest,ts-jest
和ESLint与eslint-plugin-import
。
我正在尝试使用TypeScript正确地构建项目包。以前,我只是将TypeScript文件和它们的JavaScript代码一起发布在同一个目录中。
我构建该项目的尝试在GitLab合并请求这里中可用。
现在,存储库遵循以下布局:
|- example/
| |- index.ts
| |- package.json
| `- tsconfig.json
|- packages/
| |- koas-core/
| | |- src/
| | | `- index.ts
| | |- package.json
| | `- tsconfig.json
| |- koas-status-code/
| | |- src/
| | | `- index.ts
| | |- package.json
| | `- tsconfig.json
| `- more similar workspaces…
|- package.json
|- tsconfig.json
`- more configuration files…
有些包裹相互依赖。我已经设法让Jest测试和eslint-plugin-import
使用这个设置,但是我在构建这个项目时遇到了麻烦。
根tsconfig.json
如下所示:
{
"compilerOptions": {
"baseUrl": ".",
"composite": true,
"declaration": true,
"module": "commonjs",
"noEmit": true,
"resolveJsonModule": true,
"target": "esnext",
"paths": {
"koas-*": ["packages/koas-*/src"]
}
},
"exclude": ["**/*.test.ts"]
}
工作区tsconfig.json
文件如下所示:
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib"
}
}
每个工作区都有一个在prepack
中定义的package.json
脚本,如下所示:
{
"scripts": {
"prepack": "tsc --noEmit false"
}
}
main
字段引用lib
。
如果运行yarn workspace koas-status-code prepack
,将得到以下错误:
$ tsc --noEmit false
error TS6059: File 'koas/packages/koas-core/src/SchemaValidationError.ts' is not under 'rootDir' 'koas/packages/koas-status-code'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/SchemaValidationError.ts' is not under 'rootDir' 'koas/packages/koas-status-code/src'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/createDefaultValidator.ts' is not under 'rootDir' 'koas/packages/koas-status-code'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/createDefaultValidator.ts' is not under 'rootDir' 'koas/packages/koas-status-code/src'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/createMatcher.ts' is not under 'rootDir' 'koas/packages/koas-status-code'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/createMatcher.ts' is not under 'rootDir' 'koas/packages/koas-status-code/src'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/index.ts' is not under 'rootDir' 'koas/packages/koas-status-code'. 'rootDir' is expected to contain all source files.
error TS6059: File 'koas/packages/koas-core/src/index.ts' is not under 'rootDir' 'koas/packages/koas-status-code/src'. 'rootDir' is expected to contain all source files.
error TS6307: File 'koas/packages/koas-core/src/SchemaValidationError.ts' is not listed within the file list of project 'koas/packages/koas-status-code/tsconfig.json'. Projects must list all files or use an 'include' pattern.
error TS6307: File 'koas/packages/koas-core/src/createDefaultValidator.ts' is not listed within the file list of project 'koas/packages/koas-status-code/tsconfig.json'. Projects must list all files or use an 'include' pattern.
error TS6307: File 'koas/packages/koas-core/src/createMatcher.ts' is not listed within the file list of project 'koas/packages/koas-status-code/tsconfig.json'. Projects must list all files or use an 'include' pattern.
error TS6307: File 'koas/packages/koas-core/src/index.ts' is not listed within the file list of project 'koas/packages/koas-status-code/tsconfig.json'. Projects must list all files or use an 'include' pattern.
Found 12 errors.
我也尝试过这个tsconfig.json
for koas-status-code
{
"extends": "../../tsconfig.json",
"include": ["src"],
"references": [{ "path": "../koas-core" }],
"compilerOptions": {
"outDir": "lib"
}
}
这将生成工作区,但仍然会给出以下错误:
$ tsc --noEmit false
src/index.ts:1:23 - error TS6305: Output file '/home/remco/Projects/koas/packages/koas-core/lib/src/index.d.ts' has not been built from source file '/home/remco/Projects/koas/packages/koas-core/src/index.ts'.
1 import * as Koas from 'koas-core';
~~~~~~~~~~~
Found 1 error.
我该怎么解决这个问题?
发布于 2019-11-04 10:41:36
我已经找到了基于https://github.com/NiGhTTraX/lerna-ts的这个问题的解决方案。
该项目现在在项目根目录中包含一个名为tsconfig.build.ts
的文件。这个文件基本上只是指定编译器选项,测试和构建文件应该被排除在构建过程之外。
{
"compilerOptions": {
"declaration": true,
"module": "commonjs",
"resolveJsonModule": true,
"target": "esnext"
},
"exclude": ["**/*.test.ts", "**/lib"]
}
根目录中的另一个文件是tsconfig.json
。此文件用于在构建过程之外进行类型检查,例如Jest和VSCode。此文件扩展了构建配置。它通过覆盖扩展的exclude
配置来包括测试。它还包含noEmit: true
,因为在开发过程中使用TypeScript的进程不应该发出任何内容。此外,它还包含在运行时解析baseUrl
源文件所需的paths
和TypeScript编译器选项。这是因为包的main
文件中的package.json
字段引用了包含输出文件的lib
。
{
"extends": "./tsconfig.build.json",
"compilerOptions": {
"baseUrl": ".",
"noEmit": true,
"paths": {
"koas-*": ["packages/koas-*/src"]
}
},
"exclude": ["**/lib"]
}
每个工作区都包含一个tsconfig.build.json
文件。这是必需的,因为它需要src
和outDir
选项相对于tsconfig文件。用于构建项目。重要的是,这个文件是,而不是,名为tsconfig.json
。
{
"extends": "../../tsconfig.build.json",
"include": ["src"],
"compilerOptions": {
"outDir": "lib"
}
}
每个工作区还包含一个tsconfig.json
。这可以用于覆盖用于类型检查的编译器选项,例如,如果一个存储库使用dom
类型。这一点可以略去。
{
"extends": "../../tsconfig.json"
}
每个工作区在scripts
中都有下面的package.json
部分。这将导致在构建包时编译TypeScript。
// …
"scripts": {
"prepack": "tsc --project tsconfig.build.json"
}
有两个CI工作用于打字。一个运行tsc
以确保类型检查在开发中使用这些类型的工具中仍然有效,另一个确保构建包不会中断。
tsc:
script:
- yarn --frozen-lockfile
- yarn workspaces run tsc
pack:
script:
- yarn --frozen-lockfile
- yarn workspaces run pack
- find packages -name '*.tgz' -exec mv {} ./ +
artifacts:
name: packages
paths:
- '*.tgz'
https://stackoverflow.com/questions/58542301
复制相似问题