【发布时间】:2020-01-16 03:43:24
【问题描述】:
在开始之前我有几个前提:
- 我想要一个共享包,它将包含在其他三个包中,
- 打字稿,
- 只有一个节点模块,
- 多平台使用(Windows / Linux),
- 使用共享包中的依赖项,
- (实时重新加载会很好),
- 不要失去以后发布共享包的能力(其他三个包是网络服务器,但现在正在大力开发中),
- 通用脚本
build:webbuild:apibuild:admin来自全局包。
我梦想的文件夹结构是:
/
/node_modules/***
/packages/shared/package.json // Shared dependencies like typeorm, lodash etc..
/packages/shared/src/index.ts
/packages/shared/src/models/User.ts
/packages/web/package.json
/packages/web/tsconfig.json
/packages/web/src/index.ts // first express server (using shared code)
/packages/api/package.json
/packages/api/tsconfig.json
/packages/api/src/index.ts
...
/package.json
/tsconfig.common.json
互联网上有很多解决方案 - Lerna、typescript 项目参考、npm / yarn 链接……但我找不到合适的解决方案……
例如,这个https://github.com/Quramy/lerna-yarn-workspaces-example 很酷,但我无法设置运行这样的东西:
// package.json
{
"scripts": {
"debug:web": "concurrently \"(cd packages/shared && tsc -w)\" \"(cd packages/web && tsc -w)\" \"(cd packages/web && nodemon --inspect dist/index.js --watch .. /)\""
...
我想在 ../ (包文件夹)中发生更改后不断构建共享包(生成 dist/index.js 和 .ts 文件)并在 nodemon 中重新启动服务器。
几个小时后,我放弃了重新加载代码的机会,并通过我能找到的“最简单的方式”尝试 - shared 中没有包,但直接通过以下方式包含代码:
// shared/index.ts
export const createConnection = () => ....
// web/index.ts
import { createConnection } from 'shared'
它看起来也不起作用:
// tsconfig.json
{
"extends": "../../tsconfig.settings.json",
"compilerOptions": {
"rootDir": "../",
"outDir": "dist",
"baseUrl": "./",
"paths": {
"shared": [ "../shared/src/" ]
}
}
}
现在的问题是:
File /shared/src/index.ts is not listed within the file list of the project in tsconfig.json. Projects must list all files or use an 'include' pattern.
下一步是(显然):
{
"extends": "../../tsconfig.settings.json",
"include": [
"./src/**/*.ts",
"../shared/src/**/*.ts"
],
"compilerOptions": {
"rootDir": "../",
"outDir": "dist",
"baseUrl": "./",
"paths": {
"shared": [ "../shared/src/" ]
}
}
}
这行得通!没有错误,但“dist”中也没有文件...... LOL
我很确定,有一些不错的解决方案、示例或者它也不起作用,但我无法找到或构建它。
【问题讨论】:
标签: node.js typescript npm lerna