JS modules and libraries tend to be either too large or too small.
Driven by arbitrary constraints or poor engineering and/or decision making.
I want my application size to be optimal, and I want to be able to tell friends about cool libraries I've found.
In the old days (jQuery) telling people about stuff was easy, but optimal size wasn't.
Now in the post microJS era, optimal size is achievable (not necessarily easy) but telling people about stuff can be hard.
Using technologies like browserify and targeted require statements you can do some cool things.
You can do the same thing with other module systems too.
So, write libraries and modules that can be used selectively. Not specifically micro.
So how exactly do you do this?
If you are using browserify then separate useful functionality at the file level and include at the top-level of your module.
- foo |- package.json |- index.js |- bar.js
var bar = require('foo/bar');
A good example of doing this in the wild is http://documentup.com/montagejs/collections
When should you consider breaking out?
If a sub-component has additional dependencies to the rest of the package.
If you believe a sub-component should be versioned separately.
Also micro libraries with strong conceptual boundaries are ok too. Eve and Keymaster come to mind.
- browserify: https://github.com/substack/node-browserify
- AMD include example: https://raw.github.com/Modernizr/Modernizr/master/feature-detects/canvas/todataurl.js
- ES6 module examples: http://wiki.ecmascript.org/doku.php?id=harmony:modules_examples
- montagejs collections: https://github.com/montagejs/collections