@0x/sol-compiler

raw JSON →
7.0.1 verified Fri May 01 auth: no javascript

Solidity compiler wrapper and artifactor that provides smart recompilation, project-wide compilation, per-file Solidity version management, and configurable artifact output. Current stable version: 7.0.1. Release cadence: irregular, part of 0x monorepo. Key differentiators: intelligent caching (only recompiles changed contracts), automatic version resolution from pragma statements, and standard input description support for flexible artifacts. Ships TypeScript types. Requires Node >=6.12. Suitable for both CLI and programmatic use.

error Cannot find module '@0x/typescript-typings/types'
cause Missing typeRoots configuration or package not installed.
fix
Add "typeRoots": ["node_modules/@0x/typescript-typings/types", "node_modules/@types"] to tsconfig.json, or install @0x/typescript-typings.
error Error: solc version '0.8.0' not found
cause Specified solcVersion is not locally installed or available.
fix
Either install the version via npm (npm install solc@0.8.0) or add it to the solcVersions array.
error TypeError: Compiler is not a constructor
cause Importing as default instead of named import.
fix
Use import { Compiler } from '@0x/sol-compiler' or const { Compiler } = require('@0x/sol-compiler').
deprecated useDockerisedSolc option is deprecated; use solcVersion and solcVersions instead.
fix Remove useDockerisedSolc and specify solcVersion/solcVersions.
breaking CompilerOptions interface changed in v7; artifactsDir no longer defaults to a relative path.
fix Explicitly provide artifactsDir in options.
gotcha Compiling with solcVersion that contains a caret (^) may fail if the exact version is not installed locally.
fix Use exact version or define solcVersions array.
gotcha TypeRoots configuration required for TypeScript projects using tsconfig.json.
fix Add "typeRoots": ["node_modules/@0x/typescript-typings/types", "node_modules/@types"] to tsconfig.json.
breaking Default export removed in v5; use named import { Compiler }.
fix Switch to named import { Compiler } from '@0x/sol-compiler'.
npm install solidity-compiler
yarn add solidity-compiler
pnpm add solidity-compiler

Shows basic programmatic usage: instantiate Compiler with paths and options, then call compile().

import { Compiler } from '@0x/sol-compiler';

async function compile(): Promise<void> {
  const compiler = new Compiler({
    contractsDir: './contracts',
    artifactsDir: './artifacts',
    compilerSettings: {
      outputSelection: {
        '*': {
          '*': ['abi', 'evm.bytecode.object'],
        },
      },
    },
    solcVersion: '^0.8.0',
    solcVersions: ['0.8.0', '0.8.1'],
    useDockerisedSolc: false,
    isOfflineMode: false,
    shouldCompileIndividually: true,
    shouldSaveStandardInput: true,
    standardInput: { language: 'Solidity', sources: {}, settings: {} },
  });

  await compiler.compile();
  console.log('Compilation succeeded');
}

compile().catch(console.error);