diff --git a/bitcore.js b/bitcore.js
index b55dc7b..c66b5c5 100644
--- a/bitcore.js
+++ b/bitcore.js
@@ -1,49 +1,50 @@
-/*
- * Bitcore bindings for the browser
- */
-
-
-
-module.exports.bignum = require('bignum');
-module.exports.base58 = require('base58-native');
-module.exports.buffertools = require('buffertools');
+/*
+One way to require files is this simple way:
+module.exports.Address = require('./Address');
-module.exports.config = require('./config');
-module.exports.const = require('./const');
-module.exports.Deserialize = require('./Deserialize');
-module.exports.log = require('./util/log');
-module.exports.networks = require('./networks');
-module.exports.util = require('./util/util');
+However, that will load all classes in memory even if they are not used.
+Instead, we can set the 'get' property of each class to only require them when
+they are accessed, saving memory if they are not used in a given project.
+*/
+var requireWhenAccessed = function(name, file) {
+ Object.defineProperty(module.exports, name, {get: function() {return require(file)}});
+};
-module.exports.EncodedData = require('./util/EncodedData');
-module.exports.VersionedData = require('./util/VersionedData');
-module.exports.Address = require('./Address');
-module.exports.Opcode = require('./Opcode');
-module.exports.Script = require('./Script');
-module.exports.Transaction = require('./Transaction');
-module.exports.Connection = require('./Connection');
-module.exports.Peer = require('./Peer');
-module.exports.Block = require('./Block');
-module.exports.ScriptInterpreter = require('./ScriptInterpreter');
-module.exports.Bloom = require('./Bloom');
-module.exports.KeyModule = require('./Key');
-module.exports.SINKey = require('./SINKey');
-module.exports.SIN = require('./SIN');
-module.exports.PrivateKey = require('./PrivateKey');
-module.exports.RpcClient = require('./RpcClient');
-module.exports.Wallet = require('./Wallet');
-module.exports.WalletKey = require('./WalletKey');
+requireWhenAccessed('bignum', 'bignum');
+requireWhenAccessed('base58', 'base58-native');
+requireWhenAccessed('buffertools', 'buffertools');
+requireWhenAccessed('config', './config');
+requireWhenAccessed('const', './const');
+requireWhenAccessed('Deserialize', './Deserialize');
+requireWhenAccessed('log', './util/log');
+requireWhenAccessed('networks', './networks');
+requireWhenAccessed('util', './util/util');
+requireWhenAccessed('EncodedData', './util/EncodedData');
+requireWhenAccessed('VersionedData', './util/VersionedData');
+requireWhenAccessed('Address', './Address');
+requireWhenAccessed('Opcode', './Opcode');
+requireWhenAccessed('Script', './Script');
+requireWhenAccessed('Transaction', './Transaction');
+requireWhenAccessed('Connection', './Connection');
+requireWhenAccessed('Peer', './Peer');
+requireWhenAccessed('Block', './Block');
+requireWhenAccessed('ScriptInterpreter', './ScriptInterpreter');
+requireWhenAccessed('Bloom', './Bloom');
+requireWhenAccessed('KeyModule', './Key');
+requireWhenAccessed('SINKey', './SINKey');
+requireWhenAccessed('SIN', './SIN');
+requireWhenAccessed('PrivateKey', './PrivateKey');
+requireWhenAccessed('RpcClient', './RpcClient');
+requireWhenAccessed('Wallet', './Wallet');
+requireWhenAccessed('WalletKey', './WalletKey');
module.exports.Buffer = Buffer;
if (typeof process.versions === 'undefined') {
// Browser specific
module.exports.bignum.config({EXPONENTIAL_AT: 9999999, DECIMAL_PLACES: 0, ROUNDING_MODE: 1});
- // module.exports.PeerManager = function () {
- // throw new Error('PeerManager not availabe in browser Bitcore, under .bitcore. Use it with: require(\'PeerManager\');');
- // };
}
else {
// Nodejs specific
- module.exports.PeerManager = require('./PeerManager');
+ requireWhenAccessed('PeerManager', './PeerManager');
}
diff --git a/browser/browserify.js b/browser/browserify.js
index d1b627f..21fa085 100644
--- a/browser/browserify.js
+++ b/browser/browserify.js
@@ -35,6 +35,7 @@ var modules = [
'PeerManager',
'PrivateKey',
'RpcClient',
+ 'Key',
'SIN',
'SINKey',
'Script',
@@ -58,9 +59,7 @@ var b = browserify(opts);
b.require('browserify-bignum/bignumber.js', {expose: 'bignum'} );
b.require('browserify-buffertools/buffertools.js', {expose:'buffertools'});
b.require('./bitcore', {expose: 'bitcore'});
-b.require('buffer', {expose: 'buffer'});
b.require('base58-native');
-b.require('./Key.js', {expose: 'KeyModule'});
b.require('./util/log');
b.require('./util/util');
b.require('./util/EncodedData');
diff --git a/examples/example.html b/examples/example.html
index 40a993c..951ef1e 100644
--- a/examples/example.html
+++ b/examples/example.html
@@ -12,7 +12,7 @@