vite
                                
                                 vite copied to clipboard
                                
                                    vite copied to clipboard
                            
                            
                            
                        package.json "exports" field fallback array support
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
- [x] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Vite uses resolve.exports to resolve exports
https://github.com/vitejs/vite/blob/024a2de63df60a4037f9f7b13a0bc6e2b0d41fb6/packages/vite/src/node/plugins/resolve.ts#L550-L554
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:
- should let resolve.exportsreturn an array and letvitechoose a path that can be used
- resolve.exportsis no longer used, but let- viteto implement the resolve of- exports
@Shinigami92 @patak-js what do you think, give me some suggestions.
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
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.
There's a issue upstream for this: https://github.com/lukeed/resolve.exports/issues/17
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: https://github.com/nodejs/node/issues/37928#issuecomment-808833604
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).
I actually implemented exports/imports resolution with all the bugs from resolve.exports fixed:
https://github.com/privatenumber/resolve-pkg-maps
See #10504
This is supported in resolve.exports@next, which will be released as 2.0 stable in the next few days
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:
- Follow node's behaviour of only validating the path, e.g. ['std:module', './file.js'], pick second.
- 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.
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).