<script src='...'/>
// multiply.js
module.exports = function (x, y) {
return x * y;
};
// square.js
var multiply = require('./multiply');
module.exports = function (x) {
return multiply(x, x);
};
// index.js
var square = require('./square');
console.log( square(5) ); // 25
$ npm install uniq // creates node_modules/uniq/...
// index.js
var uniq = require('uniq');
var data = [1, 2, 2, 2, 2, 3, 3, 3, 3, 3];
console.log( unique(data) ); // [ 1, 2, 3 ]
$ browserify index.js > bundle.js
// index.html
<script src="bundle.js"></script> // [ 1, 2, 3 ]
$ npm install -g beefy
$ beefy index.js:bundle.js --live
listening on http://localhost:9966/
deamdify
deglobalify
es6ify
var dep = require('dependency');
// then use dep...
// index.js
var $ = require('jquery');
$('.buy-product').click(function () {
require(['/shopping-cart'], function (shoppingCart) {
// only downloaded shopping cart code
// including jqueryui when necessary
shoppingCart.open();
});
});
// shopping-cart.js
var $ = require('jquery');
require('jquery-ui'); // imagine jquery-ui being heavy
module.exports = {
open: function () {
// open modal with jquery ui
}
};