Skip to content

Tutorial: Solidity bundler

Instead of running a separate Solidity compile step, copying ABI JSON files, and regenerating TypeScript bindings after every edit, the TEVM bundler compiles .sol files into typed ES modules at build time. Paired with @tevm/ts-plugin, your editor shows type errors and NatSpec hover documentation as you type.

  1. All bundler plugins ship inside the umbrella tevm package under tevm/bundler/*:

    Terminal window
    pnpm add tevm viem
  2. vite.config.ts
    import { defineConfig } from 'vite'
    import { vitePluginTevm } from 'tevm/bundler/vite-plugin'
    export default defineConfig({
    plugins: [vitePluginTevm()],
    })
  3. Add @tevm/ts-plugin to tsconfig.json so TypeScript resolves .sol imports, shows NatSpec comments on hover, and flags wrong argument types before build time:

    tsconfig.json
    {
    "compilerOptions": {
    "plugins": [{ "name": "@tevm/ts-plugin" }]
    }
    }

    In VS Code, run TypeScript: Select TypeScript Version and select Use Workspace Version.

    Add .tevm/ to .gitignore so compiled cache artifacts stay out of version control.

  4. Write a contract (imports from node_modules and Foundry remappings work automatically when configured in tevm.config.json):

    src/Counter.s.sol
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.20;
    contract Counter {
    uint256 public number;
    function setNumber(uint256 next) public {
    number = next;
    }
    }

    Import Counter directly and use its typed deploy, read, and write action creators with createMemoryClient():

    src/run-counter.ts
    import { Counter } from './Counter.s.sol'
    import { createMemoryClient, PREFUNDED_ACCOUNTS } from 'tevm'
    const client = createMemoryClient({
    account: PREFUNDED_ACCOUNTS[0],
    })
    try {
    const hash = await client.deployContract(Counter.deploy())
    const receipt = await client.getTransactionReceipt({ hash })
    const counter = Counter.withAddress(receipt.contractAddress!)
    await client.writeContract(counter.write.setNumber(42n))
    const value = await client.readContract(counter.read.number())
    console.log(value) // 42n
    } finally {
    await client.tevmClose()
    }
  5. Alternative: Static codegen without a bundler plugin

    Section titled “Alternative: Static codegen without a bundler plugin”

    If your framework runs strict type-checking outside the bundler (such as Next.js tsc builds), generate .ts contract modules alongside your .sol files instead:

    Terminal window
    npx tevm gen

The bundler compiles Solidity at build time and emits pure JavaScript/TypeScript Contract objects (abi, humanReadableAbi, read, write, events, and optional bytecode) with zero native dependencies. Use those compiled imports in React, Svelte, Astro, or Next.js frontends with viem, wagmi, or in-browser TEVM WASM.