2

My side project uses Electron.js, Typescript and Gulp.

I am endeavouring to lend it some structure using TypeScript Project References.

Instead of one codebase, there are 3 sibling projects that refer to each other. It compiles the TS to JS, however what I am looking to understand, preferably by learning from a working example, is how I can execute the resulting JS object code, which is now distributed across 3 build directories.

Other compiled languages have a link phase that addresses this issue. What simple thing can I use in the JavaScript/TypeScript universe?

3 Answers 3

1

For larger projects my preferred strategy would be a monorepo. This structure allows you to separate the specific bits of your app while still being able to share one repository and/or common infrastructure.

If you are using yarn and you don't plan on e.g. publishing your sibling projects to NPM separately you can easily setup monorepo in your project using yarn workspaces, otherwise I would suggest using lerna.

These tools will set those links for you automatically so your projects can depend on each other, I am not sure about yarn but lerna was very good with creating a dependency graph of your projects and building them based on that so you didn't need to care about the order in which you build your projects.

Sign up to request clarification or add additional context in comments.

Comments

0

It seems you are looking for npm link.

You need to arrange the main property of the three package.json files in order to refer to the relative build directoris; adding the dependencies between the three packages; last npm link each other the packages.

Let's say the three packages names are pkgA, pkgB and pkgC, with following commands you should achieve the desired result.

/pkgA $ npm link
/pkgB $ npm link
/pkgC $ npm link
/pkgA $ npm link pkgB
/pkgA $ npm link pkgC
/pkgB $ npm link pkgA
/pkgB $ npm link pkgC
/pkgC $ npm link pkgA
/pkgC $ npm link pkgB
/pkgA $ npm install
/pkgB $ npm install
/pkgC $ npm install

Hope this helps.

2 Comments

Looks encouraging Daniele .. can you point me to a working example? GitHub/gitlab/etc?
Sorry @MartinCowie I use it only in private monoreps... Anyway it should be quite easy, if you find any troubles, feel free to ask again
0

To answer your question for an example, I put together this simple project using Electron/TypeScript that holds multiple projects using TypeScript project references: https://github.com/SploxFox/MartinCowieExample

To execute, navigate to the Main directory and then:

npx electron .

Basically, what we're doing is just exposing modules, and then we include those components in a different Main project that is the one that is actually executed. First, we compile the individual components, and then we can compile the Main project and then run the Main project.

However, I don't recommend that you do this. Instead, you might want to use npm link -- which only creates the linkages on your computer, which is difficult to show an example of.

First, compile each project, and make sure you have type declarations on. We reference the compiled JS, not the raw source code when using different projects. Make sure that you have set the package name to what you want it to be in the package.json, then just navigate to each directory that you want to link to (so not your main directory) and do npm link. This doesn't change the package at all; rather it just creates a symlink in the user's npm AppData folder. In the directories in which you will reference another package, do npm link package-name to link to the other package.

Now you'll be able to use that package as if it were any other npm package, though the vscode autocompletion for linked packages isn't the best in my experience so you may need to manually type out the import statements.

EDIT: For the project, I added compAFunc as a dependency to compBFunc on the GitHub project. To build, navigate to the Main directory and do tsc --build. More info on project references: https://www.typescriptlang.org/docs/handbook/project-references.html

2 Comments

Hi Splox. Your example is 90% of what I'm looking for. When I add a compAFunc as a dependency of compBFunc it fails to compile.
I edited the GitHub repo so compAFunc is a dependency of compBFunc. Running tsc --build from the Main directory should build all dependencies and then the main project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.