vite icon indicating copy to clipboard operation
vite copied to clipboard

package.json "exports" field fallback array support

Open tarnishablec opened this issue 4 years ago • 10 comments

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

tarnishablec avatar Jul 29 '21 11:07 tarnishablec

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:

  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.

ygj6 avatar Jul 30 '21 07:07 ygj6

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

Niputi avatar Jul 30 '21 08:07 Niputi

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.

tarnishablec avatar Sep 23 '21 10:09 tarnishablec

There's a issue upstream for this: https://github.com/lukeed/resolve.exports/issues/17

bluwy avatar Mar 13 '22 14:03 bluwy

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

privatenumber avatar Aug 19 '22 00:08 privatenumber

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).

aleclarson avatar Oct 17 '22 03:10 aleclarson

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

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

privatenumber avatar Oct 17 '22 04:10 privatenumber

See #10504

aleclarson avatar Oct 17 '22 20:10 aleclarson

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

lukeed avatar Jan 15 '23 16:01 lukeed

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 avatar Jan 27 '23 08:01 bluwy

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 avatar Mar 12 '23 15:03 bluwy