最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Enabling imports between two bundled TS packages in a monorepo - Stack Overflow

programmeradmin5浏览0评论

I have a monorepo with a /frontend and /backend dir. I have some shared types and enums in backend that I import into frontend. I've set up paths in my tsconfig to correctly alias this, and I reference it:

// frontend tsconfig.json
{
  "compilerOptions": {
    "rootDir": "./",
    "target": "ES2020",
    "useDefineForClassFields": true,
    "lib": ["ES2020", "DOM", "DOM.Iterable"],
    "module": "ESNext",
    "skipLibCheck": true,

    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,

    "paths": {
      "@/*": ["./src/*"],
      "@backend/*": ["../backend/*"],
      // "@shared/*": ["../shared/*"],
      "$/*": ["./react-maptiler/*"],
      "react-maptiler": ["./react-maptiler/index.ts"]
    },
    "typeRoots": ["./src/types"]
  },
  "exclude": ["node_modules", "dist"],
  "include": ["src", "serviceWorker/generateSW.js", "serviceWorker/sw.js"],
  "references": [
    { "path": "./tsconfig.node.json" },
    { "path": "../backend/" },
    { "path": "../shared/" }
  ]
}

Then I import into my frontend like this:

import { isUser } from '@backend/models/account'

But when I run tsc I get a bunch of these errors:

src/state/globalState.ts:7:8 - error TS6305: Output file 'C:/Users/user/Desktop/monorepoName/backend/models/account.d.ts' has not been built from source file 'C:/Users/user/Desktop/monorepoName/backend/models/account.ts'.    

7 } from '@backend/models/account'

My backend tsconfig also uses moduleResolution: bundler` with vite-node. Is there any way I can import into frontend from backend, since backend doesn't have a build step? This is the backend's tsconfig.json:

{
  "compilerOptions": {
    
/* "incremental": true, */
    "composite": true,
    "target": "ESNext",
    "experimentalDecorators": true,
    
/* This is essential for DBOS to work: */
    "emitDecoratorMetadata": true,
    "module": "ESNext",
    "moduleResolution": "bundler",
    
// "declaration": true,
    
// "declarationMap": true,
    "sourceMap": true,
    
// "outDir": "./dist",
    "newLine": "lf",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "paths": {
      "@/*": ["./*"] 
//"@shared/*": ["../shared/*"]
    },
    "baseUrl": ".",
    "resolveJsonModule": true,
    "typeRoots": ["@types"]
  },
  "include": [
    "api",
    "auth",
    "drizzle",
    "models",
    "utils",
    "types",
    "test/test.ts",
    "auth/firebase.ts",
    "zipcodes",
    "services",
    "config",
    "lib",
    "test",
    "@types",
    "shared"
  ],
  "exclude": ["dist"]
  
// "references": [{ "path": "../shared/" }]
}

I used to have a shared package that compiled and that worked well, but I'm currently unable to deploy my backend without having all code located within one package, so I'd like a workable solution in the present that allows me to import into frontend.

发布评论

评论列表(0)

  1. 暂无评论