Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JavaScript dependency management in a post-Node...

Avatar for Romain Romain
September 25, 2013

JavaScript dependency management in a post-Node world

JavaScript dependency management has changed a lot in the last 4 years. Let's have a look at a few practices, and how the Node.js ecosystem is making our lives easier!

Avatar for Romain

Romain

September 25, 2013

More Decks by Romain

Other Decks in Programming

Transcript

  1. dependency management window.Premium = { calculate: function() { return [

    {name: 'Income', price: 40 }, {name: 'Life', price: 60 } ]; } }; premium.js
  2. dependency management window.UI = { refresh: function() { var all

    = window.Premium.calculate(); return _.sortBy(all, 'price'); } }; ui.js
  3. dependency management window.UI = function(_, premium) { function refresh() {

    var all = premium.calculate(); return _.sortBy(all, 'price'); } ...
  4. define('ui', ['underscore', 'premium'], function(_, premium) { return { refresh: function()

    { var all = premium.calculate(); return _.sortBy(all, 'price'); } } });
  5. require.js Config can get complex Async mode VS build mode

    Pay syntax price PROS CONS Ensures dependent modules are loaded Configurability Documentation
  6. exports.calculate = function() { return [ { name: 'Income', price:

    40 }, { name: 'Life', price: 60 } ]; }; premium.js syntax
  7. var _ = require('underscore'); var premium = require('./premium'); exports.refresh =

    function() { var all = premium.calculate(); return _.sortBy(all, 'price'); }; ui.js syntax
  8. var _ = require('underscore'); /* ???? */ var premium =

    require('./premium'); /* relative file path */
  9. npm registry $ npm install underscore --save $ npm install

    [email protected] --save var _ = require('underscore'); […] var sorted = _.sortBy(all, 'price');
  10. var _ = require('underscore'); var premium = require('./premium'); exports.refresh =

    function() { var all = premium.calculate(); return _.sortBy(all, 'price'); }; ui.js
  11. var p = require('./premium'); it('prints the prices', function() { sinon.stub(p,

    'calculate').returns(...); ui.refresh(); }); unit testing ui-spec.js
  12. a private registry $ npm install [email protected] var premium =

    require('premium-calculator'); $ cd premium-calculator $ npm publish
  13. The ecosystem is changing fast! A lot of evolving practices

    Make your own opinions :) keep your eyes open