restful.js icon indicating copy to clipboard operation
restful.js copied to clipboard

docs: import instructions lead to node libraries being included in browser build

Open chrisv2 opened this issue 9 years ago • 0 comments

I'm using react-scripts 0.7.0 to create an app with restful.js as the client. Using the default import instructions from the docs, e.g.:

import whatwg-fetch;
import restful, { fetchBackend } from 'restful.js';

const api = restful('http://api.example.com', fetchBackend(fetch));

leads to an error from UglifyJs when trying to build a dist bundle:

static/js/main.fec1d0fd.js from UglifyJs
Unexpected character '`' [/usr/lib/nodejs/events.js:260,0]

Though this is primarily caused by UglifyJS not being able to handle ES2015 syntax, there is no reason to include anything from /usr/lib/nodejs into a bundle targeting browsers - even if it was made to work somehow, it would unnecessarily blow up the bundle size. For this scenario, it would be better to import the standalone version directly instead:

import restful from 'restful.js/dist/restful.standalone'
const api = restful('http://api.example.com');

I think this should be mentioned in the docs, as it can be very confusing, especially for inexperienced users.

chrisv2 avatar Oct 27 '16 08:10 chrisv2