Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monorepo: Switch to hybrid ESM/CJS Build #2685

Merged
merged 33 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
534ee3d
Exemplary ESM build for Block package, extended config ts-build.sh sc…
holgerd77 May 10, 2023
cd33a31
Skip ESM build for non-converted packages
holgerd77 May 10, 2023
7d116f5
Temporarily skip client build (build failures)
holgerd77 May 10, 2023
35801db
More fixes
holgerd77 May 10, 2023
45c877c
Yet another fix
holgerd77 May 10, 2023
3120188
Fixes
holgerd77 May 10, 2023
d57c5b0
Simplify exports in package.json (review suggestion)
holgerd77 May 11, 2023
5c05881
Updated rlp package (extra: bin folder adjustment)
holgerd77 May 11, 2023
e441e3b
Update Util package
holgerd77 May 11, 2023
8d7cb60
Update Common package
holgerd77 May 11, 2023
dc42390
Update Trie package (folder references still missing to ease Trie ref…
holgerd77 May 11, 2023
5d411d7
Open up Util provider path to fix block, tx fromRpcProvider test test…
holgerd77 May 11, 2023
ed398ec
devp2p: internalize k-bucket package (no types yet)
holgerd77 May 15, 2023
b4d5704
Update Tx package
holgerd77 May 15, 2023
5c68d90
Update Ethash package
holgerd77 May 15, 2023
d741b1a
Update Blockchain package
holgerd77 May 15, 2023
2e296b6
update StateManager package
holgerd77 May 15, 2023
ce18af5
Update VM package, remove direct EVM error msg imports, rename EvmErr…
holgerd77 May 15, 2023
c465f59
Fix direct EVM -> VM imports
holgerd77 May 15, 2023
983ee3d
Rename all tsconfig.prod.json occurrences to tsconfig.prod.cjs.json
holgerd77 May 15, 2023
f7c55cc
Fix VM BlockBuilder related client build failures, add BlockBuilder t…
holgerd77 May 15, 2023
9297fe4
Rebuild package-lock.json
holgerd77 May 15, 2023
798a7aa
Fix
holgerd77 May 15, 2023
177c1cc
Fix VM benchmark block fromRPC() import
holgerd77 May 11, 2023
96148a8
devp2p: remove ms dependency + types
holgerd77 May 15, 2023
cfe67da
Update EVM package
holgerd77 May 15, 2023
7d026c6
Various fixes and type export additions (blockchain, VM)
holgerd77 May 16, 2023
f6177f6
More fixes
holgerd77 May 16, 2023
dc018a9
More fixes
holgerd77 May 16, 2023
c5773b1
Even more fixes
holgerd77 May 16, 2023
ef72830
Fix VM Karma browser test run
holgerd77 May 16, 2023
be94048
Remove testdouble dep from block/tx tests
acolytec3 May 16, 2023
8e70475
Remove unused baseUrl config option from tsconfig
acolytec3 May 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions config/cli/ts-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,58 @@ dim() {

build_node() {
blue "[Node build] "
echo "Using tsconfig.prod.json"
echo "Using tsconfig.prod.cjs.json"

echo "> tsc --build ./tsconfig.prod.json"
echo "> tsc --build ./tsconfig.prod.cjs.json"
printf "${BLUE}[Node build] Working... "

tsc --build ./tsconfig.prod.json
tsc --build ./tsconfig.prod.cjs.json
green "DONE"

echo "\n";
}

build_esm() {
if [ -f ./tsconfig.prod.esm.json ];
then
blue "[ESM build] "
echo "Using tsconfig.prod.esm.json"

echo "> tsc --build ./tsconfig.prod.esm.json"
printf "${BLUE}[ESM build] Working... "

tsc --build ./tsconfig.prod.esm.json
green "DONE"
else
echo "Skipping ESM build (no config available)."
fi
echo "\n";
}

post_build_fixes() {
blue "[Post Build Fixes]"
if [ -f ./dist/esm/index.js ];
then
echo "Adding ./dist/cjs/package.json"
rm -f ./dist/cjs/package.json
cat <<EOT >> ./dist/cjs/package.json
{
"type": "commonjs"
}
EOT
echo "Adding ./dist/esm/package.json"
rm -f ./dist/esm/package.json
cat <<EOT >> ./dist/esm/package.json
{
"type": "module"
}
EOT
else
echo "Skipping post build fixes (no ESM setup yet)."
fi
echo "\n";
}

build_browser() {
if [ -f ./tsconfig.browser.json ];
then
Expand Down Expand Up @@ -77,4 +118,6 @@ then
build_browser
else
build_node
build_esm
post_build_fixes
fi
File renamed without changes.
6 changes: 6 additions & 0 deletions config/tsconfig.prod.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "esnext"
}
}
16 changes: 0 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/block/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ module.exports = function (config) {
acornOptions: {
ecmaVersion: 12,
},
resolve: {
alias: {
'@ethereumjs/util/provider': '../util/dist/cjs/provider.js',
},
},
ignore: ['c-kzg'],
},
},
Expand Down
13 changes: 9 additions & 4 deletions packages/block/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
},
"license": "MPL-2.0",
"author": "mjbecze (mb@ethdev.com)",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
}
},
"files": [
"dist",
"src"
Expand Down Expand Up @@ -46,8 +52,7 @@
"ethereum-cryptography": "^2.0.0"
},
"devDependencies": {
"c-kzg": "^2.0.4",
"testdouble": "^3.17.2"
"c-kzg": "^2.0.4"
},
"engines": {
"node": ">=16"
Expand Down
34 changes: 21 additions & 13 deletions packages/block/test/from-rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Chain, Common, Hardfork } from '@ethereumjs/common'
import { bytesToPrefixedHexString, hexStringToBytes, randomBytes } from '@ethereumjs/util'
import { bytesToHex, equalsBytes } from 'ethereum-cryptography/utils'
import * as tape from 'tape'
import * as td from 'testdouble'

import { blockFromRpc } from '../src/from-rpc'
import { blockHeaderFromRpc } from '../src/header-from-rpc'
Expand Down Expand Up @@ -189,21 +188,30 @@ tape('[fromJsonRpcProvider]', async (t) => {
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
const provider = 'https://my.json.rpc.provider.com:8545'

const fakeFetch = async (_url: string, req: any) => {
if (
req.method === 'eth_getBlockByHash' &&
req.params[0] === '0x1850b014065b23d804ecf71a8a4691d076ca87c2e6fb8fe81ee20a4d8e884c24'
) {
const block = await import(`./testdata/infura15571241wtxns.json`)
return block
const realFetch = global.fetch
//@ts-expect-error -- Typescript doesn't like us to replace global values
global.fetch = async (_url: string, req: any) => {
const json = JSON.parse(req.body)
if (json.params[0] === '0x1850b014065b23d804ecf71a8a4691d076ca87c2e6fb8fe81ee20a4d8e884c24') {
const txData = await import(`./testdata/infura15571241wtxns.json`)
return {
json: () => {
return {
result: txData,
}
},
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, interesting. I would have never gotten to this solution. 🙂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It means we're no longer testing the Node16 route for fetching data (using https instead of the fetch API but there are separate tests in util for testing that. The goal here is to test the constructor so this seemed like the least impactful route to that.

} else {
return null // Infura returns null if no block is found
return {
json: () => {
return {
result: null, // This is the value Infura returns if no transaction is found matching the provided hash
}
},
}
}
}

const providerUtils = require('@ethereumjs/util/dist/provider')
td.replace<any>(providerUtils, 'fetchFromProvider', fakeFetch)

const blockHash = '0x1850b014065b23d804ecf71a8a4691d076ca87c2e6fb8fe81ee20a4d8e884c24'
const block = await Block.fromJsonRpcProvider(provider, blockHash, { common })
t.equal(
Expand All @@ -220,6 +228,6 @@ tape('[fromJsonRpcProvider]', async (t) => {
'returned correct error message'
)
}
td.reset()
global.fetch = realFetch
t.end()
})
16 changes: 16 additions & 0 deletions packages/block/tsconfig.prod.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../config/tsconfig.prod.cjs.json",
"compilerOptions": {
"lib": ["dom"],
"rootDir": "src",
"outDir": "dist/cjs",
"composite": true
},
"include": ["src/**/*.ts"],
"references": [
{ "path": "../rlp/tsconfig.prod.cjs.json" },
{ "path": "../trie/tsconfig.prod.cjs.json" },
{ "path": "../tx/tsconfig.prod.cjs.json" },
{ "path": "../util/tsconfig.prod.cjs.json" }
]
}
16 changes: 16 additions & 0 deletions packages/block/tsconfig.prod.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../config/tsconfig.prod.esm.json",
"compilerOptions": {
"lib": ["dom"],
"rootDir": "src",
"outDir": "dist/esm",
"composite": true
},
"include": ["src/**/*.ts"],
"references": [
{ "path": "../rlp/tsconfig.prod.cjs.json" },
{ "path": "../trie/tsconfig.prod.cjs.json" },
{ "path": "../tx/tsconfig.prod.cjs.json" },
{ "path": "../util/tsconfig.prod.cjs.json" }
]
}
16 changes: 0 additions & 16 deletions packages/block/tsconfig.prod.json
holgerd77 marked this conversation as resolved.
Outdated
Show resolved Hide resolved

This file was deleted.

10 changes: 8 additions & 2 deletions packages/blockchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
},
"license": "MPL-2.0",
"author": "mjbecze <mjbecze@gmail.com>",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
}
},
"files": [
"dist",
"src"
Expand Down
3 changes: 1 addition & 2 deletions packages/blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import { genesisStateRoot } from './genesisStates'
import {} from './utils'

import type { Consensus } from './consensus'
import type { GenesisState } from './genesisStates'
import type { BlockchainInterface, BlockchainOptions, OnBlock } from './types'
import type { BlockchainInterface, BlockchainOptions, GenesisState, OnBlock } from './types'
import type { BlockData } from '@ethereumjs/block'
import type { CliqueConfig } from '@ethereumjs/common'
import type { BigIntLike, DB, DBObject } from '@ethereumjs/util'
Expand Down
15 changes: 1 addition & 14 deletions packages/blockchain/src/genesisStates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,7 @@ import { Account, isHexPrefixed, toBytes, unpadBytes } from '@ethereumjs/util'
import { keccak256 } from 'ethereum-cryptography/keccak'
import { hexToBytes } from 'ethereum-cryptography/utils'

import type { PrefixedHexString } from '@ethereumjs/util'

export type StoragePair = [key: PrefixedHexString, value: PrefixedHexString]

export type AccountState = [
balance: PrefixedHexString,
code: PrefixedHexString,
storage: Array<StoragePair>,
nonce: PrefixedHexString
]

export interface GenesisState {
[key: PrefixedHexString]: PrefixedHexString | AccountState
}
import type { AccountState, GenesisState } from '../types'

/**
* Derives the stateRoot of the genesis block based on genesis allocations
Expand Down
3 changes: 2 additions & 1 deletion packages/blockchain/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { Blockchain } from './blockchain'
export { CasperConsensus, CliqueConsensus, Consensus, EthashConsensus } from './consensus'
export { BlockchainInterface, BlockchainOptions } from './types'
export { DBOp, DBSaveLookups, DBSetBlockOrHeader, DBSetHashToNumber, DBSetTD } from './db/helpers'
export * from './types'
export * from './utils'
16 changes: 14 additions & 2 deletions packages/blockchain/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { Consensus } from './consensus'
import type { GenesisState } from './genesisStates'
import type { Block, BlockHeader } from '@ethereumjs/block'
import type { Common } from '@ethereumjs/common'
import type { DB, DBObject } from '@ethereumjs/util'
import type { DB, DBObject, PrefixedHexString } from '@ethereumjs/util'

export type OnBlock = (block: Block, reorg: boolean) => Promise<void> | void

Expand Down Expand Up @@ -80,6 +79,19 @@ export interface BlockchainInterface {
getCanonicalHeadBlock?(): Promise<Block>
}

export type StoragePair = [key: PrefixedHexString, value: PrefixedHexString]

export type AccountState = [
balance: PrefixedHexString,
code: PrefixedHexString,
storage: Array<StoragePair>,
nonce: PrefixedHexString
]

export interface GenesisState {
[key: PrefixedHexString]: PrefixedHexString | AccountState
}

/**
* This are the options that the Blockchain constructor can receive.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/blockchain/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { addHexPrefix, bigIntToHex, isHexPrefixed } from '@ethereumjs/util'

import type { GenesisState } from './genesisStates'
import type { GenesisState } from './types'

/**
* Parses the geth genesis state into Blockchain {@link GenesisState}
Expand Down
17 changes: 17 additions & 0 deletions packages/blockchain/tsconfig.prod.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "../../config/tsconfig.prod.cjs.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist/cjs",
"types": ["node"],
"typeRoots": ["node_modules/@types"],
"composite": true
},
"include": ["src/**/*.ts", "src/**/*.json"],
"references": [
{ "path": "../common/tsconfig.prod.cjs.json" },
{ "path": "../ethash/tsconfig.prod.cjs.json" },
{ "path": "../rlp/tsconfig.prod.cjs.json" },
{ "path": "../util/tsconfig.prod.cjs.json" }
]
}
Loading