Tree shaking Support Guide
Step 1: Enable Production Mode
- Use the
productionmode configuration option to enable various optimizations including minification and tree shaking. Set your webpack.config:
'mode':'production'
Step 2: Configure sideEffects Property
- Add a
sideEffectsproperty to your project'spackage.jsonfile:
"sideEffects": false
note
For further information about sideEffects see webpack docs
Step 3: Set tsconfig Module to ES2015
- Set your tsconfig module to
ES2015or higher to supportimports, because tree shaking does not work withrequire:
"module": "ES2015"
Step 4: Use Specific Packages
-
Use the specific packages which you need,
For example, if you need
web.eth:
- JavaScript
- TypeScript
const { Web3Eth } = require('web3-eth');
// ...
import { Web3Eth } from 'web3-eth';
// ...
If you only need a few functions from web3-utils:
- JavaScript
- TypeScript
const { numberToHex, hexToNumber } = require('web3-utils');
// ...
import { numberToHex, hexToNumber } from 'web3-utils';
// ...
Example React App
You can find an example app with tree shaking here.