bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.62k stars 2.09k forks source link

Browserify creates a file with require statements. #1222

Closed davidapple closed 5 years ago

davidapple commented 5 years ago

When I run browserify bitcoinjs-lib > bitcoinjs-lib.js

I get a huge file that doesn't work in the browser end because it contains require() statements.

require=(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
// base-x encoding
// Forked from https://github.com/cryptocoinjs/bs58
// Originally written by Mike Hearn for BitcoinJ
// Copyright (c) 2011 Google Inc
// Ported to JavaScript by Stefan Thomas
// Merged Buffer refactorings from base58-native by Stephen Pair
// Copyright (c) 2013 BitPay Inc

var Buffer = require('safe-buffer').Buffer

module.exports = function base (ALPHABET) {
  var ALPHABET_MAP = {}
  var BASE = ALPHABET.length
  var LEADER = ALPHABET.charAt(0)

  // pre-compute lookup table
  for (var z = 0; z < ALPHABET.length; z++) {
    var x = ALPHABET.charAt(z)

    if (ALPHABET_MAP[x] !== undefined) throw new TypeError(x + ' is ambiguous')
    ALPHABET_MAP[x] = z
  }

This is the first 40 lines. The complete file is 21718 lines.

dcousens commented 5 years ago

https://stackoverflow.com/questions/32604311/how-to-create-standalone-browserify-bundle-exporting-directly-to-window#33764505

window.Bitcoin = require('bitcoinjs-lib')
junderw commented 5 years ago
$ browserify --help
Usage: browserify [entry files] {OPTIONS}

Standard Options:
...
  --standalone -s  Generate a UMD bundle for the supplied export name.
                   This bundle works with other module systems and sets the name
                   given as a window global if no module system is found.
...

browserify -r bitcoinjs-lib -s bitcoin > bitcoinjs-lib.js will make the global variable bitcoin available to the browser.