使用TypeScript在monorepo中构建共享库

使用TypeScript在monorepo中构建共享库,typescript,build,shared-libraries,monorepo,Typescript,Build,Shared Libraries,Monorepo,我有一个具有以下结构的monorepo: repo packages native (expo app) server (express + graphql) shared (to be shared with the rest) build graphql index.d.ts index.js models user.d.ts user.js

我有一个具有以下结构的monorepo:

repo
  packages
    native (expo app)
    server (express + graphql)
    shared (to be shared with the rest)
      build
        graphql
          index.d.ts
          index.js
        models
          user.d.ts
          user.js
          locale.d.ts
          locale.js
          index.d.ts
          index.js
        utils
          datetime.d.ts
          datetime.js
          index.d.ts
          index.js
        index.d.ts
        index.js
      src
        graphql
          index.tsx
        models
          user.ts
          locale.ts
          index.ts
        utils
          datetime.ts
          index.ts
        index.ts
      tsconfig.json
    web (nextjs app)
  lerna.json
  package.json
  tsconfig.json
我目前使用的
共享
软件包如下:

从'@repo/shared'导入{getTodayFn,UserClass}
从'@repo/shared/build/utils'导入{getTodayFn}
从'@repo/shared/build/models/user'导入{UserClass}
但我希望能够在不使用
构建的情况下导入

从'@repo/shared/utils'导入{getTodayFn}
从'@repo/shared/models/user'导入{UserClass}
我应该怎么做才能实现我想要的?

配置:

root/tsconfig.json

{
  "compilerOptions": {
    "sourceMap": true,
    "removeComments": false,
    "preserveConstEnums": true,
    "downlevelIteration": true,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "allowUnreachableCode": false,
    "noFallthroughCasesInSwitch": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "pretty": true,
    "forceConsistentCasingInFileNames": true
  }
}
{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "node",
    "jsx": "react",
    "lib": [
      "dom",
      "esnext"
    ],
    "skipLibCheck": true,
    "preserveConstEnums": true,
    "rootDir": "./src",
    "outDir": "./build",
    "declaration": true,
    "declarationDir": "./build",
    "declarationMap": true,
    "baseUrl": "./",
    "paths": {
      "@lib/*": [
        "src/*"
      ]
    }
  },
  "include": [
    "./src/**/*"
  ],
  "exclude": [
    "node_modules",
    "build"
  ]
}
{
  "name": "@repo/shared",
  "license": "MIT",
  "version": "1.0.0",
  "main": "build/index.js",
  "typings": "build/index.d.ts",
  "scripts": {
    "build": "rimraf build && tsc",
    "watch": "tsc -w"
  },
  "dependencies": {
  },
  "devDependencies": {
    "rimraf": "^2.6.3",
    "typescript": "^3.5.3"
  }
}

packages/shared/tsconfig.json

{
  "compilerOptions": {
    "sourceMap": true,
    "removeComments": false,
    "preserveConstEnums": true,
    "downlevelIteration": true,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "allowUnreachableCode": false,
    "noFallthroughCasesInSwitch": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "pretty": true,
    "forceConsistentCasingInFileNames": true
  }
}
{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "node",
    "jsx": "react",
    "lib": [
      "dom",
      "esnext"
    ],
    "skipLibCheck": true,
    "preserveConstEnums": true,
    "rootDir": "./src",
    "outDir": "./build",
    "declaration": true,
    "declarationDir": "./build",
    "declarationMap": true,
    "baseUrl": "./",
    "paths": {
      "@lib/*": [
        "src/*"
      ]
    }
  },
  "include": [
    "./src/**/*"
  ],
  "exclude": [
    "node_modules",
    "build"
  ]
}
{
  "name": "@repo/shared",
  "license": "MIT",
  "version": "1.0.0",
  "main": "build/index.js",
  "typings": "build/index.d.ts",
  "scripts": {
    "build": "rimraf build && tsc",
    "watch": "tsc -w"
  },
  "dependencies": {
  },
  "devDependencies": {
    "rimraf": "^2.6.3",
    "typescript": "^3.5.3"
  }
}

packages/shared/package.json

{
  "compilerOptions": {
    "sourceMap": true,
    "removeComments": false,
    "preserveConstEnums": true,
    "downlevelIteration": true,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "allowUnreachableCode": false,
    "noFallthroughCasesInSwitch": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "pretty": true,
    "forceConsistentCasingInFileNames": true
  }
}
{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "node",
    "jsx": "react",
    "lib": [
      "dom",
      "esnext"
    ],
    "skipLibCheck": true,
    "preserveConstEnums": true,
    "rootDir": "./src",
    "outDir": "./build",
    "declaration": true,
    "declarationDir": "./build",
    "declarationMap": true,
    "baseUrl": "./",
    "paths": {
      "@lib/*": [
        "src/*"
      ]
    }
  },
  "include": [
    "./src/**/*"
  ],
  "exclude": [
    "node_modules",
    "build"
  ]
}
{
  "name": "@repo/shared",
  "license": "MIT",
  "version": "1.0.0",
  "main": "build/index.js",
  "typings": "build/index.d.ts",
  "scripts": {
    "build": "rimraf build && tsc",
    "watch": "tsc -w"
  },
  "dependencies": {
  },
  "devDependencies": {
    "rimraf": "^2.6.3",
    "typescript": "^3.5.3"
  }
}


你是只想改变路径还是想创建不同的包?我想单独访问文件夹,如果你愿意的话,有点像“分离关注点”。例如,如果我需要一些datetime函数,我应该使用“@repo/shared/datetime”中的
import{dateTimeFn}
,但在当前配置中我不能这样做。每当我在VSC中添加斜杠时,
/build
就会弹出。您可以看到。
npm publish
应该向npm发布一个包,对吗?我得到401-未经授权的错误,实际上我不想向npm发布任何东西,因为这是一个monorepo。你应该使用项目引用。看@Inigo这看起来很有帮助。我很快就会试试的,谢谢。