# Bundler Plugins Tevm bundler plugins enable direct Solidity imports in TypeScript/JavaScript, compiling them into type-safe Tevm Contract instances at build time. :::note The bundler is optional. You can also generate contract types with `npx tevm gen` (see [Codegen Approach](#codegen-approach)). ::: ## Available Plugins | Bundler | Plugin Import Path | Repository | |---------|-------------------|------------| | Vite | `tevm/bundler/vite-plugin` | [@tevm/vite-plugin](https://github.com/evmts/tevm-bundler/tree/main/packages/vite) | | Webpack | `tevm/bundler/webpack-plugin` | [@tevm/webpack-plugin](https://github.com/evmts/tevm-bundler/tree/main/packages/webpack) | | Rollup | `tevm/bundler/rollup-plugin` | [@tevm/rollup-plugin](https://github.com/evmts/tevm-bundler/tree/main/packages/rollup) | | ESBuild | `tevm/bundler/esbuild-plugin` | [@tevm/esbuild-plugin](https://github.com/evmts/tevm-bundler/tree/main/packages/esbuild) | | Bun | `tevm/bundler/bun-plugin` | [@tevm/bun-plugin](https://github.com/evmts/tevm-bundler/tree/main/packages/bun) | | Rspack | `tevm/bundler/rspack-plugin` | [@tevm/rspack-plugin](https://github.com/evmts/tevm-bundler/tree/main/packages/rspack) | All plugins share a configuration interface. ## Prerequisites & Key Points - **Optional**: `npx tevm gen` works without any bundler integration. - **TypeScript**: Add `@tevm/ts-plugin` to `tsconfig.json` for editor support (NatSpec hovers, go-to-definition). - **`.s.sol` for bytecode**: regular `.sol` produces ABI only; `.s.sol` includes deployable bytecode. - **Cache**: artifacts go in `.tevm/` — add it to `.gitignore`. - **Foundry/remappings**: configure in `tevm.config.json`. - **Next.js**: type-checker conflicts; use [codegen](#codegen-approach) or disable `next.config` typechecks. ## Plugin Configuration Bundler plugins accept a `solc` version string. Global settings (Foundry, libs, remappings) live in `tevm.config.json`. ## Bundler-Specific Setup :::code-group ```ts [vite.config.ts] import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' import { vitePluginTevm } from 'tevm/bundler/vite-plugin' export default defineConfig({ plugins: [react(), vitePluginTevm({ solc: '0.8.20' })], }) ``` ```js [webpack.config.js] const { webpackPluginTevm } = require('tevm/bundler/webpack-plugin') module.exports = { plugins: [webpackPluginTevm({ solc: '0.8.20' })], } ``` ```js [rollup.config.js] import { rollupPluginTevm } from 'tevm/bundler/rollup-plugin' export default { plugins: [rollupPluginTevm({ solc: '0.8.20' })], } ``` ```js [build.js (esbuild)] const { build } = require('esbuild') const { esbuildPluginTevm } = require('tevm/bundler/esbuild-plugin') build({ entryPoints: ['src/index.js'], outdir: 'dist', bundle: true, plugins: [esbuildPluginTevm({ solc: '0.8.20' })], }) ``` ```ts [plugins.ts (bun)] import { plugin } from 'bun' import { bunPluginTevm } from 'tevm/bundler/bun-plugin' plugin(bunPluginTevm({ solc: '0.8.20' })) ``` ```js [rspack.config.js] const { rspackPluginTevm } = require('tevm/bundler/rspack-plugin') module.exports = { plugins: [rspackPluginTevm({ solc: '0.8.20' })], } ``` ::: For Bun, register the plugin in `bunfig.toml`: ```toml preload = ["./plugins.ts"] [test] preload = ["./plugins.ts"] ``` ## TypeScript Plugin ```json { "compilerOptions": { "plugins": [{ "name": "@tevm/ts-plugin" }] } } ``` In VSCode, switch to the workspace TypeScript version. ## How the Bundler Works All Tevm bundler plugins share a unified core (`@tevm/base-bundler`): - **Import detection & resolution**: scans `.sol` imports, merges tsconfig paths, foundry remappings, and `tevm.config.json`. - **Compilation**: runs solc on the dependency graph; `.s.sol` includes bytecode, `.sol` is ABI-only. - **Code generation**: emits a TS module exporting a Tevm Contract (with `.read`, `.write`, abi, optional bytecode). - **Caching**: stores results in `.tevm/`. - **LSP**: `@tevm/ts-plugin` references bundler outputs for IDE features. ## Configuration with tevm.config.json ```json { "foundryProject": true, "libs": ["./lib", "./node_modules"], "remappings": { "foo": "vendored/foo" }, "debug": false, "cacheDir": ".tevm", "jsonAsConst": ["**/*.abi.json"] } ``` | Option | Type | Description | |--------|------|-------------| | `foundryProject` | `boolean \| string` | Enable Foundry (`true`) or path to `foundry.toml` | | `libs` | `string[]` | Library paths for Solidity imports | | `remappings` | `Record` | Custom import remappings | | `debug` | `boolean` | Extra debug logs and files in `.tevm` | | `cacheDir` | `string` | Artifact location (default: `.tevm`) | | `jsonAsConst` | `string \| string[]` | Glob patterns for `as const` JSON imports | ## Codegen Approach ```bash npx tevm gen ``` Generates `.ts` files next to each `.sol`. Use this when a bundler hook is impractical (e.g., Next.js with strict typechecking). ## Troubleshooting - **Red underlines on `.sol` imports**: ensure `@tevm/ts-plugin` is in tsconfig and you're using the workspace TS version. - **Next.js type errors**: disable typechecking or use codegen. - **File not found**: check libs/remappings; set `foundryProject: true` for Foundry projects. - **Stale cache**: delete `.tevm/`. ## Examples and Further Reading - [Examples](https://github.com/evmts/tevm/tree/main/examples) - Vite, Next, Bun, ESBuild - [Tevm Contract Reference](/reference/contract) - [Wagmi + Tevm examples](https://github.com/evmts/tevm/tree/main/examples) - [base-bundler globals](https://github.com/evmts/tevm-bundler/tree/main/packages/base-bundler/docs/globals.md)