oven-sh / bun

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

Compile Issue w/ @apollo/client #12518

Open ksylvest opened 4 months ago

ksylvest commented 4 months ago

What version of Bun is running?

1.1.18

What platform is your computer?

x86

What steps can reproduce the bug?

Given the following simple project:

package.json

{
  "dependencies": {
    "@apollo/client": "^3.10.8",
    "react": "^18.3.1",
    "react-dom": "^18.3.1",
    "typescript": "^5.5.3"
  },
  "devDependencies": {
    "@types/bun": "^1.1.6"
  }
}

demo.tsx

import { renderToString } from "react-dom/server";
import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client";

const client = new ApolloClient({ cache: new InMemoryCache() });

const App = () => <ApolloProvider client={client}>🚀</ApolloProvider>;

Bun.write(Bun.stdout, renderToString(<App />));
bun demo.tsx # 🚀
bun build demo.tsx --outfile demo.jsx
bun demo.jsx
22302 | Object.assign(exports_rehackt, import_react);
22303 | 
22304 | // node_modules/@apollo/client/react/context/ApolloContext.js
22305 | function getApolloContext() {
22306 |   invariant2("createContext" in exports_rehackt, 45);
22307 |   var context5 = undefined[contextKey];
                         ^
TypeError: undefined is not an object (evaluating '(void 0)[contextKey]')
      at getApolloContext (/Users/kevin/Desktop/bug/demo.jsx:22307:18)
      at ApolloProvider (/Users/kevin/Desktop/bug/demo.jsx:22324:24)
      at renderWithHooks (/Users/kevin/Desktop/bug/demo.jsx:5957:22)
      at renderIndeterminateComponent (/Users/kevin/Desktop/bug/demo.jsx:6015:21)
      at renderElement (/Users/kevin/Desktop/bug/demo.jsx:6178:13)
      at renderNodeDestructiveImpl (/Users/kevin/Desktop/bug/demo.jsx:6282:15)
      at renderNodeDestructive (/Users/kevin/Desktop/bug/demo.jsx:6262:20)
      at renderIndeterminateComponent (/Users/kevin/Desktop/bug/demo.jsx:6051:13)
      at renderElement (/Users/kevin/Desktop/bug/demo.jsx:6178:13)
      at renderNodeDestructiveImpl (/Users/kevin/Desktop/bug/demo.jsx:6282:15)

Checking in the ApolloClient, I see the following snippet that matches:

https://github.com/apollographql/apollo-client/blame/main/src/react/context/ApolloContext.ts#L30-L32

The compiled source includes:

// node_modules/@apollo/client/react/context/ApolloContext.js
function getApolloContext() {
  invariant2("createContext" in exports_rehackt, 45);
  var context5 = undefined[contextKey];

Unclear why it is using undefined[contextKey]. Further confusion arises based on the target. When setting target=browser or target=bun it fails, but for target=node it strangely works...

What is the expected behavior?

Bun prints "🚀".

What do you see instead?

22302 | Object.assign(exports_rehackt, import_react);
22303 | 
22304 | // node_modules/@apollo/client/react/context/ApolloContext.js
22305 | function getApolloContext() {
22306 |   invariant2("createContext" in exports_rehackt, 45);
22307 |   var context5 = undefined[contextKey];
                         ^
TypeError: undefined is not an object (evaluating '(void 0)[contextKey]')
      at getApolloContext (/Users/kevin/Desktop/bug/demo.jsx:22307:18)
      at ApolloProvider (/Users/kevin/Desktop/bug/demo.jsx:22324:24)
      at renderWithHooks (/Users/kevin/Desktop/bug/demo.jsx:5957:22)
      at renderIndeterminateComponent (/Users/kevin/Desktop/bug/demo.jsx:6015:21)
      at renderElement (/Users/kevin/Desktop/bug/demo.jsx:6178:13)
      at renderNodeDestructiveImpl (/Users/kevin/Desktop/bug/demo.jsx:6282:15)
      at renderNodeDestructive (/Users/kevin/Desktop/bug/demo.jsx:6262:20)
      at renderIndeterminateComponent (/Users/kevin/Desktop/bug/demo.jsx:6051:13)
      at renderElement (/Users/kevin/Desktop/bug/demo.jsx:6178:13)
      at renderNodeDestructiveImpl (/Users/kevin/Desktop/bug/demo.jsx:6282:15)

Additional information

No response

codehz commented 2 months ago

workaround: downgrade the rehackt to 0.0.6

codehz commented 2 months ago

https://npmdiff.dev/rehackt/0.0.6/0.1.0/package/index.js I think I found the reason

if (0) {
  module.exports = require("react");
}
...
Object.assign(module.exports, require("react"));

break the bun build

ksylvest commented 2 months ago

@codehz thank you sharing your findings around fixing!