oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.17k stars 2.77k forks source link

Custom export conditions for Bun.resolveSync #1527

Open jacob-ebey opened 1 year ago

jacob-ebey commented 1 year ago

What is the problem this feature would solve?

When implementing things like bundler plugins it's common to receive input such as:

{
  "path": "the-mod",
  "resolveDir": "/some/absolute/dir"
}

with the requirement of resolving it to the version you would like bundled. The common way of accomplishing this in node-land without too much effort is to use the enhanced-resolve npm package maintained by the Webpack team.

What is the feature you are proposing to solve the problem?

I'm proposing that we extend Bun.resolve and Bun.resolveSync to accept a third parameter of options that allows customization of the resolution algorithm.

interface BunResolveOptions {
  extensions?: string[];
  mainFields: string[];
  conditions: string[];
}
interface Bun {
  resolveSync(moduleId: string, parent: string, options: BunResolveOptions): string;
}

This would allow resolution for assets such as CSS, JSON, JS, TS, etc when piecing together bun-bundler / bun runtime plugins. One less dependency to take on that Bun already knows how to handle internally in a more efficient way.

What alternatives have you considered?

N/A, please raise alternatives.

Jarred-Sumner commented 1 year ago

this sounds reasonable

aheissenberger commented 6 months ago

Would adding 'react-server' to conditions in this new option allow to import functions which need a different condition including all their imports and sub-imports? This would allow to run a react server component server without pre-bundling the parts which need a the 'react-server' condition or avoid running them in a second process.