gpt4 book ai didi

javascript - 具有两个输出目录的 TypeScript 共享代码库

转载 作者:行者123 更新时间:2023-12-01 01:04:47 26 4
gpt4 key购买 nike

我遇到了一个问题:/我希望共享代码库“共享”位于两个目录中。这对于 typescript 本身来说是可能的还是我需要一些额外的构建任务?有更好的解决办法吗?

重要:srvapp中需要生成“shared”文件夹,否则我们的核心系统将会崩溃。

我有以下src目录,其中客户端包含client.ts,服务器包含server.ts,共享包含index.ts。现在我想在 client.ts 和 server.ts 中使用共享。

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"}
]
}

server/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"
}
]
}

共享/tsconfig.json

{
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"composite": true,
},
"include": [
"*.*"
],
"references": []
}

PS:如果有更好的解决方案来解决这个问题。请告诉我:)

亲切的问候,塞巴斯蒂安

最佳答案

您可以尝试将/shared 文件夹转换为单独的 npm 包,然后使用 npm link/npm install 将它们连接到每个单独的项目中?我使用 TypeScript 对 React 和 React Native 做了类似的事情。

关于javascript - 具有两个输出目录的 TypeScript 共享代码库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55747917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com