我有多个 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
,从那时起,我的库的用户将需要使用奇怪的导入语句:< /p>
import { foo } from 'library/dist/src/foo';
如果可能的话,我想避免额外的 src
,dist
已经够丑了,但这是给定的。
我尝试通过多个设置将 include
属性更改为具有 src
和 test
,但构建最终位于具有相同嵌套结构的 dist
目录中:< /p>
{
"compilerOptions": {
// compiler options
"outDir": "dist"
},
"include": ["src", "test"]
}
我也尝试使用 package.json
设置,但没有任何运气。如果不在构建过程中添加额外的步骤来删除不必要的额外目录,我能做些什么吗?
答案 0 :(得分:1)
根据 @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
}
}