Skip to content

Commit

Permalink
Add production bundle (#137)
Browse files Browse the repository at this point in the history
* Change wasm bundler to directly output to src/wasm

* Add dist/esm output, add dist/types output

* Add cjs build, add browser build

* Add single index.ts entrypoint

* Add package.json entry points, fix cjs type

* Rename scripts to bundle-wasm

* Fix tests

* Change bundle to single entrypoint

* Add starter hosted-wallet-ts

* Add src/tbdex.ts

* Remove ts-node

* Add sed hack
  • Loading branch information
KendallWeihe authored Oct 8, 2024
1 parent 51cb75f commit ee2994a
Show file tree
Hide file tree
Showing 22 changed files with 445 additions and 27 deletions.
28 changes: 28 additions & 0 deletions bound/typescript/bundle-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import esbuild from "esbuild";

const browserConfig = {
entryPoints : ["./src/index.ts"],
bundle : true,
format : "esm",
sourcemap : true,
minify : true,
platform : "browser",
target : ["chrome101", "firefox108", "safari16"],
define : {
"global": "globalThis",
},
};

// esm polyfilled bundle for browser
esbuild.build({
...browserConfig,
outfile: "dist/browser.mjs",
});

// iife polyfilled bundle for browser
esbuild.build({
...browserConfig,
format : "iife",
globalName : "tbDEX",
outfile : "dist/browser.js",
});
11 changes: 11 additions & 0 deletions bound/typescript/bundle-cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import esbuild from "esbuild";

esbuild.build({
entryPoints: ["./src/index.ts"],
bundle: true,
format: "cjs",
sourcemap: true,
minify: true,
platform: "node",
outfile: "dist/index.cjs",
});
12 changes: 12 additions & 0 deletions bound/typescript/bundle-esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import esbuild from "esbuild";

esbuild.build({
entryPoints: ["./src/index.ts"],
bundle: true,
format: "esm",
sourcemap: true,
minify: true,
platform: "node",
outfile: "dist/index.mjs",
});

Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ echo "module.exports = \`$(base64 -i pkg/tbdex_wasm_bg.wasm)\`;" > pkg/tbdex_was
{
sed -e '/Text..coder.*= require(.util.)/d' \
-e '/^const path = /,$d' pkg/tbdex_wasm.js
cat scripts/epilogue.js
cat bundle-wasm/epilogue.js
} > pkg/tbdex_wasm.js.new
mv pkg/tbdex_wasm.js.new pkg/tbdex_wasm.js

# also extend the typescript
cat scripts/epilogue.d.ts >> pkg/tbdex_wasm.d.ts
cat bundle-wasm/epilogue.d.ts >> pkg/tbdex_wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ esbuild.buildSync({
bundle: true,
minify: true,
entryPoints: [`${__dirname}/../pkg/tbdex_wasm.js`],
outfile: `${__dirname}/../dist/bundle.js`,
outfile: `${__dirname}/../src/wasm/generated.js`,
allowOverwrite: true,
})

fs.copyFileSync(`${__dirname}/../pkg/tbdex_wasm.d.ts`, `${__dirname}/../dist/tbdex_wasm.d.ts`)
fs.copyFileSync(`${__dirname}/../pkg/tbdex_wasm.d.ts`, `${__dirname}/../src/wasm/generated.d.ts`)
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions bound/typescript/declaration-hack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

sed -i '' "/export { default } from \"\.\/generated\";/r src/wasm/generated.d.ts" dist/index.d.ts && \
sed -i '' "/export { default } from \"\.\/generated\";/d" dist/index.d.ts
37 changes: 21 additions & 16 deletions bound/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
{
"name": "tbdex",
"version": "0.1.0",
"private": false,
"type": "module",
"description": "",
"types": "./dist/tbdex.d.ts",
"files": [
"dist",
"src"
],
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.js",
"types": "./dist/tbdex.d.ts"
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./browser": {
"import": "./dist/browser.mjs",
"require": "./dist/browser.js"
}
},
"files": [
"./dist/index.js",
"./dist/bundle.js",
"./dist/tbdex.d.ts"
],
"devDependencies": {
"@types/chai": "4.3.0",
"@types/mocha": "9.1.0",
Expand All @@ -28,15 +32,16 @@
"rimraf": "5.0.5",
"typescript": "^5.6.2"
},
"engines": {
"node": ">= 18"
},
"scripts": {
"clean": "rimraf pkg tests/compiled dist",
"build:wasm": "./scripts/build.sh && node ./scripts/bundle.js && cp dist/bundle.js src/wasm/generated.js && cp dist/tbdex_wasm.d.ts src/wasm/generated.d.ts",
"build": "tsc && cp -r src/wasm dist/bound/typescript/src",
"test:node": "npm run build && node tests/bundle-node.js && mocha",
"test:browser": "npm run build && node tests/bundle-browser.js && web-test-runner",
"build:browser": "node bundle-browser.js",
"build:cjs": "node bundle-cjs.js",
"build:esm": "node bundle-esm.js",
"build:types": "tsc --declaration --emitDeclarationOnly --outFile dist/index.d.ts && ./declaration-hack.sh",
"build:wasm": "./bundle-wasm/build.sh && node ./bundle-wasm/bundle.js",
"build": "npm run build:wasm && npm run build:types && npm run build:browser && npm run build:esm && npm run build:cjs",
"test:node": "tsc -p tests/tsconfig.json && node tests/bundle-node.js && mocha",
"test:browser": "tsc -p tests/tsconfig.json && node tests/bundle-browser.js && web-test-runner",
"test": "npm run test:node && npm run test:browser"
},
"dependencies": {}
Expand Down
33 changes: 33 additions & 0 deletions bound/typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// credentials
export * from "./credentials/presentation-definition";

