oven-sh / bun

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

Trying to import react native by striping flow types using a runtime plugin #10083

Open ceopaludetto opened 2 months ago

ceopaludetto commented 2 months ago

What version of Bun is running?

1.1.2+c8d072c2a

What platform is your computer?

Darwin 23.4.0 arm64 arm

What steps can reproduce the bug?

I was trying to import react-native inside a bun test, so I wrote a plugin to strip flow types from react-native packages, but when I import react-native the module get empty.

  1. Create the plugin
    
    // plugin.ts
    import type { BunPlugin } from "bun";

import { plugin } from "bun"; import stripTypes from "flow-remove-types";

function stripFlowTypesPlugin(): BunPlugin { return { name: "strip-flow-types", setup: (build) => { build.onLoad({ filter: /node_modules\/react-native.*.jsx?$/ }, async ({ path, loader }) => { console.log(Processing ${path});

    const text = await Bun.file(path).text();
    const contents = stripTypes(text).toString();

    return { contents, loader: loader ?? "js" };
  });
},

}; }

await plugin(stripFlowTypesPlugin());


2. Add into preload section in `bunfig.toml`:
```toml
# bunfig.toml
[test]
preload = ["./plugin.ts"]
  1. Create a stub test (I'm using dynamic import to avoid errors):
    
    // example.spec.tsx
    import { describe, expect, it } from "bun:test"

describe("Example", () => { it("should", async () => { const mod = await import("react-native") console.log(mod) // prints -> Module {} }) })


### What is the expected behavior?

React native should be imported correctly and the modules should be listed

### What do you see instead?

An empty module
<img width="961" alt="Screenshot 2024-04-08 at 16 39 28" src="https://github.com/oven-sh/bun/assets/30508218/7a2623a8-1d6a-40d4-8aff-141fcbee5b66">

### Additional information
When I try to use `require("react-native")` I get this:

TypeError: require() async module "/Users/carlos/Documents/Projetos/Javascript/bun-test-react-native/node_modules/react-native/index.js" is unsupported. use "await import()" instead.

isaachinman commented 1 month ago

Any update or advice on this? React Native is truly terrible to compile with any tool, but I'd love to be able to use Bun.