Esm: Feature Request: add ability to override module resolution

Created on 27 Mar 2018  Â·  11Comments  Â·  Source: standard-things/esm

Flex point had been added in jest to allow for something like moduleNameMapper or moduleDirectories. In trying to integrate jest and esm in a largish codebase, I found that there's no way to directly override in esm. This is a feature request to allow for this as a flex point in esm as well to synchronize the settings of resolve logic between jest & esm when doing something like esm-jest.

bug

All 11 comments

Hi @kenotron!

Just to be clear flex point isn't a library right? You're saying a flex point is in jest that allows it to do something like moduleNameMapper and moduleDirectories?

With cjs interop esm will use the same wiring that CJS uses so if it's overridable in CJS it should transfer to esm. That's where I'd like that kind of feature to stay. So if it's not working that way, then that's something for us to dig into.

flex point isn't a lib :) it's just a saying :)

Indeed, I'm hoping to see something like moduleNameMapper and moduleDirectories type thing. If not, even exposing a resolver callback would be helpful.

It seems jest has some internal mapper functionality that isn't exposed in esm. I might have to now monkey patch esm to get this sort of thing. What I'm looking for is some way to do aliasing that is on par with jest, webpack or typescript... unfortunately it doesn't seem like truly in the spirit of this project, but having a hook would be very useful for lots of projects that have some custom way to do resolve on top of how they are doing it with other build tooling.

There will eventually be loader hooks in Node for ES modules and we'll support those for sure. If you're looking to make something work today though, you should go down the path of making it work for CJS then seeing if esm can just hop on that.

Trying to override the module._resolveFilename won't work with esm it seems
...
On Tue, Mar 27, 2018 at 3:03 PM John-David Dalton notifications@github.com
wrote:

There will eventually be loader hooks in Node for ES modules. We'll adopt
those for sure. If you're looking to make something work today though. You
should go down the path of making it work for CJS then seeing if esm can
just hop on that.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/standard-things/esm/issues/331#issuecomment-376690898,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AACHparr65ZvuDQGVncrlmGRGhpdklBFks5tirdJgaJpZM4S9qES
.

The way one would go about doing this would be the same hooks that esm is doing really. However, esm takes over the resolution logic in a way that hides all the hook points as far as I can tell. What one would want to do with CJS is something like this: https://www.npmjs.com/package/module-alias

With esm, it seems I'll have to actually FORK it to expose the right hooks maybe... I'm still digesting this codebase, but I'm very new to it, so it'll take some time to find the right place to add the hook (frankly it would be nice to do it at the hook/module or hook/require)

My sense is that there's deliberately no hooks because esm is trying to bridge the gap to future Node behavior, not be a tool for hooking module loading more generally. For that, there's pirates, and I think esm uses that and respects chaining loaders with that, which is how @babel/register and esm can be used together.

I have had no problem using esm with @babel/register and babel-plugin-module-resolver. You can either run with @babel/register as your hook, then, or compile with babel before publishing and use esm in production without hooking the resolver further.

Apologies if I'm just lacking understanding of the details, though. I could see how maybe for a testing tool like jest it might be hard to register a hook before esm, depending on how things are invoked. I hope my comment helps.

@kenotron

Trying to override the module._resolveFilename won't work with esm it seems

I regressed something in esm is all. I'll fix it up.

This is 💯. I'll be able to replace a bunch of transformer weirdness with
this in our pipeline!
On Tue, Mar 27, 2018 at 8:13 PM John-David Dalton notifications@github.com
wrote:

Trying to override the module._resolveFilename won't work with esm it seems

I regressed something in esm is all. I'll fix it up.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/standard-things/esm/issues/331#issuecomment-376746961,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AACHpZpXm7gJtIeKwbyNlz1Iv1e7o4zWks5tiv_dgaJpZM4S9qES
.

Was this page helpful?
0 / 5 - 0 ratings