NiGhTTraX / ts-monorepo

Template for setting up a TypeScript monorepo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

importing files from package without /src or /dist

CosticaPuntaru opened this issue · comments

hello,
i am trying to link a file from one package to other directly, it doesn't work,
here is a fork with my example https://github.com/CosticaPuntaru/ts-monorepo/blob/master/packages/bar/src/index.ts

can you help me make it work ? :)

if i change paths to

  "paths": {
      "@nighttrax/foo/*": ["packages/foo/src/*"],
      "@nighttrax/*": ["packages/*/src"]
    },

the webstorm ide does not complain about the file, but compile breaks,

if i change tsconfig.build.json and add

"baseUrl": ".",
    "paths": {
      "@nighttrax/foo/*": [
        "packages/foo/src/*"
      ]
    }

it compiles but the compiled files are all messed up,
image

i understand why, but i am looking for workaround :)

Hey @CosticaPuntaru, since foo's compiled output resides in dist/ the import has to contain that path when targeting a file inside the package:

//                                                   VVVV
import { theDeepMeaningOfLife } from "@nighttrax/foo/dist/utils/my-utils";

If you want to expose the package's file structure to the consumer (thus making it part of the API), then you're going to have to do some wrangling in your build scripts. You already figured out that adding path aliases in the build config will make things compile, but place both packages in the build output. If you want to publish bar you can copy its package.json in dist/bar and publish from there. Here's an example of the copying and publishing (with lerna) respectively. This would apply for both foo and bar.

If you're authoring libraries then I recommend exposing all your exports from the root index.ts so that consumers have a unified way of importing them and changes to your folder structure will not break those imports. If you're authoring "apps" that you build and then deploy then I recommend importing using foo/src/... paths and relying on yarn/lerna symlinks instead of path aliases (removing compilerOptions.paths and pkg.main altogether).

Hope this helps!

exporting everything into index isn't a desired solution as the lib will have large components, my intention is to do something like your example, "package": "cp -r README.md package.json ./dist", but i'm wondering how can i make lerna/tsc play nice and work with files from /src folder while developing locally

exporting everything into index isn't a desired solution as the lib will have large components

If you make your library tree shakable then consumers will transparently get only the code they explicitly import. Try to give rollup a go.

Having said that, the approach you described in the first post should work for local development — you explicitly map "@nighttrax/foo/*": ["packages/foo/src/*"] in both the local and the build tsconfig, and then use a script to publish from within the package's dist folder.