Skip to content

Instantly share code, notes, and snippets.

@nicolashery
Created March 21, 2014 11:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolashery/9684078 to your computer and use it in GitHub Desktop.
Save nicolashery/9684078 to your computer and use it in GitHub Desktop.
Building UMD modules with dependencies with Browserify
var moment = require('moment');
module.exports = {
now: function() { return moment().format(); },
hello: function() { return 'Hello world'; }
};
var _ = require('lodash');
var foo = require('./foo');
module.exports = function () {
_.forEach(foo, function(fn, key) {
console.log(key, fn());
});
};
dist:
browserify \
--external lodash \
--external moment \
--require ./index.js:robot \
> bundle.js
cat umd-head.js bundle.js umd-tail.js > robot.js
(function(root, factory) {
if(typeof exports === 'object') {
module.exports = factory(require('lodash'), require('moment'));
}
else if(typeof define === 'function' && define.amd) {
define(['lodash', 'moment'], factory);
}
else {
root.robot = factory(root._, root.moment);
}
}(this, function(_, moment) {
var require = function(name) {
return {'G2vsx1': _, 'ZCrjYp': moment}[name];
};
require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var moment = require('moment');
module.exports = {
now: function() { return moment().format(); },
hello: function() { return 'Hello world'; }
};
},{"moment":"ZCrjYp"}],"Focm2+":[function(require,module,exports){
var _ = require('lodash');
var foo = require('./foo');
module.exports = function () {
_.forEach(foo, function(fn, key) {
console.log(key, fn());
});
};
},{"./foo":1,"lodash":"G2vsx1"}],"robot":[function(require,module,exports){
module.exports=require('Focm2+');
},{}]},{},[])
return require('robot');
}))
(function(root, factory) {
if(typeof exports === 'object') {
module.exports = factory(require('lodash'), require('moment'));
}
else if(typeof define === 'function' && define.amd) {
define(['lodash', 'moment'], factory);
}
else {
root.robot = factory(root._, root.moment);
}
}(this, function(_, moment) {
var require = function(name) {
return {'G2vsx1': _, 'ZCrjYp': moment}[name];
};
return require('robot');
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment