【发布时间】:2019-09-08 21:39:45
【问题描述】:
我遇到了一个问题:/ 我希望共享代码库“共享”在两个目录中。打字稿本身可以做到这一点,还是我需要一些额外的构建任务?有没有更好的解决方案?
重要提示:“shared”文件夹需要在srv和app中生成,否则我们的核心系统会崩溃。
我有以下src 目录,其中客户端包含一个client.ts,服务器一个server.ts,共享包含一个index.ts。现在我想在client.ts和server.ts中使用shared。
src
- client
- client.ts
- tsconfig.json
- server
- server.ts
- tsconfig.json
- shared
- index.ts
- tsconfig.json
如果我正在编译我的 TypeScript,应该会生成以下输出。
app
- client
- client.js
- shared
- index.ts
srv
- server
- server.js
- shared
- index.ts
这是我对每个目录的配置 client/tsconfig.json
{
"compileOnSave": true,
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": true,
"preserveConstEnums": true,
"outDir": "../../app",
"sourceMap": true,
"diagnostics": true,
},
"include": [
"client.ts"
],
"references": [
{ "path": "../shared"}
]
}
服务器/ts.config.json
{
"compileOnSave": true,
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": true,
"preserveConstEnums": true,
"outDir": "../../srv",
"sourceMap": true,
"diagnostics": true,
},
"include": [
"server.ts"
],
"references": [
{
"path": "../shared"
}
]
}
shared/tsconfig.json
{
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"composite": true,
},
"include": [
"*.*"
],
"references": []
}
PS:如果有更好的解决方案来解决这个问题。请告诉我:)
亲切的问候, 塞巴斯蒂安
【问题讨论】:
标签: javascript node.js typescript