From 6fa863f2cca4427dbddd8f8f77e18ad63a65d974 Mon Sep 17 00:00:00 2001 From: Nicolas Gotchac Date: Wed, 16 Nov 2016 16:50:12 +0100 Subject: [PATCH] Make parity.js usable by Node and Browser #3471 --- js/package.json | 1 + js/src/parity.js | 34 +++++++++++++++++++++++++--------- js/webpack.libraries.js | 5 ++++- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/js/package.json b/js/package.json index 200de6ee6..77d6e0351 100644 --- a/js/package.json +++ b/js/package.json @@ -138,6 +138,7 @@ "mobx-react": "^3.5.8", "mobx-react-devtools": "^4.2.9", "moment": "^2.14.1", + "node-fetch": "^1.6.3", "qs": "^6.3.0", "react": "^15.2.1", "react-ace": "^4.0.0", diff --git a/js/src/parity.js b/js/src/parity.js index 4e6184c6a..57afdb734 100644 --- a/js/src/parity.js +++ b/js/src/parity.js @@ -14,19 +14,35 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -import 'babel-polyfill'; -import 'whatwg-fetch'; - +import 'babel-polyfill/dist/polyfill.js'; import es6Promise from 'es6-promise'; es6Promise.polyfill(); -import Api from './api'; +try { + if (typeof self.window !== 'undefined') { + self.window.fetch = require('isomorphic-fetch'); + } +} catch (e) {} +try { + if (typeof global !== 'undefined') { + global.fetch = require('node-fetch'); + } +} catch (e) {} + +import Api from './api'; import './dev.parity.html'; -const api = new Api(new Api.Transport.Http('/rpc/')); +// commonjs +module.exports = { Api }; +// es6 default export compatibility +module.exports.default = module.exports; -window.parity = { - Api, - api -}; +if (typeof self !== 'undefined' && typeof self.window !== 'undefined') { + const api = new Api(new Api.Transport.Http('/rpc/')); + + self.window.parity = { + Api, + api + }; +} diff --git a/js/webpack.libraries.js b/js/webpack.libraries.js index bf54a933f..8e166fc54 100644 --- a/js/webpack.libraries.js +++ b/js/webpack.libraries.js @@ -26,6 +26,7 @@ const DEST = process.env.BUILD_DEST || '.build'; module.exports = { context: path.join(__dirname, './src'), + target: 'node', entry: { // library 'inject': ['./web3.js'], @@ -34,7 +35,9 @@ module.exports = { }, output: { path: path.join(__dirname, DEST), - filename: '[name].js' + filename: '[name].js', + library: '[name].js', + libraryTarget: 'umd' }, module: { loaders: [