ts: Import node modules if not browser (#436)
This commit is contained in:
parent
c7de475177
commit
a7f6af2cc6
|
@ -11,6 +11,7 @@ import {
|
||||||
SimulatedTransactionResponse,
|
SimulatedTransactionResponse,
|
||||||
Commitment,
|
Commitment,
|
||||||
} from "@solana/web3.js";
|
} from "@solana/web3.js";
|
||||||
|
import { isBrowser } from "./utils/common";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The network and wallet context used to send transactions paid for and signed
|
* The network and wallet context used to send transactions paid for and signed
|
||||||
|
@ -54,12 +55,14 @@ export default class Provider {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a `Provider` read from the `ANCHOR_PROVIDER_URL` envirnment
|
* Returns a `Provider` read from the `ANCHOR_PROVIDER_URL` environment
|
||||||
* variable
|
* variable
|
||||||
*
|
*
|
||||||
* (This api is for Node only.)
|
* (This api is for Node only.)
|
||||||
*/
|
*/
|
||||||
static env(): Provider {
|
static env(): Provider {
|
||||||
|
if (isBrowser) return;
|
||||||
|
|
||||||
const process = require("process");
|
const process = require("process");
|
||||||
const url = process.env.ANCHOR_PROVIDER_URL;
|
const url = process.env.ANCHOR_PROVIDER_URL;
|
||||||
if (url === undefined) {
|
if (url === undefined) {
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
/**
|
||||||
|
* Returns true if being run inside a web browser,
|
||||||
|
* false if in a Node process or electron app.
|
||||||
|
*/
|
||||||
|
export const isBrowser =
|
||||||
|
typeof window !== "undefined" && !window.process?.hasOwnProperty("type");
|
|
@ -3,6 +3,7 @@ import * as toml from "toml";
|
||||||
import { PublicKey } from "@solana/web3.js";
|
import { PublicKey } from "@solana/web3.js";
|
||||||
import { Program } from "./program";
|
import { Program } from "./program";
|
||||||
import { Idl } from "./idl";
|
import { Idl } from "./idl";
|
||||||
|
import { isBrowser } from "./utils/common";
|
||||||
|
|
||||||
let _populatedWorkspace = false;
|
let _populatedWorkspace = false;
|
||||||
|
|
||||||
|
@ -15,17 +16,14 @@ let _populatedWorkspace = false;
|
||||||
*/
|
*/
|
||||||
const workspace = new Proxy({} as any, {
|
const workspace = new Proxy({} as any, {
|
||||||
get(workspaceCache: { [key: string]: Program }, programName: string) {
|
get(workspaceCache: { [key: string]: Program }, programName: string) {
|
||||||
const fs = require("fs");
|
if (isBrowser) {
|
||||||
const process = require("process");
|
console.log("Workspaces aren't available in the browser");
|
||||||
|
|
||||||
if (
|
|
||||||
typeof window !== "undefined" &&
|
|
||||||
!window.process?.hasOwnProperty("type")
|
|
||||||
) {
|
|
||||||
// Workspaces are available in electron, but not in the browser, yet.
|
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fs = require("fs");
|
||||||
|
const process = require("process");
|
||||||
|
|
||||||
if (!_populatedWorkspace) {
|
if (!_populatedWorkspace) {
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue