oven-sh / bun

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

Bun.build plugin onResolve treeshaking issue #2952

Open samuelgja opened 1 year ago

samuelgja commented 1 year ago

What version of Bun is running?

0.6.2

What platform is your computer?

Darwin 22.4.0 arm64 arm

What steps can reproduce the bug?

Testing new Bun.build api and found a treeshaking issue.

When have compilation code something like this (with custom plugin and onResolve defined):

import type { BunPlugin } from "bun";

const myPlugin: BunPlugin = {
  target: "browser",
  name: "YAML loader",
  setup(build) {
    build.onResolve({ filter: /.*/ }, async (args) => {
      return null; // expect when return null, nothing is changed.
    });
  },
};
const result = await Bun.build({
  entrypoints: ["index.ts"],
  minify: true,
  outdir: "dist",
  plugins: [myPlugin],
});

And code to be compiled is something like this (index.ts):

import { isArray } from "lodash-es";

export default function isArray2(value: any): boolean {
  return isArray(value);
}

Build result is ~93kb.

When I remove build.onResolve, it works as expected and output result is ~72b.

What is the expected behavior?

So Idk what is expected behaviour. I think if using custom onResolve it should still provide me treeshaking support, at least when return null or undefined from onResolve.

What do you see instead?

No response

Additional information

But good job for this anyway. Can't wait for more stuff :)

Jarred-Sumner commented 1 year ago

oh interesting

it's losing the "sideEffects": false flag after the onResolve plugin has been run

Jarred-Sumner commented 1 year ago

(to be clear, i am confirming this is a bug, but explaining that the cause is "sideEffects": false being lost)