Bundler plugins
A bundler plugin turns each header and .rs file you import into a JavaScript module. For the web it also compiles the wasm while you develop and emits it with your bundle; in a React Native app, the app's Xcode and Gradle builds compile the native code.
Which plugin
| Bundler | Package | Builds for |
|---|---|---|
| Vite | @crossbind/plugin-vite | Browser and Apps |
| Webpack | @crossbind/plugin-webpack and @crossbind/plugin-webpack-loader | Browser |
| Rspack | @crossbind/plugin-webpack and @crossbind/plugin-webpack-loader | Browser |
| Rollup | @crossbind/plugin-rollup | Browser |
| Metro | @crossbind/plugin-metro and @crossbind/plugin-react-native | React Native, and Expo's web build |
Each one reads the project's crossbind.config.js, or crossbind.config.mjs, from the folder the bundler runs in. A page with no bundler builds with the CLI instead: see No bundler.
Vite
The plugin sits next to your framework's plugin; React, Vue and Svelte need nothing more.
Webpack
getRule() hands headers and .rs files to the loader package, which is why both are installed. getDevServerConfig() serves the compiled module in dev and sends the COOP/COEP headers; if you already have a devServer block, spread it in and keep its headers and setupMiddlewares.
await, and Node cannot require() a module that does: a CommonJS webpack.config.js that requires the plugin fails with ERR_REQUIRE_ASYNC_MODULE. Name it webpack.config.mjs, or set "type": "module".Rspack
Rspack takes the same two packages through its webpack-compatible plugin API.
Rollup
The Rollup plugin is the core the Vite plugin wraps; use it directly only for a plain Rollup build. Rollup has no dev server: rollup -c -w rebuilds the wasm when a native file changes, and nothing reloads a page.
The bundle step writes crossbind.js and crossbind.wasm into the output directory. Plain Rollup has no base path, so the generated code loads them from the site root.
Metro
@crossbind/plugin-metro turns header, .i and .rs imports into modules and writes the C++ bridge for each header while Metro bundles a React Native or Expo app; on iOS and Android the app's Xcode and Gradle builds compile the C++, through @crossbind/plugin-react-native. React Native shows its config, and Expo the web build it makes as well.
Another bundler
The plugins are thin: the work is done by functions crossbind exports, and @crossbind/plugin-rollup is the smallest complete plugin to copy. Each one starts by picking the browser target:
- Before the first transform,
buildDependencies()puts the library headers in place. - Imported headers become modules through
createBridgeFile()andgetCrossbindScript(); a library header such as@crossbind/port-zlib/zlib.hresolves throughgetDependFilePath(). - The bundle step compiles with
createLib()andbuildWasm()and emits the target'sjsName,wasmNameanddataTxtNamefromstate.config.paths.buildascrossbind.js,crossbind.wasmandcrossbind.data.txt. - In dev, the plugin serves those files and watches
state.config.paths.nativeandappSourceFiles(), whose imports decide what a library header binds.