oven-sh / bun

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

Bun.build problems #4407

Open CodeFromAnywhere opened 1 year ago

CodeFromAnywhere commented 1 year ago

What version of Bun is running?

0.8.1

What platform is your computer?

Darwin 21.6.0 arm64 arm

What steps can reproduce the bug?

This issue consists of two different problems that both prevent me from working with this library: https://github.com/ricky0123/vad

But the issues are not just for that library of course, I've had the first problem more often and I'm sure the second problem also happens in other libraries that require these type of dependencies.

Issue 1) Bun.build({ target: "browser" }) with dynamic import()s strange behaviour

I'm doing React SSR using Bun by building all react code with target browser. I adapted this example: https://github.com/bun-community/create-templates/tree/main/react-ssr

Building works like this:


const buildResult = await Bun.build({
  entrypoints: [hydratePath, ...Object.values(srcRouter.routes)],
  outdir: BUILD_DIR,
  target: "browser",
  splitting: true,
  root: SRC_DIR,
});

if (!buildResult.success) {
  console.log("BUILD FAILED");
  console.log(buildResult);
}

It seems that whenever I'm building for dynamic imports await import(...) for target "browser" results in buildfiles in the wrong place and sometimes overwriting the original code. E.g. import("abc") results in overwriting node_modules/abc/index.js being overwritten.

I can't therefore do dynamic imports with React.lazy and thus it's impossible to import code that uses window in it.

This occurred to me when trying to install http://github.com/ricky0123/vad

Issue 2) Bun.build can't install perf_hooks / worker_threads from onnxruntime-web

With things like web pack it should be possible but with Bun.build target "browser" it seems to crash.

After resolving the window error(s), things like http://github.com/ricky0123/vad don't build due to the inability of Bun.build to build it with target browser.

error: Could not resolve Node.js builtin: "perf_hooks". To use Node.js builtins, set target to 'node' or 'bun' /Users/king/os/packages/ui-visual-smart/recording-audio/node_modules/vad/node_modules/onnxruntime-web/dist/ort-web.node.js:6:534820 534947

error 2: error: Could not resolve Node.js builtin: "worker_threads". To use Node.js builtins, set target to 'node' or 'bun' /Users/king/os/packages/ui-visual-smart/recording-audio/node_modules/vad/node_modules/onnxruntime-web/dist/ort-web.node.js

Library: https://github.com/Microsoft/onnxruntime

What is the expected behavior?

These things work like they do in webpack

What do you see instead?

Strange behaviour and crash when building

Additional information

To reproduce, just try and install https://github.com/ricky0123/vad in a React SSR setup like https://www.npmjs.com/package/@bun-examples/react-ssr

masb0ymas commented 1 year ago

Same problem, but I just run the command like this :

bun build --target=bun ./src/index.ts --outdir ./dist

I didn't encounter any errors from worker threads, and it worked for me