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

package.json "exports" field fallback array support #4439

Closed
4 tasks done
tarnishablec opened this issue Jul 29, 2021 · 11 comments
Closed
4 tasks done

package.json "exports" field fallback array support #4439

tarnishablec opened this issue Jul 29, 2021 · 11 comments
Labels
enhancement New feature or request p2-to-be-discussed Enhancement under consideration (priority)

Comments

@tarnishablec
Copy link

tarnishablec commented Jul 29, 2021

Clear and concise description of the problem

Currently vite does not support fallback array in "exports" field in package.json, vite does support array type but when first entrypoint is not accessable, an error will show. webpack 5 has already has a implementation in Alternatives.

Suggested solution

add fallback array support

Alternative

No response

Additional context

"exports": {
    "default": {
      "import": [
        "./dist/index.esm.js",
        "./src/index.tsx"
      ],
      "require": "./dist/index.umd.js"
    }
  },

Validations

@ygj6
Copy link
Member

ygj6 commented Jul 30, 2021

Vite uses resolve.exports to resolve exports

// resolve exports field with highest priority
// using https://github.com/lukeed/resolve.exports
if (data.exports) {
entryPoint = resolveExports(data, '.', options, targetWeb)
}

But it does not support alternatives, it will only return the first path in the array:
https://github.com/lukeed/resolve.exports/blob/master/src/index.js#L5-L24
I did not find other suitable library to resolve exports to replace resolve.exports.

Alternatives is a great feature, I want to contribute it,I have two thoughts:

  1. should let resolve.exports return an array and let vite choose a path that can be used
  2. resolve.exports is no longer used, but let vite to implement the resolve of exports

@Shinigami92 @patak-js
what do you think, give me some suggestions.

@Niputi
Copy link
Contributor

Niputi commented Jul 30, 2021

I think this should be made at the library.
I remember reading a conversation on twitter Evan had that there should be a library for making a standard way of reading the exports and resolve.exports got made for this purpose.
with a github search, it can be seen at wmr, marko, dvlp and vite uses this library

@tarnishablec
Copy link
Author

tarnishablec commented Sep 23, 2021

resolve.exports

resolve.exports is a NO file-system reliance library, so I think technically it can not implement alternatives itself.

let resolve.exports return an array and let vite choose a path that can be used will be more practicable.

@bluwy
Copy link
Member

bluwy commented Mar 13, 2022

There's a issue upstream for this: lukeed/resolve.exports#17

@bluwy bluwy added the bug: upstream Bug in a dependency of Vite label Mar 13, 2022
@privatenumber
Copy link
Contributor

privatenumber commented Aug 19, 2022

FYI Currently, Node.js does not fallback to trying the next path if one is unresolvable. In addition to Webpack, TypeScript seems to support this too (source).

Discussion here:
nodejs/node#37928 (comment)

@aleclarson
Copy link
Member

The maintainer of resolve.exports seems too busy to care for the package lately. We should fork the package and give it a facelift. It could use a rewrite in TypeScript, Prettier formatting, Vitest suite, and less code golfing (easier to read).

@privatenumber
Copy link
Contributor

privatenumber commented Oct 17, 2022

I actually implemented exports/imports resolution with all the bugs from resolve.exports fixed:

https://github.com/privatenumber/resolve-pkg-maps

@aleclarson
Copy link
Member

See #10504

aleclarson added a commit to aleclarson/vite that referenced this issue Nov 14, 2022
@lukeed
Copy link
Contributor

lukeed commented Jan 15, 2023

This is supported in resolve.exports@next, which will be released as 2.0 stable in the next few days

@bluwy bluwy added enhancement New feature or request and removed bug: upstream Bug in a dependency of Vite labels Jan 27, 2023
@bluwy
Copy link
Member

bluwy commented Jan 27, 2023

Note: Vite has upgraded to resolve.exports 2.0, so this feature request is unblocked. I'm putting this to the Vite discussion though to decide if we should:

  1. Follow node's behaviour of only validating the path, e.g. ['std:module', './file.js'], pick second.
  2. Follow webpack's & typescript's behaviour of hitting disk checking existence, e.g. ['./missing.js', './file.js'], pick second.

I'm leaning towards no1 to follow the spec, but also I'm not sure why packages would export missing files if no2 is supported.

@bluwy bluwy added this to Team Board Jan 27, 2023
@github-project-automation github-project-automation bot moved this to Discussing in Team Board Jan 27, 2023
@bluwy bluwy added the p2-to-be-discussed Enhancement under consideration (priority) label Feb 18, 2023
@patak-dev patak-dev moved this from Discussing to P2 - 2 in Team Board Feb 23, 2023
@bluwy bluwy moved this from P2 - 2 to P2 - 3 in Team Board Mar 9, 2023
@patak-dev patak-dev moved this from P2 - 3 to Rejected in Team Board Mar 9, 2023
@patak-dev patak-dev moved this from Rejected to P2 - 3 in Team Board Mar 9, 2023
@bluwy
Copy link
Member

bluwy commented Mar 12, 2023

In the last team meeting, we've decided to follow node as it defines the exports behaviour. And since node doesn't implement file existence checks, we will also not implement it.

In the original issue description, package.json's publishConfig should be used instead to define the fields before and after publish (dev and build).

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Mar 12, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Mar 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request p2-to-be-discussed Enhancement under consideration (priority)
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

8 participants