Skip to content

Commit 179ab04

Browse files
authored
Add dist Test Setup / Some Developer Docs (#24)
* Add possibility to run dist tests * Add some developer docs (AI, shortened, proof-read) * Fix browser tests
1 parent 0cdf1d6 commit 179ab04

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,87 @@ type KZGProofWithCells = {
126126
cells: string[] // Array of 128 cells, each 2048 bytes
127127
}
128128
```
129+
130+
## Development
131+
132+
### Build Process
133+
134+
This section outlines the build scripts included in this repository to create distributable JavaScript/TypeScript packages from the WASM compilation.
135+
136+
#### Prerequisites
137+
138+
Ensure you have the following installed:
139+
- **Node.js** (v16+)
140+
- **npm**
141+
- **TypeScript** (installed as dev dependency)
142+
- **Babel** (installed as dev dependency)
143+
144+
#### Build Scripts Overview
145+
146+
The repository includes the following build scripts:
147+
148+
1. **`build`** - Main build command
149+
2. **`build:ts`** - TypeScript compilation to multiple targets
150+
3. **`transpileCJS`** - Babel transpilation for CommonJS compatibility
151+
4. **`fixRequire`** - Fix dynamic require statements
152+
5. **`fixWasmDirInNode`** - Fix WASM paths for Node.js builds
153+
6. **`fixWasmDirInWeb`** - Fix WASM paths for browser builds
154+
155+
#### Script Details
156+
157+
**`build:ts`** (`scripts/ts-build.sh`)
158+
- Copies WASM file to `dist/wasm/`
159+
- Compiles TypeScript to CommonJS (`dist/cjs/`)
160+
- Compiles TypeScript to ESM (`dist/esm/`)
161+
- Creates browser build (`dist/browser/`)
162+
163+
**`transpileCJS`**
164+
- Uses Babel to transpile `src/kzg.js` to `dist/cjs/`
165+
- Ensures CommonJS compatibility
166+
167+
**`fixRequire`**
168+
- Fixes dynamic require statements in CommonJS build
169+
- Replaces `\_require('url')` with `require('url')`
170+
171+
**`fixWasmDirInNode`**
172+
- Updates WASM file paths in Node.js builds
173+
- Changes `kzg-node.wasm``kzg.wasm``../wasm/kzg.wasm`
174+
175+
**`fixWasmDirInWeb`**
176+
- Updates WASM file paths in browser builds
177+
- Changes `kzg-web.wasm``../wasm/kzg.wasm`
178+
179+
#### Build Output Structure
180+
181+
```
182+
dist/
183+
├── cjs/ # CommonJS build for Node.js
184+
│ ├── index.js
185+
│ ├── kzg.js
186+
│ ├── loader.cjs
187+
│ └── package.json
188+
├── esm/ # ES Modules build
189+
│ ├── index.js
190+
│ ├── kzg.js
191+
│ ├── loader.mjs
192+
│ └── package.json
193+
├── browser/ # Browser build
194+
│ ├── index.js
195+
│ ├── kzg.js
196+
│ └── package.json
197+
└── wasm/ # WASM binary
198+
└── kzg.wasm
199+
```
200+
201+
#### Testing Builds
202+
203+
```bash
204+
# Test all builds
205+
npm test
206+
207+
# Test specific builds
208+
npm run test:dist # Test distribution build
209+
npm run test:src # Test source code
210+
npm run test:browser # Test browser environment
211+
```
212+

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
"version": "1.0.0",
44
"description": "a WASM compilation of c-kzg-4844",
55
"scripts": {
6-
"build": "npm run build:ts && npm run transpileCJS && npm run fixRequire ",
6+
"build": "rm -rf dist && npm run build:ts && npm run transpileCJS && npm run fixRequire ",
77
"build:ts": "scripts/ts-build.sh",
88
"transpileCJS": "rm dist/cjs/kzg.js && babel src/kzg.js -d dist/cjs",
99
"fixRequire": "sed -i.bak \"s/\\_require('url')/require('url')/g\" dist/cjs/kzg.js && rm dist/cjs/kzg.js.bak",
1010
"fixWasmDirInNode": "sed -i.bak 's#kzg-node.wasm#kzg.wasm#g' src/kzg.js && rm src/kzg.js.bak && sed -i.bak 's#kzg.wasm#../wasm/kzg.wasm#g' src/kzg.js && rm src/kzg.js.bak",
1111
"fixWasmDirInWeb": "sed -i.bak 's#kzg-web.wasm#../wasm/kzg.wasm#g' browser/kzg.js && rm browser/kzg.js.bak",
1212
"clean": "rm -rf dist && rm -rf node_modules",
13-
"test": "vitest run test/*",
13+
"test": "npm run test:dist && npm run test:src",
14+
"test:dist": "npm run build && TYPE=DIST npx vitest run test/*",
15+
"test:src": "TYPE=SRC npx vitest run test/*",
1416
"test:browser": "npx vitest run -c vitest.config.browser.ts test/*",
1517
"prepare": "npm run build"
1618
},

test/index.spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { describe, it, assert, beforeAll } from 'vitest'
2-
import { loadKZG } from '../src/index.js'
3-
import { bytesToHex, hexToBytes } from '../src/util.js'
2+
import { bytesToHex } from '../src/util.js'
3+
4+
// Conditional imports based on TYPE environment variable
5+
const TYPE = (typeof process !== 'undefined' && process.env?.TYPE) || 'SRC'
6+
7+
// Dynamic imports based on TYPE
8+
const { loadKZG } = TYPE === 'SRC'
9+
? await import('../src/index.js')
10+
: await import('../dist/esm/index.js')
411

512
const BYTES_PER_FIELD_ELEMENT = 32
613
const FIELD_ELEMENTS_PER_BLOB = 4096

0 commit comments

Comments
 (0)