Notable Methods & Exports
Internal utilities for custom bundling workflows, debugging compilation, or extending Tevm.
Core Bundler (@tevm/base-bundler)
import { bundler } from '@tevm/base-bundler'
import { createCache } from '@tevm/bundler-cache'
import { createSolc } from '@tevm/solc'
import { loadConfig } from '@tevm/config'
const tevmBundler = bundler(config, console, fileAccessObj, solcInstance, cacheInstance)
const { code, modules } = await tevmBundler.resolveTsModule(
'MyContract.s.sol',
process.cwd(),
false, // includeAst
true, // includeBytecode
)Available methods:
resolveTsModule- TypeScript outputresolveEsmModule- ESM outputresolveCjsModule- CommonJS outputresolveDts- TypeScript declarations
Each has a *Sync variant.
Import Resolution (@tevm/resolutions)
import { moduleFactory, resolveImports } from '@tevm/resolutions'
const imports = await resolveImports(
'/path/to/Contract.sol',
contractSourceCode,
{ '@openzeppelin/': 'node_modules/@openzeppelin/' },
['lib', 'node_modules'],
false, // async
)
const modules = await moduleFactory(
'/path/to/Contract.sol',
contractSourceCode,
remappings,
libraryPaths,
fileAccessObj,
false,
)resolveImports()- parse Solidity imports into absolute paths.moduleFactory()- build a dependency graph.
moduleFactory.js | resolveImports.js
Compilation (@tevm/compiler)
import { resolveArtifacts } from '@tevm/compiler'
const { artifacts, modules, asts } = await resolveArtifacts(
'MyContract.s.sol',
process.cwd(),
console,
config,
true, // includeAst
true, // includeBytecode
fileAccessObj,
solcInstance,
)Also has resolveArtifactsSync().
Caching (@tevm/bundler-cache)
import { createCache } from '@tevm/bundler-cache'
const cache = createCache('.tevm', fileAccessObj, process.cwd())
await cache.writeArtifacts(contractPath, compiledContracts)
const artifacts = await cache.readArtifacts(contractPath)
await cache.writeDts(contractPath, dtsContent)
const dts = await cache.readDts(contractPath)Methods: readArtifacts/writeArtifacts, readDts/writeDts, readMjs/writeMjs, readCjs/writeCjs. Each has a *Sync variant.
Configuration (@tevm/config)
import { loadConfig } from '@tevm/config'
const config = await loadConfig(process.cwd())
config.remappings
config.libs
config.cacheDirMerges Foundry remappings, tsconfig, and tevm.config.json.
Runtime (@tevm/runtime)
import { generateTevmBody, generateTevmBodyDts } from '@tevm/runtime'
const tsCode = generateTevmBody('MyContract', artifacts, 'tevm/contract', true)
const dtsCode = generateTevmBodyDts('MyContract', artifacts, 'tevm/contract', true)generateTevmBody.js | generateTevmBodyDts.js
Creating a Custom Bundler
import { loadConfig } from '@tevm/config'
import { createSolc } from '@tevm/solc'
import { createCache } from '@tevm/bundler-cache'
import { bundler } from '@tevm/base-bundler'
import fs from 'node:fs/promises'
const fileAccess = {
readFile: (path, enc) => fs.readFile(path, { encoding: enc }),
writeFile: fs.writeFile,
}
const config = await loadConfig(process.cwd())
const cache = createCache('.tevm', fileAccess, process.cwd())
const solc = await createSolc()
const myBundler = bundler(config, console, fileAccess, solc, cache)
const result = await myBundler.resolveTsModule('path/to/Contract.sol', process.cwd(), false, true)
console.log(result.code)
