Gruntfile produces proper stand-alone files for browser.

This commit is contained in:
ricmoo 2017-02-27 17:35:23 -05:00
parent 7394e83eeb
commit 8286c63ddc
13 changed files with 142 additions and 63 deletions

@ -4,34 +4,29 @@ var through = require('through');
var undef = "module.exports = undefined;";
var empty = "module.exports = {};";
// The elliptic package.json is only used for its version
var ellipticPackage = require('elliptic/package.json');
ellipticPackage = JSON.stringify({ version: ellipticPackage.version });
// We already have a random Uint8Array browser/node safe source
var brorand = "var randomBytes = require('ethers-utils').randomBytes; module.exports = function(length) { return randomBytes(length); };";
var transforms = {
"ripemd.js": "module.exports = {ripemd160: null}",
"precomputed/secp256k1.js": undef,
"elliptic/lib/elliptic/precomputed/secp256k1.js": undef,
"elliptic/curve/edwards.js": empty,
"elliptic/curve/mont.js": empty,
"elliptic/lib/elliptic/eddsa/.*": empty,
"elliptic/lib/elliptic/eddsa/.*": empty,
"elliptic/lib/elliptic/hmac-drbg.js": empty,
// "elliptic/package.json" :
"elliptic/package.json" : ellipticPackage,
"base64-js/.*": undef,
"browser-resolve:.*": undef,
"buffer/.*": undef,
"buffer-shims/.*": undef,
"cipher-base/.*": undef,
"core-util-is/.*": undef,
"create-hash/.*": undef,
"create-hmac/.*": undef,
"events/.*": undef,
"hmac-drbg/lib/hmac-drbg.js": undef,
"ieee754/.*": undef,
"isarray/.*": undef,
"pbkdf2/.*": undef,
"hash.js/lib/hash/ripemd.js": "module.exports = {ripemd160: null}",
// brorand (Maybe swap out brorand with out getRandomBytes from utils?
"brorand/index.js": brorand,
// "browser-resolve/.*": undef, // If we use the actual brorand, we need this
// Used by sha3
"process/.*": undef,
"readable-stream/.*": undef,
"sha.js/.*": undef,
"stream-browserify/.*": undef,
"string_decoder/.*": undef,
};
var modified = {};
@ -39,7 +34,7 @@ var unmodified = {};
function transformFile(path) {
for (var pattern in transforms) {
if (path.match(new RegExp(pattern + '$'))) {
if (path.match(new RegExp('/' + pattern + '$'))) {
modified[pattern] = true;
return transforms[pattern];
}
@ -55,38 +50,74 @@ function transform(path, options) {
}, function () {
var transformed = transformFile(path);
if (transformed != null) {
//console.log('Transform: ' + path + ' => ' + transformed);
data = transformed;
} else {
unmodified[path] = true;
//console.log('Not transformed:', path);
}
//data = data.replace(/__ETHERS_EXPORT__/g, '__MASKED_ETHERS_EXPORT__');
//console.log(data.length, 'FOOBAR', path);
this.queue(data);
this.queue(null);
});
}
function checkBundle(error, source, next) {
var passed = Object.keys(unmodified);
passed.sort();
console.log('Unmodified:');
passed.forEach(function(path) {
console.log(' ' + path);
});
/*
var skipped = Object.keys(transforms);
Object.keys(modified).forEach(function(key) {
delete skipped[key];
});
skipped.sort();
if (skipped.length) {
console.log('Unused Patterns:');
skipped.forEach(function(pattern) {
console.log(' ' + pattern);
});
var inflight = 0;
function preBundle(bundle) {
inflight++;
}
function postBundle(error, source, next) {
if (error) {
console.log(error);
} else {
// We setup the utils instance to be able to create a stand-alone package
source = source.toString();
var lengthBefore = source.length;
var source = source.replace(/"__STAND_ALONE_FALSE__"/g, '"__STAND_ALONE_TRUE__"');
if (lengthBefore - source.length !== 1) {
next(new Error('multiple stand-alone variables changed'));
return;
}
}
*/
inflight--
if (inflight === 0) {
// List all files that passed though unchanged
var preserved = {};
Object.keys(unmodified).forEach(function(filename) {
var match = filename.match(/(node_modules.*)$/);
if (!match) {
match = filename.match(/(ethers\.js.*)$/);
}
if (!match) {
match = [null, filename];
}
preserved[match[1]] = true;
});
preserved = Object.keys(preserved);
preserved.sort();
console.log('Preserved:');
preserved.forEach(function(path) {
console.log(' ' + path);
});
// Make sure there were no replacement patterns that went unused
var skipped = [];
for (var key in transforms) {
if (!modified[key]) { skipped.push(key); }
}
skipped.sort();
if (skipped.length) {
console.log('Unused Patterns:');
skipped.forEach(function(pattern) {
console.log(' ' + pattern);
});
}
}
next(error, source);
}
@ -101,16 +132,16 @@ module.exports = function(grunt) {
'dist/ethers-providers.js': './providers/index.js',
'dist/ethers-utils.js': './utils/index.js',
'dist/ethers-wallet.js': './wallet/index.js',
'dist/ethers-tests.js': './tests/browser.js',
},
options: {
transform: [
[ transform, { global: true } ],
],
browserifyOptions: {
standalone: 'ethers',
//standalone: '_ethers',
},
postBundleCB: checkBundle
preBundleCB: preBundle,
postBundleCB: postBundle
},
},
},

@ -1,3 +1,5 @@
'use strict';
var Contract = require('./contract.js');
var Interface = require('./interface.js');
@ -5,3 +7,6 @@ module.exports = {
Constract: Contract,
Interface: Interface,
}
require('ethers-utils/standalone.js')(module.exports);

@ -257,3 +257,6 @@ module.exports = {
validMnemonic: validMnemonic,
};
require('ethers-utils/standalone.js')({
HDNode: module.exports
});

@ -1,6 +1,5 @@
'use strict';
/*
var contracts = require('ethers-contracts');
var HDNode = require('ethers-hdnode');
@ -46,3 +45,4 @@ module.exports = {
_SigningKey: Wallet._SigningKey,
};
require('ethers-utils/standalone.js')(module.exports);

@ -8,6 +8,7 @@
"version": "grunt dist"
},
"dependencies": {
"ethers-utils": "2.0.0"
},
"devDependencies": {
"grunt": "^0.4.5",

@ -7,6 +7,13 @@ var FallbackProvider = require('./fallback-provider.js');
var InfuraProvider = require('./infura-provider.js');
var JsonRpcProvider = require('./json-rpc-provider.js');
function getDefaultProvider(testnet) {
return new FallbackProvider([
new InfuraProvider(testnet),
new EtherscanProvider(testnet),
]);
}
module.exports = {
EtherscanProvider: EtherscanProvider,
FallbackProvider: FallbackProvider,
@ -15,5 +22,12 @@ module.exports = {
isProvder: Provider.isProvider,
_Provider: Provider,
getDefaultProvider:getDefaultProvider,
Provider: Provider,
}
require('ethers-utils/standalone.js')({
providers: module.exports
});

@ -7,6 +7,7 @@
"xmlhttprequest": "./browser-xmlhttprequest.js"
},
"dependencies": {
"ethers-utils": "2.0.0",
"inherits": "2.0.1",
"xmlhttprequest": "1.8.0"
},

@ -2,18 +2,20 @@
var hash = require('hash.js');
var sha2 = require('./sha2.js');
var convert = require('./convert.js');
// @TODO: Make this use create-hmac in node
function createSha256Hmac(key) {
if (!key.buffer) { key = convert.arrayify(key); }
return new hash.hmac(hash.sha256, key);
return new hash.hmac(sha2.createSha256, key);
}
function createSha512Hmac(key) {
if (!key.buffer) { key = convert.arrayify(key); }
return new hash.hmac(hash.sha512, key);
return new hash.hmac(sha2.createSha512, key);
}
module.exports = {

@ -17,15 +17,6 @@ var rlp = require('./rlp.js');
var utf8 = require('./utf8.js');
var units = require('./units.js');
////var xmlhttprequest = require('./xmlhttprequest.js');
/*
function cloneObject(object) {
var clone = {};
for (var key in object) { clone[key] = object[key]; }
return clone;
}
*/
module.exports = {
rlp: rlp,
@ -68,3 +59,8 @@ module.exports = {
randomBytes: randomBytes,
}
require('./standalone.js')({
utils: module.exports
});

@ -13,8 +13,7 @@
"xmlhttprequest": "1.8.0"
},
"browser": {
"./random-bytes.js": "./browser-random-bytes.js",
"xmlhttprequest": "./browser-xmlhttprequest.js"
"./random-bytes.js": "./browser-random-bytes.js"
},
"keywords": [
"ethereum",

23
utils/standalone.js Normal file

@ -0,0 +1,23 @@
var defineProperty = require('./properties.js').defineProperty;
function Ethers() { }
function defineEthersValues(values) {
// This is modified in the Gruntfile.js
if ("__STAND_ALONE_FALSE__" !== ("__STAND_ALONE_" + "TRUE__")) {
return;
}
if (global.ethers == null) {
defineProperty(global, 'ethers', new Ethers());
}
for (var key in values) {
if (global.ethers[key] == null) {
defineProperty(global.ethers, key, values[key]);
}
}
}
module.exports = defineEthersValues;

@ -354,3 +354,8 @@ utils.defineProperty(Wallet, 'summonBrainWallet', function(username, password, p
utils.defineProperty(Wallet, '_SigningKey', SigningKey);
module.exports = Wallet;
require('ethers-utils/standalone.js')({
Wallet: module.exports
});

@ -1,7 +1,6 @@
'use strict';
var aes = require('aes-js');
var pbkdf2 = require('pbkdf2');
var scrypt = require('scrypt-js');
var uuid = require('uuid');