|
| 1 | +--- |
| 2 | +prev: false |
| 3 | +--- |
| 4 | + |
| 5 | +# How to configure the compiler |
| 6 | + |
| 7 | +Solidity compilation in Hardhat is fully customizable. This guide explains the main ways in which compilation can be configured. |
| 8 | + |
| 9 | +## Configuring the compiler version and settings |
| 10 | + |
| 11 | +The simplest way to configure compilation is to specify the solc compiler version and, optionally, its settings: |
| 12 | + |
| 13 | +```typescript |
| 14 | +solidity: { |
| 15 | + version: "0.8.29", |
| 16 | + settings: { |
| 17 | + /* solc settings */ |
| 18 | + } |
| 19 | +}, |
| 20 | +``` |
| 21 | + |
| 22 | +One common use of settings is enabling optimizations. You can also define the number of runs, which affects how the optimizer balances code size and execution cost: |
| 23 | + |
| 24 | +```typescript |
| 25 | +solidity: { |
| 26 | + version: "0.8.29", |
| 27 | + settings: { |
| 28 | + optimizer: { |
| 29 | + enabled: true, |
| 30 | + runs: 200 |
| 31 | + } |
| 32 | + } |
| 33 | +}, |
| 34 | +``` |
| 35 | + |
| 36 | +Another use case is enabling the IR-based code generator. This compilation mode can be slower, but it enables more powerful optimizations: |
| 37 | + |
| 38 | +```typescript |
| 39 | +solidity: { |
| 40 | + version: "0.8.29", |
| 41 | + settings: { |
| 42 | + viaIR: true |
| 43 | + } |
| 44 | +}, |
| 45 | +``` |
| 46 | + |
| 47 | +The `settings` property accepts the same options supported by the chosen compiler version. For the full details, check [solc's documentation](https://docs.soliditylang.org/en/latest/). |
| 48 | + |
| 49 | +## Using multiple solidity versions |
| 50 | + |
| 51 | +Some projects need to compile different files with different `solc` versions. To enable this, Hardhat lets you define multiple compiler configurations using an extended format of the `solidity` property: |
| 52 | + |
| 53 | +```typescript |
| 54 | +solidity: { |
| 55 | + compilers: [ |
| 56 | + { |
| 57 | + version: "0.7.6" |
| 58 | + }, |
| 59 | + { |
| 60 | + version: "0.8.11" |
| 61 | + }, |
| 62 | + { |
| 63 | + version: "0.8.29", |
| 64 | + settings: { |
| 65 | + optimizer: { enabled: true } |
| 66 | + } |
| 67 | + } |
| 68 | + ] |
| 69 | +}, |
| 70 | +``` |
| 71 | + |
| 72 | +Hardhat compiles each Solidity file in the project using the **latest configured solc version** that is compatible with its version pragma of the file and its dependencies. |
| 73 | + |
| 74 | +For example, given the configuration above: |
| 75 | + |
| 76 | +- A file with `pragma solidity ^0.8.0` will be compiled with solc `0.8.29`, even though `0.8.11` is also compatible with it. |
| 77 | +- A file with `pragma solidity ^0.7.0` will use solc `0.7.6`, which is the only valid matching version. |
| 78 | + |
| 79 | +## Overriding configured compilers |
| 80 | + |
| 81 | +Some projects need to compile specific files using a different compiler version than Hardhat’s default choice. You can handle this with the `overrides` property: |
| 82 | + |
| 83 | +```typescript |
| 84 | +solidity: { |
| 85 | + compilers: [ |
| 86 | + /* configured compilers */ |
| 87 | + ], |
| 88 | + overrides: { |
| 89 | + "contracts/Foo.sol": { |
| 90 | + version: "0.8.11" |
| 91 | + } |
| 92 | + } |
| 93 | +}, |
| 94 | +``` |
| 95 | + |
| 96 | +In this case, `Foo.sol` will always be compiled with solc 0.8.11, regardless of the versions defined in `solidity.compilers`. |
| 97 | + |
| 98 | +Each entry in the `overrides` object maps a file to a custom compiler configuration. Just like the main configuration, only the `version` field is mandatory. |
| 99 | + |
| 100 | +You can use overrides even if you are using a single solc version, but you still need to use the extended format of the `solidity` property. For example, you can enable the optimizer only for a single file: |
| 101 | + |
| 102 | +```typescript |
| 103 | +solidity: { |
| 104 | + compilers: [ |
| 105 | + { |
| 106 | + version: "0.8.29", |
| 107 | + } |
| 108 | + ], |
| 109 | + overrides: { |
| 110 | + "contracts/Foo.sol": { |
| 111 | + version: "0.8.29", |
| 112 | + settings: { |
| 113 | + optimizer: { |
| 114 | + enabled: true |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +}, |
| 120 | +``` |
| 121 | + |
| 122 | +<!-- |
| 123 | +## Using remappings |
| 124 | +
|
| 125 | +TODO—blocked until dependency resolution is finalized. |
| 126 | +--> |
| 127 | + |
| 128 | +## Generating artifacts from npm dependencies |
| 129 | + |
| 130 | +By default, Hardhat generates compilation artifacts for all the contracts in your project, but not for those in the project’s npm dependencies. If you want to generate artifacts for a specific file in a dependency, you can use the `dependenciesToCompile` property: |
| 131 | + |
| 132 | +```typescript |
| 133 | +solidity: { |
| 134 | + version: "0.8.29", |
| 135 | + dependenciesToCompile: [ |
| 136 | + "some-dependency/contracts/SomeContract.sol" |
| 137 | + ] |
| 138 | +}, |
| 139 | +``` |
| 140 | + |
| 141 | +Artifacts can be used to deploy contracts or to obtain their ABIs, among other things. For example, once you’ve configured Hardhat to generate artifacts for `some-dependency/contracts/SomeContract.sol`, you can use that contract in a TypeScript test: |
| 142 | + |
| 143 | +```typescript |
| 144 | +const someContract = await viem.deployContract("SomeContract"); |
| 145 | +``` |
0 commit comments