Output Solidity contract sizes with Hardhat.
Versions of this plugin prior to2.0.0
were released asbuidler-contract-sizer
.
npm install --save-dev hardhat-contract-sizer# oryarn add --dev hardhat-contract-sizer
Load plugin in Hardhat config:
require('hardhat-contract-sizer');
Add configuration under thecontractSizer
key:
option | description | default |
---|
alphaSort | whether to sort results table alphabetically (default sort is by contract size) | false |
runOnCompile | whether to output contract sizes automatically after compilation | false |
disambiguatePaths | whether to output the full path to the compilation artifact (relative to the Hardhat root directory) | false |
strict | whether to throw an error if any contracts exceed the size limit (may cause compatibility issues withsolidity-coverage ) | false |
only | Array ofString matchers used to select included contracts, defaults to all contracts iflength is 0 | [] |
except | Array ofString matchers used to exclude contracts | [] |
outputFile | file path to write contract size report | null |
unit | unit of measurement for the size of contracts, which can be expressed in 'B' (bytes), 'kB' (kilobytes) or 'KiB' (kibibytes) | KiB |
contractSizer:{alphaSort:true,disambiguatePaths:false,runOnCompile:true,strict:true,only:[':ERC20$'],}
Run the included Hardhat task to output compiled contract sizes:
npx hardhat size-contracts# oryarn run hardhat size-contracts
By default, the hardhatcompile
task is run before sizing contracts. This behavior can be disabled with the--no-compile
flag:
npx hardhat size-contracts --no-compile# oryarn run hardhat size-contracts --no-compile
Install dependencies via Yarn:
Setup Husky to format code on commit: