Skip to content

Commit 52bfa4b

Browse files
committed
Add "Configuring the compiler" guide
1 parent 305d97a commit 52bfa4b

File tree

2 files changed

+141
-2
lines changed

2 files changed

+141
-2
lines changed

src/content/hardhat3-alpha/learn-more/_dirinfo.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ order:
55
href: /comparison
66
- title: Alpha version limitations
77
href: /alpha-limitations
8-
# - title: Solidity Tests
9-
# href: /solidity-tests
8+
- title: Configuring the compiler
9+
href: /configuring-the-compiler
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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 speed:
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+
version: "0.7.6"
57+
}, {
58+
version: "0.8.11"
59+
}, {
60+
version: "0.8.29",
61+
settings: {
62+
optimizer: { enabled: true }
63+
}
64+
}]
65+
},
66+
```
67+
68+
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.
69+
70+
For example, given the configuration above:
71+
72+
- 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.
73+
- A file with `pragma solidity ^0.7.0` will use solc `0.7.6`, which is the only valid matching version.
74+
75+
## Overriding configured compilers
76+
77+
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:
78+
79+
```typescript
80+
solidity: {
81+
compilers: [
82+
/* configured compilers */
83+
],
84+
overrides: {
85+
"contracts/Foo.sol": {
86+
version: "0.8.11"
87+
}
88+
}
89+
},
90+
```
91+
92+
In this case, `Foo.sol` will always be compiled with solc 0.8.11, regardless of the versions defined in `solidity.compilers`.
93+
94+
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.
95+
96+
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:
97+
98+
```typescript
99+
solidity: {
100+
compilers: [{
101+
version: "0.8.29",
102+
}],
103+
overrides: {
104+
"contracts/Foo.sol": {
105+
version: "0.8.29",
106+
settings: {
107+
optimizer: {
108+
enabled: true
109+
}
110+
}
111+
}
112+
}
113+
},
114+
```
115+
116+
<!--
117+
## Using remappings
118+
119+
TODO—blocked until dependency resolution is finalized.
120+
-->
121+
122+
## Generating artifacts from dependencies
123+
124+
By default, Hardhat generates compilation artifacts for all the contracts in your project, but not for those in the project’s dependencies. If you want to generate artifacts for a specific file in a dependency, you can use the `dependenciesToCompile` property:
125+
126+
```typescript
127+
solidity: {
128+
version: "0.8.29",
129+
dependenciesToCompile: [
130+
"some-dependency/contracts/SomeContract.sol"
131+
]
132+
},
133+
```
134+
135+
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/SomeContract.sol`, you can use that contract in a TypeScript test:
136+
137+
```typescript
138+
const someContract = await viem.deployContract("SomeContract");
139+
```

0 commit comments

Comments
 (0)