Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: could exports support array #17

Closed
lovetingyuan opened this issue Mar 8, 2022 · 7 comments · Fixed by #24
Closed

feature request: could exports support array #17

lovetingyuan opened this issue Mar 8, 2022 · 7 comments · Fixed by #24

Comments

@lovetingyuan
Copy link

{
  "exports": {
    ".": {
      "import": ["./dist/module.mjs", "./dist/another-module.mjs"]
      "require": "./dist/require.js"
    },
  }
}

for the "import" condition exports, could it be an array? if fail to resolve "./dist/module.mjs", then try to resolve next value, "./dist/another-module.mjs"

@ljharb
Copy link

ljharb commented Mar 8, 2022

Yes, arrays are always supported in any location, afaik.

@lovetingyuan
Copy link
Author

Yes, arrays are always supported in any location, afaik.

But always the first file in array will be taken as result, right?

So, this lib does not care about whether the resolved file exsits on file-system, right?

@ljharb
Copy link

ljharb commented Mar 8, 2022

node will select the first one it can resolve (which yes, means it must exist on the filesystem), and I believe it will skip any unresolvable ones. I'm not sure what this lib does.

@privatenumber
Copy link

Seems Node.js hard errors on the first path that it cannot resolve:
nodejs/node#44282

Ideally, it would try the next path in the array.

@lukeed
Copy link
Owner

lukeed commented Jan 11, 2023

supporting this is a breaking change as it changes the API return type. saving this for the next major

personal opinion: it makes zero sense for this to be part of the specification. Author knows what/where each module can be found. Giving a consumer "alternatives" for how/where to find it + a "go pick & figure out which" seems very silly & undermines the "O(1) benefit" of ESM mapping.... If you're trying to offer a .tsx file instead of the .js equivalent (see linked vite issue), that is much better served by a custom condition (because I thought that was the point)

@privatenumber
Copy link

From reading the spec & Node.js discussions, the fallback array is only validated semantically so you still get very fast checks. For example, if the first entry is unsupported-protocol://file.js then it can be immediately be skipped to use the next one.

However, it's worth noting that TypeScript and Webpack both diverged from this behavior and actually try to resolve the paths before resolving the fallbacks.

For more details, I documented the behavior here in my own exports/imports resolver.

@lukeed
Copy link
Owner

lukeed commented Jan 12, 2023

I’ve followed too, thanks. I’m arguing that conceptually it doesn’t make sense. In your own example, there should never be a time where an author declares “lol://file.js” as a possible resolution. Every import should map to one thing depends on the conditions (including the how) it was imported

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants