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

Manage Those Dependencies! (scandev)

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Manage Those Dependencies! (scandev)

Avatar for Jakob Mattsson

Jakob Mattsson

March 05, 2013
Tweet

More Decks by Jakob Mattsson

Other Decks in Programming

Transcript

  1. "It's a piece of the stack that's been notably missing

    for years and after using it for a while now, I'm not sure how I lived without it." What is this referring to?
  2. "It's a piece of the stack that's been notably missing

    for years and after using it for a while now, I'm not sure how I lived without it." What is this referring to? 2011
  3. Even Microsoft is catching on! 2011 ”NuGet is a free

    and open source package manager for the .NET Framework”
  4. And stupid habits Add semicolons to the beginning of your

    files! Copy-paste libs - why not small pieces of code too No choice but to check-in dependencies Premature minimification
  5. CommonJS CommonJS is a project with the goal of specifying

    an ecosystem for JavaScript outside the browser
  6. exports.square = function(x) { return x * x; }; console.log(”loaded

    foo”); foo.js A module can have side-effects
  7. exports.version = ”0.1.0”; exports.cube = function(x) { return x *

    x * x; }; console.log(”loaded bar”); bar.js Modules can have multiple values
  8. var coin = Math.random() > 0.5; var name = coin

    ? ‘foo’ : ‘bar’; var mod = require(’./’ + name + ’.js’); console.log(mod.version); main.js We can load dynamically
  9. NPM

  10. We can install packages > npm install foo > npm

    install bar var foo = require(‘foo’); console.log(foo.square(4));
  11. package.json And publish them { "name" : "backbone", "description" :

    "Give your JS App some Backbone.", "url" : "http://backbonejs.org", "keywords" : ["util", "server", "client", "browser"], "author" : "Jeremy Ashkenas <[email protected]>", "dependencies" : { "underscore" : ">=1.3.1" }, "main" : "backbone.js", "version" : "0.9.2" } > npm publish
  12. Browserify Make node-style require() work in the browser with a

    server-side build step, as if by magic!
  13. Write files as if it was node.js // use relative

    requires var foo = require('./foo'); var bar = require('../lib/bar'); // or use modules installed by npm var domready = require('domready'); domready(function() { var elem = document.getElementById('result'); elem.textContent = foo(100) + bar('baz'); }); main.js
  14. Use it in the browser > browserify main.js -o bundle.js

    <script src="bundle.js"></script> Step 1 Step 2
  15. Use it with require var main = require(”main”); (function() {

    var main = require(”main”); // .. use it here .. }()) Or better yet
  16. How it works // magic above... require.define("/main.js", function( require, module,

    exports, __dirname, __filename, process) { // code from main.js }); bundle.js
  17. For front-end developers who crave maintainable assets, Jam is a

    package manager for JavaScript. Unlike other repositories, we put the browser first.
  18. So what does util.js contain then? define(['dep1', 'dep2'], function(dep1, dep2)

    { return { foo: function() { // ... }, bar: 42 }; }); util.js
  19. Simplified CommonJS wrapping define(function(require) { var dep1 = require(”dep1”); var

    dep2 = require(”dep2”); return { foo: function() { // ... }, bar: 42 }; }); util.js
  20. Computed deps will cause issues define(function(require) { var dice =

    Math.random() > 0.5; var name = dice ? ‘foo’ : ‘bar’; var mod = require(’./’ + name + ’.js’); // doh!! console.log(mod.version); return {}; }); util.js
  21. Summary - Asyncronous - Not coupled with any packaging system

    - Superset of CommonJS modules (almost) - Slightly more complex - Some gotchas
  22. Bower is an index bower install jquery bower install git://github.com/components/jquery.git

    bower install http://foo.com/jquery.awesome-plugin.js bower install ./repos/jquery
  23. Write a bower declaration { "name": "my-project", "version": "1.0.0", "dependencies":

    { "mocha": "1.8.1", "angular-ui": "0.3.1", "select2": "3.2.0", "bootstrap": "2.1.1", "jquery-ui": "1.9.2", "jquery": "1.8.3" } }
  24. ?