Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hedaukartik opened this issue Feb 28, 2024 · 5 comments
Closed
Labels
bug Something isn't working bundler Something to do with the bundler

Comments

@hedaukartik
Copy link

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?

@hedaukartik hedaukartik added the bug Something isn't working label Feb 28, 2024
@Electroid Electroid added the bundler Something to do with the bundler label Feb 28, 2024
@hedaukartik
Copy link
Author

@Electroid @Jarred-Sumner any idea what are we missing for above expected behavior?

@shpaw415
Copy link

shpaw415 commented Mar 1, 2024

quick fix Bun.build({ publicPath: "./" });

@hedaukartik
Copy link
Author

Thanks @shpaw415 worked!

@shpaw415
Copy link

shpaw415 commented Mar 2, 2024

if you look for thanking me. could you please put a star on my repo.. https://github.com/shpaw415/bunext

@hedaukartik
Copy link
Author

@shpaw415 You can also check my recent work on React CSR. I appreciate your feedback. Thanks.
https://github.com/hedaukartik/bun-react-csr-base

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working bundler Something to do with the bundler
Projects
None yet
Development

No branches or pull requests

3 participants