oven-sh / bun

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

`Bun.build` return the original path of entry point #15033

Open BjornTheProgrammer opened 3 weeks ago

BjornTheProgrammer commented 3 weeks ago

What is the problem this feature would solve?

With the following build,

const result = await Bun.build({
    entrypoints: ['index.js', 'second.js'],
    naming: '[hash].[ext]'
})

This can return the following object

result: {
  outputs: [
    BuildArtifact (entry-point) {
      path: "./s0d6tbms.js",
      loader: "ts",
      kind: "entry-point",
      hash: "s0d6tbms",
      Blob (86 bytes) {
        type: "text/javascript;charset=utf-8"
      },
      sourcemap: null
    }, BuildArtifact (entry-point) {
      path: "./x2hwc40c.js",
      loader: "ts",
      kind: "entry-point",
      hash: "x2hwc40c",
      Blob (86 bytes) {
        type: "text/javascript;charset=utf-8"
      },
      sourcemap: null
    }
  ],
  success: true,
  logs: [],
}

Because the order is not respected, if I added naming for the input, it would be impossible to know which one came in first, and from where.

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

The feature I am proposing to solve the indeterminate path problem is adding the initially provided entrypoint, if the output is of kind entry-point

BuildArtifact (entry-point) {
    path: "./x2hwc40c.js",
    loader: "ts",
    kind: "entry-point",
    hash: "x2hwc40c",
    Blob (86 bytes) {
        type: "text/javascript;charset=utf-8"
    },
    originalPath: "./index.js"
    sourcemap: null
}

What alternatives have you considered?

The alternative is to run Bun.build on each entry point separately. But this might cause code duplication and is slower. Additionally it makes it so the paths aren't resolved relative to one another.

paperdave commented 2 weeks ago

for #14763, i added entry_point_index as a field on OutputFile, which is basically the native data structure for BuildArtifact

Image

this could be used to trivially add a sourceFile property to these, or otherwise we should just implement --metafile like esbuild does (generate a json file with build metadata)

BjornTheProgrammer commented 2 weeks ago

I did look at that in the source code, and it makes sense to just add it. Then again maybe metafile is the better way to go since it makes Bun.build less compatibility breaking with esbuild (not sure if that really is a concern).

paperdave commented 2 weeks ago

i suspect we can do both since metafile has its own purposes

BjornTheProgrammer commented 2 weeks ago

@paperdave I've submitted pull request #15167 which implements this feature.