Skip to content

Lazy and Dynamic import in react app build using bun not working correctly #9151

@hedaukartik

Description

@hedaukartik

What version of Bun is running?

1.0.29

What platform is your computer?

Darwin 21.6.0 arm64 arm

What steps can reproduce the bug?

A lazy import in code like below (layout/home) is not resolved correctly in the output bundle -

import * as ReactDOM from "react-dom/client";
import { lazy } from "react";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
const Home = lazy(() => import("./layouts/home"));

const router = createBrowserRouter([
  {
    path: "/",
    element: <Home />,
    errorElement: <ErrorPage />,
  }
]);

const rootDiv = document.getElementById("root") as HTMLElement;
const root = ReactDOM.createRoot(rootDiv);

root.render(<RouterProvider router={router} />);

Build javascript code -

Bun.build({
    entrypoints: [import.meta.dir + "/src/index.tsx"],
    outdir: BUILD_PATH,
    minify: true,
    splitting: true,
  });

What is the expected behavior?

In the built output file (/index.js) -

// Expected
V.lazy(() => import("./layouts/home/index.js")),

What do you see instead?

// Seen
V.lazy(() => import("layouts/home/index.js")),

Additional information

Is there any solution to fix this issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingbundlerSomething to do with the bundler

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions