0x80 / isolate-package

Isolate a monorepo package with its internal dependencies to form a self-contained directory with a pruned lockfile
MIT License
121 stars 13 forks source link

Not sure how to use the local package #37

Closed maelp closed 10 months ago

maelp commented 10 months ago

In my expressjs server that I'm using with isolate to deploy on GAE I do imports like

import app from "@/app"

with in the tsconfig:

"@/*": "./src/*"

but after compiling, and running isolate, I have this bug when deploying:

2023-12-01 13:17:04 default[20231201t131511]  Error: Cannot find module '@/app'
2023-12-01 13:17:04 default[20231201t131511]  Require stack:
2023-12-01 13:17:04 default[20231201t131511]  - /workspace/dist/server.js
2023-12-01 13:17:04 default[20231201t131511]      at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
2023-12-01 13:17:04 default[20231201t131511]      at Module._load (node:internal/modules/cjs/loader:922:27)

is there a way for this to work?

here is my tsconfig.json (the lib-gouach-db-schemas is my local monorepo lib that I'm including in the project)

{
  "references": [{ "path": "../lib-gouach-db-schemas/tsconfig.cjs.json" }],
  "compilerOptions": {
    "target": "ES2020",
    "module": "CommonJS",
    "moduleResolution": "Node",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "sourceMap": true,
    "allowJs": true,
    "skipLibCheck": true,
    "lib": ["esnext.asynciterable"],
    "outDir": "dist",
    "rootDir": "src",
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"],
      "@gouach/db-schemas/*": ["../lib-gouach-db-schemas/dist/cjs/*"]
    },
    "pretty": true,
    "composite": false
  },
  "compileOnSave": true,
  "include": ["src", "src/**/*.js", "src/**/*.ts"]
}
maelp commented 10 months ago

and there is actually a dist/app.js which is there and correctly compiled...

0x80 commented 10 months ago

@maelp The typescript compiler does not convert your path aliases. I know, it was surprising to me too when I first encountered this.

You will need to run something after it like tspath (not recommended) or use a bundler like tsup to compile and bundle the code. Tsup is based on esbuild which is very fast. I recommend using tsup. It's very easy and you can have a look at my mono-ts example to see how to configure it.

0x80 commented 10 months ago

Going to close this since it is unrelated to isolate-package, but feel free to continue the conversation.