diff --git a/browser/build.js b/browser/build.js index ca91ca6..14f14ed 100644 --- a/browser/build.js +++ b/browser/build.js @@ -1,44 +1,16 @@ 'use strict'; -/* - * - * The key parameter 'pack' - * The supplied 'custom_prelude.js' file is needed for - * .load function of soop. - */ - var fs = require('fs'); var browserify = require('browserify'); var browserPack = require('browser-pack'); -var program = require('commander'); - -// concat browser vendor files var exec = require('child_process').exec; var sys = require('sys'); var puts = function(error, stdout, stderr) { if (error) console.log(error); - sys.puts(stdout); - sys.puts(stderr); -}; - -exec('cd browser; sh concat.sh', puts); - -var list = function(val) { - return val.split(','); + //sys.puts(stdout); + //sys.puts(stderr); }; -program - .version('0.0.1') - .option('-a, --includeall', 'Include all submodules.') - .option('-d, --dontminify', 'Don\'t minify the code.') - .option('-s, --submodules ', 'Include the listed comma-separated submodules.', list) - .parse(process.argv); - -if (!program.includeall && (!program.submodules || program.submodules.length === 0)) { - console.log('Must use either -s or -a option. For more info use the --help option'); - process.exit(1); -} - var pack = function (params) { var file = require.resolve('soop'); var dir = file.substr(0, file.length - String('soop.js').length); @@ -82,40 +54,103 @@ var modules = [ 'util/VersionedData', ]; -var opts = {}; -opts.pack = pack; -opts.debug = true; -opts.standalone = 'bitcore'; -opts.insertGlobals = true; - -var b = browserify(opts); -b.require('browserify-bignum/bignumber.js', {expose: 'bignum'} ); -b.require('browserify-buffertools/buffertools.js', {expose:'buffertools'}); -b.require('base58-native', {expose: 'base58-native'}); -b.require('./bitcore', {expose: 'bitcore'}); -modules.forEach(function(m) { - if (program.includeall || program.submodules.indexOf(m) > -1) { - console.log('Including '+m+' in the browser bundle'); - b.require('./' + m + '.js' , {expose: './'+m} ); +var createBitcore = function(opts) { + + + opts.dir = opts.dir || ''; + + // concat browser vendor files + exec('cd ' + opts.dir + 'browser; sh concat.sh', puts); + + if (!opts.includeall && (!opts.submodules || opts.submodules.length === 0)) { + if (!opts.stdout) console.log('Must use either -s or -a option. For more info use the --help option'); + process.exit(1); } -}); -if (!program.dontminify) { - b.transform({ - global: true - }, 'uglifyify'); -} + if (opts.submodules) { + for (var i = 0; i < opts.submodules.length; i++) { + var sm = opts.submodules[i]; + if (modules.indexOf(sm) === -1) throw new Error('Unknown submodule ' + sm); + } + } -var bundle = b.bundle(); -bundle = bundle.pipe(fs.createWriteStream('browser/bundle.js')); + var bopts = { + pack: pack, + debug: true, + standalone: 'bitcore', + insertGlobals: true + }; + var b = browserify(bopts); + + b.require(opts.dir + 'browserify-bignum/bignumber.js', { + expose: 'bignum' + }); + b.require(opts.dir + 'browserify-buffertools/buffertools.js', { + expose: 'buffertools' + }); + b.require(opts.dir + 'base58-native', { + expose: 'base58-native' + }); + b.require('./' + opts.dir + 'bitcore', { + expose: 'bitcore' + }); + modules.forEach(function(m) { + if (opts.includeall || opts.submodules.indexOf(m) > -1) { + if (!opts.stdout) console.log('Including ' + m + ' in the browser bundle'); + b.require('./' + opts.dir + m + '.js', { + expose: './' + m + }); + } + }); + + if (!opts.dontminify) { + b.transform({ + global: true + }, 'uglifyify'); + } -opts.standalone = 'testdata'; -var tb = browserify(opts); -tb.require('./test/testdata', {expose: 'testdata'}); -tb.transform('brfs'); + var bundle = b.bundle(); + return bundle; +}; -tb.bundle().pipe(fs.createWriteStream('browser/testdata.js')); +var createTestData = function() { + var bopts = { + pack: pack, + debug: true, + standalone: 'testdata', + insertGlobals: true + }; + var tb = browserify(bopts); + tb.require('./test/testdata', { + expose: 'testdata' + }); + tb.transform('brfs'); + + return tb.bundle(); +}; +if (require.main === module) { + var list = function(val) { + return val.split(','); + }; + var program = require('commander'); + program + .version('0.0.1') + .option('-a, --includeall', 'Include all submodules.') + .option('-d, --dontminify', 'Don\'t minify the code.') + .option('-o, --stdout', 'Specify output as stdout') + .option('-D, --dir ', 'Specify a base directory') + .option('-s, --submodules ', 'Include the listed comma-separated submodules.', list) + .parse(process.argv); + if (!program.stdout) { + var testBundle = createTestData(program); + testBundle.pipe(fs.createWriteStream('browser/testdata.js')); + } + var bitcoreBundle = createBitcore(program); + bitcoreBundle.pipe(program.stdout ? process.stdout : fs.createWriteStream('browser/bundle.js')); +} +module.exports.createBitcore = createBitcore; +module.exports.createTestData = createTestData;