带有 src 和 test 的 Typescript 配置
Typescript configuration with src and test
我有多个 TypeScript 项目,我将源代码从测试中分离到它们的目录(src
和 test
)。在最终包中,我只想包含没有测试的源代码,因为运行时不需要知道任何关于测试文件、固定装置等的信息。
如果我在tsconfig.json
中有如下设置:
{
"compilerOptions": {
// compiler options
"outDir": "dist"
},
"include": ["src"]
}
还有 package.json
中的这些:
{
// usual settings
"main": "dist/index.js",
"files": [
"dist/**/*.js",
"dist/**/*.d.ts"
]
}
那么最后的打包只包含了我想要的目录结构中的源代码,这是我想要看到的,但是我的环境有问题。我正在使用 Doom Emacs 并且为了测试潮汐会抛出这样的错误:
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)
如果我将 tsconfig.json
设置更改为包含 test
目录,那么潮汐错误就会消失:
{
"compilerOptions": {
// compiler options
"outDir": "dist",
"rootDirs": ["src", "test"]
}
}
但是随后分发的目录结构发生了变化,我想避免出现 dist/src
和 dist/test
,因为那时我的库的用户将需要使用奇怪的导入语句:
import { foo } from 'library/dist/src/foo';
如果可能的话,我想避免额外的 src
,dist
已经够丑了,但这是给定的。
我尝试使用多个设置将 include
属性 更改为 src
和 test
,但构建最终出现在 dist
目录中相同的嵌套结构:
{
"compilerOptions": {
// compiler options
"outDir": "dist"
},
"include": ["src", "test"]
}
我也试过 package.json
设置,但没有成功。如果不在构建过程中添加额外的步骤来删除不必要的额外目录,我能做些什么吗?
根据 @jonrsharpe 的评论,我得到了两个 tsconfig 文件:
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"]
}
还有一个tsconfig.build.json
:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "es2018",
"noUnusedLocals": true
},
"include": ["src"]
}
并且在 package.json
中:
{
// usual settings
"main": "dist/index.js",
"files": [
"dist/**/*.js",
"dist/**/*.d.ts"
],
"scripts": {
"build": "tsc --build tsconfig.build.json",
// other scripts
}
}