UNPKG

3.17 kBMarkdownView Raw
1# bromote
2
3Tool to setup and require remote scripts with browserify.
4
5### Server Side
6
7```js
8var browserify = require('browserify');
9var bromote = require('bromote');
10var PassThrough = require('stream').PassThrough;
11
12var remote =
13 { jquery:
14 { exports: '$',
15 url: 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js' },
16 backbone:
17 { deps: { jquery: '$', underscore: '_' },
18 exports: 'Backbone',
19 url: 'http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js' },
20 underscore:
21 { exports: '_',
22 url: 'http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js' } };
23
24var passThrough = new PassThrough();
25var bify = browserify();
26
27bromote(bify, remote, function (err) {
28 if (err) return console.error(err);
29
30 bify
31 .add('./main.js', { entry: true })
32 .bundle()
33 .pipe(passThrough);
34});
35return passThrough;
36```
37
38### Client Side
39
40```js
41bromote.backbone(function (backbone) {
42 console.log(backbone.$().jquery); // => '1.7.1'
43});
44
45bromote.jquery(function ($) {
46 console.log($().jquery); // => '1.7.1'
47})
48```
49
50Example derived from [this test](https://github.com/thlorenz/bromote/tree/master/test/remote-backbone-jquery-underscore)
51
52For more examples see [examples](https://github.com/thlorenz/bromote/tree/master/examples) and [tests](https://github.com/thlorenz/bromote/tree/master/test).
53
54## Disclaimer
55
56Since you are already using browserify and thus can pull in modules via npm in a version controlled manner, **you better
57have a damn good reason to load scripts from a url**.
58
59You loose versioning and are opening your app up to lots of unknowns. **Bad things will happen!**
60
61If any of the scripts pulled in via a url has a bug, it could potentially crash your app. Since you are loading it
62dynamically, you have no control over when that could happen, i.e. it the script could change months after you deployed
63your app.
64
65So in general **please don't do this**.
66
67## Why bromote then?
68
69I created bromote to allow people to still use browserify, even if they find themselves in a situation where they are
70forced to load scripts from urls for whatever reason that is out of their control.
71
72## Installation
73
74 npm install bromote
75
76## Features
77
78- load dependencies from any url
79- you may use bromote to load external browserify bundles **on demand**
80- bromote properly resolves and loads nested dependencies in correct order (see example)
81
82## API
83
84### Server side: ***bromote(bify, remote, cb)***
85```
86/**
87 * Generates all remote loaders and adds them to browserify instance if it is given.
88 * Calls back with paths to generated loaders.
89 *
90 * @name exports
91 * @function
92 * @param bify {Object} browserify instance (optional) but recommended
93 * @param remote {Object} hashtable containing information about remote scripts for which to generate and add loaders
94 * @param cb {Function} called back with paths to generated loaders
95 */
96```
97
98### Client side: ***bromote.foo(function (foo) { ... })***
99
100Assuming that `foo` was included as a remote server side, this will resolve it from the remote url and call back with
101its export once it is loaded.
102
103## License
104
105MIT