browser-metro is a unique, client-side JavaScript and TypeScript bundler designed to run entirely within a web browser, typically leveraging a Web Worker for performance. Inspired by React Native's Metro bundler, it provides features like Hot Module Replacement (HMR), React Refresh support, and integration with Expo Router for file-based routing. It manages modules through a VirtualFS, performs rapid compilation of TypeScript and JSX via Sucrase transforms, and supports on-demand bundling of npm packages through an external ESM server (e.g., `https://esm.reactnative.run`). As of version 1.0.15, it emphasizes rapid development feedback loops in browser-based playgrounds and development environments. Its key differentiator is its completely client-side operation, removing the need for a Node.js build server for many common development tasks, making it ideal for interactive coding environments and sandboxes.
npm install browser-metroVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up a basic `Bundler` with a `VirtualFS` to bundle TypeScript code, including a dependency. It outputs the resulting self-executing JavaScript bundle, ready for execution in a browser context. It also highlights the necessary `packageServerUrl` for external npm modules.
Optimize your virtual file system content, use smaller entry points, and consider pre-bundling external dependencies if feasible. Leveraging Web Workers for the bundling process (as `browser-metro` itself does) can mitigate some impact. Monitor bundle size and parsing times.
Ensure network connectivity to the configured `packageServerUrl`. For critical applications or offline usage, consider self-hosting an ESM package server or pre-packaging essential npm dependencies directly into your `VirtualFS`.
For HMR with React Refresh, use `IncrementalBundler`, specify `reactRefreshTransformer` in your config, and enable `hmr: { enabled: true, reactRefresh: true }`. Ensure your application's runtime correctly receives and applies the `hmrUpdate` objects (e.g., via `iframe.postMessage`) and that React components follow Fast Refresh rules (e.g., named exports).Verify the import path is correct and the module exists in the `VirtualFS`. Ensure `resolver.sourceExts` in your `BundlerConfig` includes the file extension of the module (e.g., `['ts', 'tsx', 'js', 'jsx']`). If importing an npm package, check network connectivity to your `packageServerUrl` and that the package exists on the remote server.
Ensure you are importing and passing a valid transformer, such as `typescriptTransformer` or `reactRefreshTransformer`, to your `BundlerConfig`. If creating a custom transformer, it must adhere to the `Transformer` interface specified by `browser-metro`.
Ensure all React components are named exports or default exports of named functions. Verify your HMR client-side logic correctly processes `hmrUpdate` objects from `IncrementalBundler` and applies patches to the iframe or target environment. Check for duplicate React installations if possible.
No dependency data recorded yet.