Open nestarz opened 4 months ago
I would welcome such a change. It would be particularly useful during compilation using deno compile
, in order to tap into a runtime
condition which only imports packages/package paths needed during runtime. A prominent example is Svelte: excluding svelte/compiler
during runtime is only possible with export conditions, and that's how we're currently doing it with Bun.
What is the problem this feature would solve?
Deno currently does not provide a way to specify custom export conditions when importing npm packages. This makes it impossible to import non-default exports that are conditionally exported based on certain conditions, such as the "react-server" condition used by React Server Components.
See: https://nodejs.org/api/packages.html#conditional-exports https://github.com/reactjs/rfcs/blob/main/text/0227-server-module-conventions.md
What is the feature you are proposing to solve the problem?
Add support for a config flag in Deno, similar to the one supported by Node.js and Bun. This flag would allow specifying custom export conditions when importing npm packages.
For example, in Bun, thanks to this issue https://github.com/oven-sh/bun/issues/8990 you can do:
Then, if you import an npm package, it will follow the conditions specified in the package's
exports
field.Ideally, it would be great to set up this condition inside the
deno.json
config, so that it can be used by Deno Deploy as well. An other option would be to specify it directly in the npm specifier, so that no config file are needed ?What alternatives have you considered?
One workaround would be to directly import the specific file path, like:
However, this doesn't work if the file is not explicitly exported in the package's
exports
field:I think this is intentional from the Deno team to not expose what's not exposed through the
exports
option. But then, Deno should provide a way to define the export conditions like Bun and Node does.