oven-sh / bun

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

Bundler, jsxDEV in production breaking the build and the import #14978

Open shpaw415 opened 1 week ago

shpaw415 commented 1 week ago

What version of Bun is running?

1.1.34+5e5e7c60f

What platform is your computer?

Linux 6.8.0-48-generic x86_64 x86_64

What steps can reproduce the bug?

NODE_ENV = production

Bun.build() // output jsxDEV_7x81h0kn in a nested JSX Element

import(module).default() 
// output this error 
//  jsxDEV_7x81h0kn is not a function. 
// (In 'jsxDEV_7x81h0kn(DesignContainer, {}, void 0, !1, void 0, this)', 'jsxDEV_7x81h0kn' is undefined)
// part of the file

export default function DesignPage({ props }: { props: Props }) {
  return (
    <PropsContext.Provider value={props}>
      <Box
        sx={{
          width: "100%",
          minHeight: "85%",
          display: "flex",
          justifyContent: "center",
          paddingTop: 10,
        }}
      >
        <DesignContainer />
      </Box>
    </PropsContext.Provider>
  );
}

What is the expected behavior?

bun version 1.1.32 was working correctly

What do you see instead?

build and import output the same error

jsxDEV_7x81h0kn is not a function. (In 'jsxDEV_7x81h0kn(DesignContainer, {}, void 0, !1, void 0, this)', 'jsxDEV_7x81h0kn' is undefined)

Additional information

No response

shpaw415 commented 1 week ago

finaly found a workaround making

global.jsxDEV_7x81h0kn = jsxs

the fact that this is a const is friendly

shpaw415 commented 5 days ago

For fixing the Parser import problem i used a runtime plugin

import { plugin, type BunPlugin } from "bun";

const reactFix: BunPlugin = {
  name: "React-import-tmp-fix",
  setup(runtime) {
    runtime.onLoad(
      {
        filter: /\.tsx$/,
      },
      async (props) => {
        const file = await Bun.file(props.path).text();

        return {
          contents:
            `
            import { jsxDEV as jsxDEV_7x81h0kn } from "react/jsx-dev-runtime";
          ` + file,
        };
      }
    );
  },
};

plugin(reactFix);