From bf5ea728635f31014ed2b29fa62b83c9f5dd2a61 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 26 Jun 2017 17:41:11 -0700 Subject: [PATCH] webpack: fix babelification. upgrade to webpack3. --- .babelrc | 2 +- Makefile | 5 ++- browser/index.html | 12 ++++---- browser/server.js | 8 ++--- lib/env-browser.js | 14 +++------ lib/env.js | 17 +++++++++-- lib/utils/index.js | 1 - lib/utils/lazy-browser.js | 11 ------- lib/utils/lazy.js | 25 --------------- lib/utils/nfkd.js | 10 +----- lib/workers/worker-browser.js | 2 +- package.json | 57 +++++++++++++++-------------------- webpack.config.js | 32 +++++++++++--------- 13 files changed, 78 insertions(+), 118 deletions(-) delete mode 100644 lib/utils/lazy-browser.js delete mode 100644 lib/utils/lazy.js diff --git a/.babelrc b/.babelrc index 1142f253..17d01063 100644 --- a/.babelrc +++ b/.babelrc @@ -1,7 +1,7 @@ { "presets": ["es2015"], "plugins": [ - "transform-async-to-generator", + ["transform-async-to-generator"], ["transform-runtime", { "polyfill": true, "regenerator": true diff --git a/Makefile b/Makefile index 79ec5c58..71225bcf 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,10 @@ clean: docs: @npm run docs +lint: + @npm run lint + test: @npm test -.PHONY: all clean docs test +.PHONY: all clean docs lint test diff --git a/browser/index.html b/browser/index.html index d05b7613..3e147a00 100644 --- a/browser/index.html +++ b/browser/index.html @@ -3,12 +3,12 @@ bcoin - +

Bcoin, the browser full node

diff --git a/browser/server.js b/browser/server.js index a75d16d4..c98ed884 100644 --- a/browser/server.js +++ b/browser/server.js @@ -7,8 +7,8 @@ var server, proxy; var index = fs.readFileSync(__dirname + '/index.html'); var indexjs = fs.readFileSync(__dirname + '/index.js'); -var bcoin = fs.readFileSync(__dirname + '/bcoin.min.js'); -var master = fs.readFileSync(__dirname + '/bcoin-master.min.js'); +var bcoin = fs.readFileSync(__dirname + '/bcoin.js'); +var master = fs.readFileSync(__dirname + '/bcoin-master.js'); var worker = fs.readFileSync(__dirname + '/bcoin-worker.js'); proxy = new WSProxy({ @@ -37,11 +37,11 @@ server.get('/index.js', function(req, res) { res.send(200, indexjs, 'js'); }); -server.get('/bcoin.min.js', function(req, res) { +server.get('/bcoin.js', function(req, res) { res.send(200, bcoin, 'js'); }); -server.get('/bcoin-master.min.js', function(req, res) { +server.get('/bcoin-master.js', function(req, res) { res.send(200, master, 'js'); }); diff --git a/lib/env-browser.js b/lib/env-browser.js index 8cef81cc..0c740bfe 100644 --- a/lib/env-browser.js +++ b/lib/env-browser.js @@ -15,6 +15,7 @@ * @exports Environment * @constructor * @property {Function} env - See {@link Environment}. + * @property {Function} require - See {@link module:utils/lazy}. * * @property {Function} bn - See {@url https://github.com/indutny/bn.js}. * @property {Object} elliptic - See {@url https://github.com/indutny/elliptic}. @@ -126,10 +127,6 @@ function Environment() { this.env = Environment; - // BN - this.bn = require('bn.js'); - this.elliptic = require('elliptic'); - // Horrible BIP this.bip70 = require('./bip70'); @@ -150,6 +147,7 @@ function Environment() { // Crypto this.crypto = require('./crypto'); + this.bn = require('./crypto/bn'); this.ec = require('./crypto/ec'); this.pk = require('./crypto/pk'); this.schnorr = require('./crypto/schnorr'); @@ -235,6 +233,7 @@ function Environment() { this.bloom = require('./utils/bloom'); this.co = require('./utils/co'); this.encoding = require('./utils/encoding'); + this.int64 = require('./utils/int64'); this.lock = require('./utils/lock'); this.reader = require('./utils/reader'); this.staticwriter = require('./utils/staticwriter'); @@ -291,12 +290,7 @@ Environment.prototype.now = function now() { */ Environment.prototype.cache = function cache() { - this.bip70; - this.common; - this.crypto; - this.fullnode; - this.http; - this.spvnode; + ; }; /* diff --git a/lib/env.js b/lib/env.js index daf82494..60996c66 100644 --- a/lib/env.js +++ b/lib/env.js @@ -7,8 +7,6 @@ 'use strict'; -var lazy = require('./utils/lazy'); - /** * A bcoin "environment" which exposes all * constructors for primitives, the blockchain, @@ -128,7 +126,6 @@ var lazy = require('./utils/lazy'); function Environment() { this.env = Environment; - this.require = lazy(require, this); // Horrible BIP this.require('bip70', './bip70'); @@ -301,12 +298,26 @@ Environment.prototype.cache = function cache() { this.spvnode; }; +/** + * Cache all necessary modules. + */ + +Evironment.prototype.require = function _require(name, path) { + var cache; + this.__defineGetter__(name, function() { + if (!cache) + cache = require(path); + return cache; + }); +}; + /* * Expose by converting `exports` to an * Environment. */ exports.cache = Environment.prototype.cache; +exports.require = Environment.prototype.require; exports.set = Environment.prototype.set; exports.now = Environment.prototype.now; diff --git a/lib/utils/index.js b/lib/utils/index.js index ba3bac2a..a9dc8bc8 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -17,7 +17,6 @@ exports.fs = require('./fs'); exports.Heap = require('./heap'); exports.Int64 = require('./int64'); exports.IP = require('./ip'); -exports.lazy = require('./lazy'); exports.Lock = require('./lock'); exports.MappedLock = exports.Lock.Mapped; exports.LRU = require('./lru'); diff --git a/lib/utils/lazy-browser.js b/lib/utils/lazy-browser.js deleted file mode 100644 index 92b907a2..00000000 --- a/lib/utils/lazy-browser.js +++ /dev/null @@ -1,11 +0,0 @@ -/*! - * lazy-browser.js - lazy loading for bcoin - * Copyright (c) 2016-2017, Christopher Jeffrey (MIT License). - * https://github.com/bcoin-org/bcoin - */ - -'use strict'; - -module.exports = function lazy(require, exports) { - return function() {}; -}; diff --git a/lib/utils/lazy.js b/lib/utils/lazy.js deleted file mode 100644 index 7627315a..00000000 --- a/lib/utils/lazy.js +++ /dev/null @@ -1,25 +0,0 @@ -/*! - * lazy.js - lazy loading for bcoin - * Copyright (c) 2016-2017, Christopher Jeffrey (MIT License). - * https://github.com/bcoin-org/bcoin - */ - -'use strict'; - -/** - * Setup a lazy loader. - * @alias module:utils.lazy - * @param {Function} require - * @param {Object} exports - */ - -module.exports = function lazy(require, exports) { - return function _require(name, path) { - var cache; - exports.__defineGetter__(name, function() { - if (!cache) - cache = require(path); - return cache; - }); - }; -}; diff --git a/lib/utils/nfkd.js b/lib/utils/nfkd.js index 8ab57ba0..35335502 100644 --- a/lib/utils/nfkd.js +++ b/lib/utils/nfkd.js @@ -6,8 +6,6 @@ 'use strict'; -var unorm; - /** * Normalize unicode string. * @alias module:utils.nfkd @@ -16,13 +14,7 @@ var unorm; */ function nfkd(str) { - if (str.normalize) - return str.normalize('NFKD'); - - if (!unorm) - unorm = require('../../vendor/unorm'); - - return unorm.nfkd(str); + return str.normalize('NFKD'); } /* diff --git a/lib/workers/worker-browser.js b/lib/workers/worker-browser.js index dfc17a6e..a1fa25d9 100644 --- a/lib/workers/worker-browser.js +++ b/lib/workers/worker-browser.js @@ -9,7 +9,7 @@ /* jshint worker: true */ -self.importScripts('/bcoin-master.min.js'); +self.importScripts('/bcoin-master.js'); self.onmessage = function onmessage(event) { var env; diff --git a/package.json b/package.json index 20da46a1..b9305a15 100644 --- a/package.json +++ b/package.json @@ -30,25 +30,24 @@ "optionalDependencies": { "bcoin-native": "0.0.19", "leveldown": "1.7.0-0", - "level-js": "2.2.4", "secp256k1": "3.2.5", "socket.io": "2.0.1", "socket.io-client": "2.0.1" }, "devDependencies": { - "babelify": "^7.3.0", - "babel-loader": "^6.4.1", - "babel-preset-es2015": "^6.24.1", - "babel-polyfill": "^6.23.0", + "babel-core": "^6.25.0", + "babel-loader": "^7.1.0", + "babel-plugin-transform-async-to-generator": "^6.24.1", "babel-plugin-transform-runtime": "^6.23.0", "babel-plugin-transform-regenerator": "^6.24.1", - "babel-plugin-transform-async-to-generator": "^6.24.1", + "babel-polyfill": "^6.23.0", + "babel-preset-es2015": "^6.24.1", "eslint": "^4.1.0", "hash.js": "^1.0.3", "jsdoc": "^3.4.3", + "level-js": "2.2.4", "mocha": "^3.4.1", - "uglify-js": "^3.0.5", - "webpack": "^1.13.3" + "webpack": "^3.0.0" }, "main": "./lib/bcoin.js", "bin": { @@ -58,15 +57,26 @@ "bcoin": "./bin/bcoin" }, "scripts": { - "test": "mocha --reporter spec test/*-test.js", - "test-file": "mocha --reporter spec ", - "test-browser": "BCOIN_NO_NATIVE=1 BCOIN_USE_ELLIPTIC=1 mocha --reporter spec test/*-test.js", - "clean": "rm browser/bcoin.min.js browser/bcoin-master.min.js", - "lint": "eslint lib/ test/ migrate/ examples/ bench/ bin/cli bin/node bin/spvnode || exit 0", + "clean": "rm browser/bcoin.js browser/bcoin-master.js browser/bcoin-worker.js", "docs": "jsdoc -c jsdoc.json", - "webpack": "./node_modules/webpack/bin/webpack.js" + "lint": "eslint lib/ test/ migrate/ examples/ bench/ bin/cli bin/node bin/spvnode || exit 0", + "lint-file": "eslint", + "test": "mocha --reporter spec test/*-test.js", + "test-browser": "BCOIN_NO_NATIVE=1 BCOIN_USE_ELLIPTIC=1 mocha --reporter spec test/*-test.js", + "test-file": "mocha --reporter spec", + "webpack": "webpack" }, "browser": { + "bcoin-native": "./browser/empty.js", + "child_process": "./browser/empty.js", + "crypto": "./browser/empty.js", + "dgram": "./browser/empty.js", + "dns": "./browser/empty.js", + "fs": "./browser/empty.js", + "net": "./browser/empty.js", + "os": "./browser/empty.js", + "secp256k1": "./browser/empty.js", + "socket.io": "./browser/empty.js", "./lib/blockchain/layout.js": "./lib/blockchain/layout-browser.js", "./lib/crypto/backend.js": "./lib/crypto/backend-browser.js", "./lib/crypto/ec.js": "./lib/crypto/ec-elliptic.js", @@ -89,25 +99,8 @@ "./lib/utils/native": "./browser/empty.js", "./lib/utils/nfkd": "./lib/utils/nfkd-browser.js", "./lib/utils/nexttick": "./lib/utils/nexttick-browser.js", - "./lib/utils/lazy": "./lib/utils/lazy-browser.js", "./lib/wallet/http": "./browser/empty.js", "./lib/wallet/layout": "./lib/wallet/layout-browser.js", - "./lib/wallet/server": "./browser/empty.js", - "bcoin-native": "./browser/empty.js", - "child_process": "./browser/empty.js", - "crypto": "./browser/empty.js", - "dgram": "./browser/empty.js", - "dns": "./browser/empty.js", - "ec": "./lib/crypto/ec-elliptic.js", - "fs": "./browser/empty.js", - "net": "./browser/empty.js", - "os": "./browser/empty.js", - "secp256k1": "./browser/empty.js", - "socket.io": "./browser/empty.js" - }, - "browserify": { - "transform": [ - "babelify" - ] + "./lib/wallet/server": "./browser/empty.js" } } diff --git a/webpack.config.js b/webpack.config.js index 6de4195c..77c89ddf 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,26 +1,30 @@ -const webpack = require('webpack') +'use strict'; + +var webpack = require('webpack') +var path = require('path'); module.exports = { + target: 'web', entry: { - 'bcoin.min': './lib/bcoin', - 'bcoin-master.min': './lib/workers/master' + 'bcoin': './lib/bcoin', + 'bcoin-master': './lib/workers/master' }, output: { - path: './browser', + path: path.resolve(__dirname, 'browser'), filename: '[name].js' }, resolve: { - extensions: ['', '.js', '.json'], - packageAlias: 'browser' + descriptionFiles: ['package.json'], + modules: ['node_modules'], + extensions: ['.js', '.json'], + aliasFields: ['browser'] }, module: { - loaders: [ - { test: /\.js$/, loader: 'babel', exclude: /node_modules/ }, - { test: /\.json$/, loader: 'json' } - ] - }, - node: { - fs: 'empty' + rules: [{ + test: /\.js$/, + exclude: path.resolve(__dirname, 'node_modules'), + loader: 'babel-loader' + }] }, plugins: [ new webpack.optimize.UglifyJsPlugin({ @@ -29,4 +33,4 @@ module.exports = { } }) ] -} +};