我试图设置一个基于TypeScript的monorepo
Lerna
酒吧
福
进口
按路径别名,执行此操作失败。
-
tree
.
âââ lerna.json
âââ package.json
âââ package-lock.json
âââ packages
â  âââ bar
â  â  âââ lib
â  â  â  âââ index.d.ts
â  â  â  âââ index.js
â  â  âââ package.json
â  â  âââ src
â  â  â  âââ index.ts
â  â  âââ tsconfig.build.json
â  â  âââ tsconfig.json
â  âââ foo
â  âââ lib
â  â  âââ index.d.ts
â  â  âââ index.js
â  âââ package.json
â  âââ src
â  â  âââ index.ts
â  âââ tsconfig.build.json
â  âââ tsconfig.json
âââ tsconfig.build.json
âââ tsconfig.json
-
./tsconfig.build.json
{
"compilerOptions": {
"baseUrl": ".",
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true
}
}
-
{
"extends": "./tsconfig.build.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@company/bar": [
"packages/bar"
],
"@company/foo": [
"packages/foo"
]
}
}
}
-
{
"packages": [
"packages/*"
],
"version": "0.0.0"
}
-
./包.json
{
"name": "root",
"private": true,
"scripts": {
"tsc": "lerna run tsc"
},
"devDependencies": {
"lerna": "^3.22.1",
"ts-node": "^9.0.0",
"ts-node-dev": "^1.0.0-pre.63",
"typescript": "^4.0.3"
}
}
:
-
./包装/bar/src/索引.ts
export const bar = 'bar';
-
{
"name": "@company/bar",
"version": "1.0.0",
...
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"tsc": "tsc -p tsconfig.build.json"
}
}
-
/包/条/tsconfig.build.json
{
"extends": "../../tsconfig.build.json",
"compilerOptions": {
"outDir": "./lib"
},
"include": [
"src/**/*"
]
}
-
{
"extends": "../../tsconfig.json"
}
包裹
福
-
./packages/foo/src/索引.ts
import { bar } from '@company/bar';
console.log(bar);
-
./packages/食品/包.json
{
"name": "@company/foo",
"version": "1.0.0",
...
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"tsc": "tsc -p tsconfig.build.json"
}
}
-
“编译器选项”:{
“outDir”:“../lib”
},
“src/**/*”
]
}
-
{
“扩展”:“.//tsconfig.json文件"
}
最后:
npm run tsc
编译我的包,在哪里
福
进口
. 它给出了以下错误:
> lerna run tsc
lerna notice cli v3.22.1
lerna info Executing command in 2 packages: "npm run tsc"
lerna info run Ran npm script 'tsc' in '@company/bar' in 2.4s:
> @company/bar@1.0.0 tsc /.../monorepo-lerna/packages/bar
> tsc -p tsconfig.build.json
lerna ERR! npm run tsc exited 2 in '@company/foo'
lerna ERR! npm run tsc stdout:
> @company/foo@1.0.0 tsc /.../monorepo-lerna/packages/foo
> tsc -p tsconfig.build.json
src/index.ts(1,21): error TS2307: Cannot find module '@company/bar' or its corresponding type declarations.
如果我改变
import { bar } from '@company/bar';
到
import { bar } from '../../bar/src';
不过,我想先用进口的方式
bar