javascript - 具有两个输出目录的 TypeScript 共享代码库
问题描述
我遇到了一个问题:/我希望共享代码库“共享”在两个目录中。打字稿本身可以做到这一点,还是我需要一些额外的构建任务?有更好的解决方案吗?
重要提示:“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"
}
]
}
共享/tsconfig.json
{
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"composite": true,
},
"include": [
"*.*"
],
"references": []
}
PS:如果有更好的解决方案来解决这个问题。请告诉我 :)
亲切的问候,塞巴斯蒂安
解决方案
您可以尝试将 /shared 文件夹转换为单独的 npm 包,然后在每个单独的项目中使用npm link
/npm install
将它们连接在一起?我使用 TypeScript 对 React 和 React Native 做了类似的事情。
推荐阅读
- angular - Angular,您提供了一个无效对象,其中仅在 --aot build 中需要流
- python - Discord py bot 未触发 on_member_join 函数
- c# - 在 asp.net core 3.0 中为角色 SecurityStamp 创建 ClaimsIdentityOptions
- javascript - 具有配置索引的数组在 ng-repeat 上不起作用,也不能在 AngularJS 的 html 中显示
- java - 如何保留我的距离表示分数字段并将其映射到我的结果实体中?
- python-3.x - 即使转到正确的路径,我也无法在 python 中打开文件
- javascript - 以任何方式将 JavaScript 文件链接到 HTML 文件都不起作用
- c++ - c++ MPI广播矢量
- javascript - 如何从javascript中的字符串中提取json
- r - R中的凸联合操作