Skip to content

Commit

Permalink
Add browser build
Browse files Browse the repository at this point in the history
This commit uses rollup, same as solana's web3 project uses to bundle a browser-specific build
  • Loading branch information
jeduan committed Dec 6, 2021
1 parent 101a717 commit 0a05cb3
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 8 deletions.
1 change: 1 addition & 0 deletions ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.rollup.cache/
12 changes: 11 additions & 1 deletion ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Anchor client",
"module": "./dist/esm/index.js",
"main": "./dist/cjs/index.js",
"browser": "./dist/browser/index.js",
"license": "(MIT OR Apache-2.0)",
"types": "dist/cjs/index.d.ts",
"homepage": "https://github.com/project-serum/anchor#readme",
Expand All @@ -21,8 +22,9 @@
"node": ">=11"
},
"scripts": {
"build": "rm -rf dist/ && yarn build:node",
"build": "rimraf dist/ && yarn build:node && yarn build:browser",
"build:node": "tsc && tsc -p tsconfig.cjs.json",
"build:browser": "rollup --config",
"lint:fix": "prettier src/** tests/** -w",
"lint": "prettier src/** tests/** --check",
"watch": "tsc -p tsconfig.cjs.json --watch",
Expand All @@ -49,6 +51,10 @@
"devDependencies": {
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@rollup/plugin-commonjs": "^21.0.1",
"@rollup/plugin-node-resolve": "^13.0.6",
"@rollup/plugin-replace": "^3.0.0",
"@rollup/plugin-typescript": "^8.3.0",
"@types/bn.js": "^4.11.6",
"@types/bs58": "^4.0.1",
"@types/crypto-hash": "^1.1.2",
Expand All @@ -63,9 +69,13 @@
"jest-config": "27.3.1",
"lint-staged": "^10.5.0",
"prettier": "^2.1.2",
"rimraf": "^3.0.2",
"rollup": "^2.60.2",
"rollup-plugin-terser": "^7.0.2",
"ts-jest": "^27.0.7",
"ts-jest-resolver": "^2.0.0",
"ts-node": "^9.0.0",
"tslib": "^2.3.1",
"typedoc": "^0.22.10",
"typescript": "^4.5.2"
}
Expand Down
54 changes: 54 additions & 0 deletions ts/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import nodeResolve from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";
import replace from "@rollup/plugin-replace";
import commonjs from "@rollup/plugin-commonjs";
import { terser } from "rollup-plugin-terser";

const env = process.env.NODE_ENV;

export default {
input: "src/index.ts",
plugins: [
commonjs(),
nodeResolve({
browser: true,
extensions: [".js", ".ts"],
dedupe: ["bn.js", "buffer"],
preferBuiltins: false,
}),
typescript({
tsconfig: "./tsconfig.base.json",
moduleResolution: "node",
outDir: "types",
target: "es2019",
outputToFilesystem: false,
}),
replace({
preventAssignment: true,
values: {
"process.env.NODE_ENV": JSON.stringify(env),
"process.env.BROWSER": JSON.stringify(true),
},
}),
terser(),
],
external: [
"@project-serum/borsh",
"@solana/web3.js",
"assert",
"base64-js",
"bn.js",
"bs58",
"buffer",
"camelcase",
"eventemitter3",
"js-sha256",
"pako",
"toml",
],
output: {
file: "dist/browser/index.js",
format: "cjs",
sourcemap: true,
},
};
Loading

0 comments on commit 0a05cb3

Please sign in to comment.