swc icon indicating copy to clipboard operation
swc copied to clipboard

Alternative package manager support for the module resolution

Open XiNiHa opened this issue 3 years ago • 16 comments

Describe the feature

Since the currently implemented plugin resolve algorithm relies on the node_modules structure, it doesn't work with alternative module linking approaches like ones from pnpm and Yarn Berry. While directly pointing at the WASM binary is implemented as a temporary workaround, this should be covered with a more elegant and user-friendly approach.

Initially discussed in the Discord channel

Babel plugin or link to the feature description

No response

Additional context

No response

XiNiHa avatar Jan 13 '22 03:01 XiNiHa

This is largely about module resolution we use in swc. It is not limited to plugin itself but somewhat overlaps to bundler, or any other features needs module resolution in some way.

Two main questions

  • We'll support npm as first class citizen. What else / and how? there are some esoteric implementation like yarn's pnp.
  • What / how to provide way to explicitly specify some resolution? We already support polymorphic behavior of plugin config itself (path vs. module name). But we need better.

kwonoj avatar Jan 13 '22 08:01 kwonoj

We'll support npm as first class citizen. What else / and how? there are some esoteric implementation like yarn's pnp.

One really nice thing about yarn pnp is that there is one source of truth for how modules are resolved. Particularly with monorepo structures it can be really complicated if there are multiple things trying to resolve a module:

  • Your IDE.
  • Your bundler.
  • Other tooling (type-checker, linting, etc).

charrondev avatar Jan 25 '22 19:01 charrondev

One really nice thing about yarn pnp

I know good things about pnp and I'd like to use for some other places as well.

Main blocker in here is yarn's pnp resolution relies on its generated resolver in js, which swc's host cannot resolve by running it. This means it may requires to reimplement whole resolving logic by ourselves which I consider as heavy blocker to support.

kwonoj avatar Jan 25 '22 19:01 kwonoj

for plugin: I think a postInstall would work, or maybe we can put it in bin in package.json and try to parse generated .sh file in node_modules/.bin. for bundler: I think it support pnpm since we follow soft link. but no idea for yarn pnp.

RiESAEX avatar Feb 13 '22 02:02 RiESAEX

any update?

laclance avatar Apr 16 '22 17:04 laclance

We do not have any work in the progress / do not have visible plan for this. This issue is filed for tracking purpose.

kwonoj avatar Apr 16 '22 17:04 kwonoj

Support for Yarn 3.x PNP would be really appreciated. Thx

Ethorsen avatar Jul 09 '22 12:07 Ethorsen

Main blocker in here is yarn's pnp resolution relies on its generated resolver in js, which swc's host cannot resolve by running it.

Yarn supports a JSON data file. Perhaps that might be an easier pnp map to integrate with? You don't need to run any JS.

The preferred approach IMO would be to expose a plugin API for module resolution. Yarn could then maintain their own plugin.

noahnu avatar Aug 03 '22 20:08 noahnu

The doc I was referencing was https://yarnpkg.com/features/pnp#initializing-pnp which explicitly says it creates cjs. Where does those json spec exists?

Also it doesn't mean reducing lot of work. The only difference is port cjs resolution logic to rust, while json we need to still deal with all others like zipfs implementation.

Opening up module resolution is huge refactoring work, and it still requires yarn implements above logic. If that's the case, someone can directly upstream pr still. In short, the issue is amount of the work and resources we need compare to the active userbase we currently support who should use pnp.

kwonoj avatar Aug 03 '22 20:08 kwonoj

It's opt-in. You have to set pnpEnableInlining to false to generate the json file: https://yarnpkg.com/configuration/yarnrc#pnpEnableInlining

noahnu avatar Aug 03 '22 20:08 noahnu

The spec for .pnp.data.json is here: https://yarnpkg.com/advanced/pnp-spec.

In addition to Yarn itself, it's also been implemented in ESBuild v0.15.0.

sargunv avatar Sep 19 '22 23:09 sargunv

I'm marking this as open for contributions. Anyone would like to provide support welcome to review PR, however we do not have enough bandwidth to try this by our own.

kwonoj avatar Jul 24 '23 19:07 kwonoj