How to share code between TypeScript projects?

后端 未结 5 750
猫巷女王i
猫巷女王i 2020-12-12 19:11

Let\'s say I have two projects with following file structure

/my-projects/

  /project-a/
    lib.ts
    app.ts
    tsconfig.json

  /project-b/
    app.ts           


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-12 19:29

    It seems we've got a few options here:

    (1) Put both projects in a mono repo and use answer given by @ThdK using TS project references

    NOT GOOD if you don't want a mono repo

    (2) Use Lerna - see answer by @metric_caution

    NOT GOOD if you can't be bothered to learn Lerna / don't want to publish your shared files to npm

    (3) Create a shared npm package

    NOT GOOD if you don't want to publish your shared files to npm

    (4) Put shared folders in a "shared" directory in project A and write a script to copy files in the shared folder from project A to project B's shared folder that is executed on a git push OR a script to sync the two folders.

    Here the script can be executed manually when copying / syncing is required. The copying / syncing could also be done prior to a git push using husky and the shared files added to git automatically in the script.

    Since I don't want a mono repo and I can't be bothered to publish an npm package for such a pathetic purpose I'm gonna go with option 4 myself.

提交回复
热议问题