Pruning jQuery With Custom Builds
I'm not saying jQuery is as ineluctable as an MLT, but the recent improvements to jQuery's build system might just entice you to sneak a nibble. Supplementing the releases of jQuery 1.11 and 2.1 is a reorganization of the source into AMD modules. Last week, I faithfully endowed the internet with a brief elucidation of the changes. Disregarding the inherent usefulness of those modules, their introduction came with a side-effect: a more robust, flexible build system.
Before we can create custom builds, there are some prerequisites. I expect you already have git and node.js. And if you don't already have bower and the grunt-cli, you should get on that.
Of course, you'll need the jQuery source repository and its dependencies.
git clone git://github.com/jquery/jquery.git cd jquery && npm install
Building is a matter of a grunt task. It's simple, really:
# A custom jQuery build excluding AJAX grunt custom:-ajax # A custom build excluding all deprecated functionality and jQuery animations grunt custom:-deprecated,-effects
The custom task is most applicable to the exclusion of modules, but, you ask, "What if I only want a couple modules?" Let's trickle down to the lower-level build task, which custom uses internally. The syntax is similar, but contains an ominous asterisk. Just know that it goes there.
# A custom build containing only traversing # and its dependencies (Core and Sizzle) grunt build:*:traversing # A custom build containing only jQuery.Deferred and Core grunt build:*:deferred
If you're wondering where these mysterious keys ('traversing', 'deferred', 'deprecated', etc.) originate, take a look in the src folder. Each file in this folder is its own module. The keys are straightforward; they are the paths to those files relative to the src folder. Any module can be excluded (except for core), even the smaller ones:
# Excludes event aliases and JSONP functionality grunt custom:-ajax/jsonp,-event/alias # Excludes only the `$(document).ready` module grunt custom:-core/ready
Incidentally, those folders entitled "var" are special. They contain petite modules which are converted to bona fide javascript variable declarations when injected into jQuery.
Creating custom builds is especially pertinent to libraries that incorporate jQuery directly. It is impetuous to include all of jQuery when the library only requires a reduced subset of its features. For the purposes of the mainstream, custom builds provide a flexible mechanism to streamline jQuery and meld it with other javascript to produce optimized, targeted, and consumable packages.