oven-sh / bun

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

Dynamic imports not resolved correctly #6496

Open late-warrior opened 1 year ago

late-warrior commented 1 year ago

What version of Bun is running?

1.0.6+969da088f5db3258a803ec186012e30f992829b4

What platform is your computer?

Darwin 23.0.0 arm64 arm

What steps can reproduce the bug?

A dynamic import in code like below (trial/components) is not resolved correctly in the output bundle -

import React, { lazy } from "react";
import ReactDOM from "react-dom";

console.log("entrypoint");
const Components = lazy(() => import("./trial/components"));

function App() {
  return (
    <React.Suspense fallback={<span> Loading </span>}>
      <Components />
    </React.Suspense>
  );
}

ReactDOM.render(<App />, document.querySelector("#root"));

Complete reproducer in https://github.com/late-warrior/bun-dyn-import-bug.

What is the expected behavior?

// Seen
var Components = import_react.lazy(() => import("trial/components.js"));
// Expected
var Components = import_react.lazy(() => import("./trial/components.js"));

What do you see instead?

No response

Additional information

Tried setting a few options like --public-path and also varying the location of the entry point to see if that has an effect but could not figure out.

zigazajc007 commented 1 year ago

Same here, but I'm using FileSystemRouter from Bun.

const router = new Bun.FileSystemRouter({
  style: 'nextjs',
  dir: './server/endpoints'
});

Bun.serve({
  port: Settings.server.port,
  hostname: Settings.server.hostname,
  development: true,
  async fetch(req, server) {
    const url = new URL(req.url);
    const path = url.pathname;

    const match = router.match(path);
    if(!match) return Utils.jsonResponse(Errors.getJson(404), 404);

    const { src } = match;

    try{
      const module = await import('./endpoints/' + src);
      return await module.default(req, match);
    }catch{
      return Utils.jsonResponse(Errors.getJson(404), 404);
    }
  }
});

Works great with bun run server/index.ts, but it doesn't work after I bundle it with: bun build server/index.ts --compile --minify --outfile ./rabbitchat

Project structure: image