Book Image

Mastering jQuery

By : Alex Libby
Book Image

Mastering jQuery

By: Alex Libby

Overview of this book

<p>Mastering jQuery has been written not only to help maximize your skills with core functionality in the library, but also to explore some of the more intriguing ways of using the library to achieve real-world solutions that could feature on any website or online environment.</p> <p>You'll start with a look at some of the more advanced ways to incorporate the library into your pages, followed by working with forms and advanced form validation using regular expressions. Next you'll move on to animating in jQuery, advanced event handling, and using jQuery effects.</p> <p>Finally, you will develop practical examples of using jQuery with external functionality such as node-webkit, before finishing with a session on optimizing your version of the library for maximum efficiency and exploring best practices for using QUnit.</p>
Table of Contents (21 chapters)
Mastering jQuery
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Customizing the downloads of jQuery from Git


If we feel so inclined, we can really push the boat out and build a custom version of jQuery using the JavaScript task runner, Grunt. The process is relatively straightforward but involves a few steps; it will certainly help if you have some prior familiarity with Git!

Note

The demo assumes that you have already installed Node.js—if you haven't, then you will need to do this first before continuing with the exercise.

Okay, let's make a start by performing the following steps:

  1. You first need to install Grunt if it isn't already present on your system—bring up the Node.js command prompt and enter this command:

    npm install -g grunt-cli
    
  2. Next, install Git—for this, browse to http://msysgit.github.io/ in order to download the package.

  3. Double-click on the setup file to launch the wizard, accepting all the defaults is sufficient for our needs.

    Note

    If you want more information on how to install Git, head over and take a look at https://github.com/msysgit/msysgit/wiki/InstallMSysGit for more details.

  4. Once Git is installed, change to the jquery folder from within the command prompt and enter this command to download and install the dependencies needed to build jQuery:

    npm install
    
  5. The final stage of the build process is to build the library into the file we all know and love; from the same command prompt, enter this command:

    grunt
    
  6. Browse to the jquery folder—within this will be a folder called dist, which contains our custom build of jQuery, ready for use, as shown in the following screenshot:

Removing redundant modules

If there are modules within the library that we don't need, we can run a custom build. We can set the Grunt task to remove these when building the library, leaving in those that are needed for our project.

Note

For a complete list of all the modules that we can exclude, see https://github.com/jquery/jquery#modules.

For example, to remove AJAX support from our build, we can run this command in place of step 5, as shown previously:

grunt custom:-ajax

This results in a file saving on the original raw version of 30 KB as shown in the following screenshot:

The JavaScript and map files can now be incorporated into our projects in the usual way.

Note

For a detailed tutorial on the build process, this article by Dan Wellman is worth a read (https://www.packtpub.com/books/content/building-custom-version-jquery).

Using a GUI as an alternative

There is an online GUI available, which performs much the same tasks, without the need to install Git or Grunt. It's available at http://projects.jga.me/jquery-builder/, although it is worth noting that it hasn't been updated for a while!

Okay, so we have jQuery installed; let's take a look at one more useful function that will help in the event of debugging errors in our code. Support for source maps has been made available within jQuery since version 1.9. Let's take a look at how they work and see a simple example in action.