oven-sh / bun

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

Bun.build with a plugin results in unexpected output across resulting js files #13950

Closed j4w8n closed 1 month ago

j4w8n commented 1 month ago

What version of Bun is running?

1.1.26

What platform is your computer?

WSL

What steps can reproduce the bug?

  1. Create a plugin.
  2. Import a function to the plugin; where the imported function has it's own imports or globals.
  3. Create an index.ts file that imports something, along with any other code you want.
  4. Build index.ts, using the plugin in the process.

What is the expected behavior?

The imports and globals from the plugin's top-level imports should not be included in the built index.js file, nor any other one-off built files.

What do you see instead?

With a plugin including something like this,

import { someFunction } from 'somewhere'

...
async setup (build) {
  await someFunction()
}
...

where someFunction has a global import or a global [const | var | let],

import { statSync } from 'node:fs'

const cwd = process.cwd()
...

and an index.ts file of

import { anything } from 'something'

anything()

/* Bun.serve may not be a requirement, but I'm using it. */
Bun.serve({
  fetch(req) { return new Response('Hi!') }
})

the resulting out/index.js file will include built code for statSync and cwd. Is this expected?

Furthermore, if the someFunction() code has it's own calls to Bun.build(), for one-off files I need to build, and those files have even a single import of their own, their resulting filename.js output will also include the statSync and cwd built code. If those one-off files don't have any imports, the statSync and cwd code is not included with their built code.

Additional information

No response

j4w8n commented 1 month ago

Running the build with the option target set to either "bun" or "node" resolves the issue. Running it with the default of browser is what causes the issue.