Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ts: Create a minified browser build (#1108)
- Loading branch information
Showing
15 changed files
with
350 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,4 @@ ts/docs/ | |
cli/npm-package/anchor | ||
cli/npm-package/*.tgz | ||
docker-target | ||
.rollup.cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "es", | ||
sourcemap: true, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
import { isBrowser } from "./utils/common.js"; | ||
|
||
export { default as BN } from "bn.js"; | ||
export * as web3 from "@solana/web3.js"; | ||
export { | ||
default as Provider, | ||
getProvider, | ||
setProvider, | ||
NodeWallet as Wallet, | ||
} from "./provider.js"; | ||
export { default as Provider, getProvider, setProvider } from "./provider.js"; | ||
export { | ||
default as Coder, | ||
InstructionCoder, | ||
|
@@ -17,6 +14,11 @@ export { | |
export * from "./error.js"; | ||
export { Instruction } from "./coder/instruction.js"; | ||
export { Idl } from "./idl.js"; | ||
export { default as workspace } from "./workspace.js"; | ||
|
||
export * as utils from "./utils/index.js"; | ||
export * from "./program/index.js"; | ||
|
||
if (!isBrowser) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
zisko
|
||
exports.workspace = require("./workspace.js").default; | ||
exports.Wallet = require("./nodewallet.js").default; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Buffer } from "buffer"; | ||
import { Keypair, PublicKey, Transaction } from "@solana/web3.js"; | ||
import { Wallet } from "./provider"; | ||
|
||
/** | ||
* Node only wallet. | ||
*/ | ||
export default class NodeWallet implements Wallet { | ||
constructor(readonly payer: Keypair) {} | ||
|
||
static local(): NodeWallet { | ||
const process = require("process"); | ||
const payer = Keypair.fromSecretKey( | ||
Buffer.from( | ||
JSON.parse( | ||
require("fs").readFileSync(process.env.ANCHOR_WALLET, { | ||
encoding: "utf-8", | ||
}) | ||
) | ||
) | ||
); | ||
return new NodeWallet(payer); | ||
} | ||
|
||
async signTransaction(tx: Transaction): Promise<Transaction> { | ||
tx.partialSign(this.payer); | ||
return tx; | ||
} | ||
|
||
async signAllTransactions(txs: Transaction[]): Promise<Transaction[]> { | ||
return txs.map((t) => { | ||
t.partialSign(this.payer); | ||
return t; | ||
}); | ||
} | ||
|
||
get publicKey(): PublicKey { | ||
return this.payer.publicKey; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
import { isBrowser } from "../common"; | ||
|
||
export function decode(array: Uint8Array): string { | ||
const decoder = | ||
typeof TextDecoder === "undefined" | ||
? new (require("util").TextDecoder)("utf-8") // Node. | ||
: new TextDecoder("utf-8"); // Browser. | ||
const decoder = isBrowser | ||
? new TextDecoder("utf-8") // Browser https://caniuse.com/textencoder. | ||
: new (require("util").TextDecoder)("utf-8"); // Node. | ||
|
||
return decoder.decode(array); | ||
} | ||
|
||
export function encode(input: string): Uint8Array { | ||
const encoder = | ||
typeof TextEncoder === "undefined" | ||
? new (require("util").TextEncoder)("utf-8") // Node. | ||
: new TextEncoder(); // Browser. | ||
const encoder = isBrowser | ||
? new TextEncoder() // Browser. | ||
: new (require("util").TextEncoder)("utf-8"); // Node. | ||
return encoder.encode(input); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"include": [ | ||
"./src/**/*" | ||
], | ||
"compilerOptions": { | ||
"sourceMap": true, | ||
"declaration": true, | ||
"declarationMap": true, | ||
"allowSyntheticDefaultImports": true, | ||
"experimentalDecorators": true, | ||
"emitDecoratorMetadata": true, | ||
"noImplicitAny": false, | ||
"strictNullChecks": true, | ||
"esModuleInterop": true, | ||
"resolveJsonModule": true, | ||
"composite": true, | ||
"baseUrl": ".", | ||
"typeRoots": [ | ||
"types/", | ||
"node_modules/@types" | ||
], | ||
"paths": { | ||
"@solana/web3.js": [ | ||
"./node_modules/@solana/web3.js/lib" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
This breaks typescript autocomplete support for:
import * as anchor from '@project-serum/anchor'
to repro, anchor init and open the generated test file in vscode.