Do you think jQuery class libraries are not flexible enough to dynamically load modules like other frameworks or class libraries, such as dojo, alternatively, you do not use all functions provided by jQuery in your project. For example, you do not need AJAX functions, but only DOM-related operation functions. So can we reduce the weight of jQuery? In this article, we will introduce how to use some compilation tools to create our own jQuery class library.
Tools required for compilation
Before compiling, we need to prepare three compilation tools:
Git 1.7 or update: used to clone and capture the jQuery code base
Npm: This is introduced in our previous node. js entry. If you have installed node. js, this tool is included.
Grunt: a task-based command line javascript compilation tool
Install and compile
First, download git and npm. We use the windows environment to download and install git and node. js. As follows:
Download and install git:
Download and install node. js:
Note that npm is used to install grunt.
Get and compile jQuery code
Open Git Bash first. If you have installed git, you can find the shortcut in the Start menu and run the following command:
Git clone git: // github.com/jquery/jquery.git
You will see that the file of jquery is being crawled. For example:
After completion, we will find the directory jquery in the personal directory of the local user. This is the jquery project file directory.
Next, go to the jquery directory and run npm:
Cd jquery & npm install
Make sure you have installed grunt and enter the following command:
Grunt-version
Finally, compile with grunt as follows:
Grunt
You will be able to see your compiled jQuery under the dist directory.
Note: If you encounter the following error:
The reason is that the sub-module is not extracted. Use the following method to solve the problem:
Git submodule update -- init
Run grunt again and you can see the following output:
We can see that three versions of jquery. js are generated under the dist directory.
Compile a custom version of jQuery
Next we will compile the custom version of jQuery. Before making the product-level jQuery class library, please confirm the latest stable version, as shown below:
Git pull; git checkout $ (git describe -- abbrev = 0 -- tags)
Then confirm that all node dependency packages and all Git sub-modules have been checked out.
Npm install & grunt
Now let's compile the custom version. For example, if AJAX is not required, run the following command line:
Grunt custom:-ajax
Css is not required:
Grunt custom:-css
Deprecated is not required:
Grunt custom:-deprecated
Dimensions is not required:
Grunt custom:-dimensions
Effects not required:
Grunt custom:-effects
Offset not required:
Grunt custom:-offset
All optional modules are not required:
Grunt custom:-offsetgrunt custom:-ajax,-css,-deprecated,-dimensions,-effects,-offset
Note that all dependencies are compiled for internal processing. You don't have to worry.
Finally, run the Unit test case to ensure the code is correct.
Cd jquery & grunt watch
In this way, you can go to dist to check the number of lines in the jQuery class library, and you can see that the file is smaller.
For more compilation information, see jQuery's github address: https://github.com/jquery/jquery
I hope you will like this article. If you have any questions, please leave a message in our article. Thank you for reading it!
Source: how to compile the latest jQuery class library of the custom version by yourself?
Date: 2012-9-21 Source: GBin1.com