node.js - 我可以从 ts 编译器中动态排除文件吗?
问题描述
我正在设置一个节点/打字稿服务器来构建一个实时应用程序。我的服务器和客户端在同一个文件夹中。
我想要做的是在我运行“server:dev”脚本时从 typescript 编译器中排除“src/client”,当我运行“client:dev”时排除“src/server”。
我已经尝试找到一种从命令行中排除文件的方法,但我没有找到解决方案。
这就是我的 tsconfig.json 的样子
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": ["dom","es2017"],
"sourceMap": true,
"outDir": "dist",
"strict": true,
"noImplicitAny": true,
"moduleResolution": "node",
"esModuleInterop": true,
"resolveJsonModule": true
},
"exclude": [
"src/client"
]
}
但是当我运行客户端时,我需要包含“src/client”并排除“src/server”。
解决方案
tsconfig.json
supportextends
字段,在您的情况下,您应该将公共配置放在 base中,然后分别为客户端和服务器tsconfig.json
创建 tsconfig 。extends
// tsconfig.json
{
"compilerOptions": {
...
}
}
// tsconfig.client.json
{
"extends": "./tsconfig.json",
"compilerOptions": {
...
},
"exclude": ["src/server"]
}
// tsconfig.server.json
{
"extends": "./tsconfig.json",
"compilerOptions": {
...
},
"exclude": ["src/client"]
}
对于 npm 脚本:
// package.json
{
"scripts": {
"server:dev": "tsc --build tsconfig.server.json",
"client:dev": "tsc --build tsconfig.client.json",
}
}
推荐阅读
- javascript - 带有 forRoot 方法的惰性模块
- google-chrome-extension - 基本谷歌浏览器扩展菜鸟问题
- powershell - PowerShell:列出 CSV 文件行,其中第三列和最后一列之间的至少一个值等于“0”或“1”
- excel - 删除 CSV 中的逗号,以便在使用记事本编辑时不显示
- google-chrome - WebRTC ICE-Stun 消息完整性属性
- css - Bootstrap 4 浮动属性不适用于 SVG 元素
- python - Python numpy 数组 - 初始化第一个元素?
- ruby-on-rails - 如何在 Rails 应用程序中创建模块化 UI 单元(组件)?
- angular - 应用程序在卸载状态下死亡:无法读取未定义的属性“注入器”
- html - 设置@media res 时图像不会调整大小