question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Relative `alias` resolution for monorepos

See original GitHub issue

Feature request

In monorepos, use of “common packages” is widespread. Files of these common packages can be referenced directly from other packages, and be put together into one webpack.config.js, with a single output.

However, different packages might have common aliases, which should resolve differently per package. Example: the src alias should point to the package’s src folder.

What is motivation or use case for adding/changing the behavior?

Example:

  • 2 packages: App and common-lib.
  • There is only one webpack.config.js in the project. It is to build App.
    • common-lib does not need to be built independently. It is only to be used in conjunction with other modules.
  • Files in App can directly import files from common-lib (usually by configuring resolve.modules and resolve.alias).

Problem: some aliases are package-relative. Most commonly: the src alias. (It is so common in fact, that VSCode automatically resolves to src, if it is shorter than a relative import, even though, if baseUrl is configured, and src is in baseUrl.)

How should this be implemented in your opinion?

The alias configuration option can take a function which (or provide some other way where it) resolves the same alias differently, depending on where it is used.

This enables the following scenario:

  • import from 'src/X';
    • should resolve to App/src/X, if used in App
    • should resolve to common-lib/src/X, if used in common-lib

Are you willing to work on this yourself?

Maybe, depends on feedback.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
alexander-akaitcommented, May 19, 2021

You can achieve this using:

Also it is not good for ESM, because in real world the same URL in import should resolve the same file (don’t forget import is cached), so because it is non standard, please use any custom solution from above, thanks

0reactions
alexander-akaitcommented, Jan 3, 2022

When you bundle multiple multiple version of package, each modules have identifier (it is path to module i.e. for example if you have require("package") and some package also have require("package") you will have node_modules/package/index.js and node_modules/another-package/node_modules/package/index.js), so you will have two versions of packages.

As I written above you can create a custom plugin for resolver and implement any logic. module federation is some other technology, it allow to share modules in runtime and reuse them, so you can create micro architecture.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript aliases and Lerna monorepo setup - webman.pro
Hey, TypeScript! Now, when I import a package let me use aliases instead of relative paths. Also, enable a source code navigation and...
Read more >
TypeScript "paths" in monorepo · Discussion #620 · vercel/turbo
Let's say I want to use typescript's "paths" to simplify my imports. I'll use the current starter project as an example, with the...
Read more >
Typescript monorepo, path aliases conflicts between ... - Reddit
Hello everyone, I made a small repository to reproduce an issue I am having. It basically is related to dependency B is without...
Read more >
Monorepo with paths from Typescript is not working
I say this, because an import from "commonlib/src" is not obvious to be a path alias, and could simply be a relative path...
Read more >
Making TypeScript monorepos play nice with other tools
The path aliases will resolve to files inside the monorepo that are outside of the app so we need to remove the compilation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found