// crypto
export * from "./crypto/in-memory-key-manager";
export * from "./crypto/jwk";
export * from "./crypto/key-manager";
export * from "./crypto/signer";

// dids
export * from "./dids/bearer-did";
export * from "./dids/did";
export * from "./dids/document";
export * from "./dids/portable-did";

// http
export * from "./http/exchanges";
export * from "./http/balances";
export * from "./http/offerings";

// http-client
export * from "./http-client/balances";
export * from "./http-client/exchanges";
export * from "./http-client/offerings";

// messages
export * from "./messages";

// resources
export * from "./resources";

// errors
export * from "./errors";
8 changes: 8 additions & 0 deletions bound/typescript/src/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import { OrderStatus } from "./order-status";
import { Quote } from "./quote";
import { Rfq } from "./rfq";

export * from "./cancel";
export * from "./close";
export * from "./order-instructions";
export * from "./order-status";
export * from "./order";
export * from "./quote";
export * from "./rfq";

export type Message =
| Rfq
| Quote
Expand Down
3 changes: 3 additions & 0 deletions bound/typescript/src/resources/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Balance } from "./balance";
import { Offering } from "./offering";

export * from "./balance"
export * from "./offering"

export type Resource = Balance | Offering;

export type ResourceMetadata = {
Expand Down
1 change: 1 addition & 0 deletions bound/typescript/src/tbdex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./index"
6 changes: 5 additions & 1 deletion bound/typescript/tests/bundle-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import { fileURLToPath } from 'node:url'

import esbuild from 'esbuild'
import path from 'node:path'
import fs from 'node:fs'

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

fs.copyFileSync(`${__dirname}/../src/wasm/generated.d.ts`, `${__dirname}/compiled/bound/typescript/src/wasm/generated.d.ts`)
fs.copyFileSync(`${__dirname}/../src/wasm/generated.js`, `${__dirname}/compiled/bound/typescript/src/wasm/generated.js`)

esbuild.buildSync({
entryPoints : [`${__dirname}/../dist/bound/typescript/tests/**/*.test.js`],
entryPoints: [`${__dirname}/compiled/bound/typescript/tests/**/*.test.js`],
format : 'esm',
bundle : true,
sourcemap : true,
Expand Down
6 changes: 5 additions & 1 deletion bound/typescript/tests/bundle-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import { fileURLToPath } from 'node:url'

import esbuild from 'esbuild'
import path from 'node:path'
import fs from 'node:fs'

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

fs.copyFileSync(`${__dirname}/../src/wasm/generated.d.ts`, `${__dirname}/compiled/bound/typescript/src/wasm/generated.d.ts`)
fs.copyFileSync(`${__dirname}/../src/wasm/generated.js`, `${__dirname}/compiled/bound/typescript/src/wasm/generated.js`)

esbuild.buildSync({
entryPoints: [`${__dirname}/../dist/bound/typescript/tests/**/*.test.js`],
entryPoints: [`${__dirname}/compiled/bound/typescript/tests/**/*.test.js`],
format: 'esm',
bundle: true,
sourcemap: true,
Expand Down
19 changes: 19 additions & 0 deletions bound/typescript/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"strict": true,
"useUnknownInCatchVariables": false,
"outDir": "compiled",
"declaration": true,
"declarationMap": true,
"declarationDir": "compiled/types",
"sourceMap": true,
},
"include": [
"../src",
".",
],
"exclude": [
"./compiled"
]
}
11 changes: 11 additions & 0 deletions bound/typescript/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"lib": [
"DOM",
"ES2015",
],
"target": "ES5",
"outDir": "dist/cjs"
}
}
14 changes: 9 additions & 5 deletions bound/typescript/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "node",
"outDir": "./dist",
"resolveJsonModule": true,
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
"skipLibCheck": true,
"declaration": false,
"outDir": "dist"
},
"include": [
"src/**/*",
"tests/**/*"
"src"
],
"exclude": [
"node_modules"
"dist",
"node_modules",
"pkg",
"bundle-wasm",
"tests"
]
}
Loading

0 comments on commit ee2994a

Please sign in to comment.