Using the Grunt tool to manage JS files, there are several common functions, such as compression and merging.
Install the node environment first, install grunt
Build your own project in a directory environment My_project
Add two source files
Create a Package.json file under the project root directory
{ "name": "My_project", "version": "0.1.0", "Devdependencies": { "grunt": "~*", " Grunt-contrib-concat ":" ^0.5.0 ", " Grunt-contrib-jshint ":" ^0.10.0 ", " grunt-contrib-uglify ":" ^0.6.0 " }}
Package.json specific parameter description see https://www.npmjs.org/doc/files/package.json.html
Run NPM Install
The root directory will appear with the Node_modules folder and the corresponding grunt plugin.
If no further package.json is stated, using NPM installation is also possible with NPM install grunt-contrib-plug-in name-G (Global installation)--save-dev (write Package.json).
Create Gruntfile.js
Examples such as the following
Module.exports = function (grunt) { var pkg = Grunt.file.readJSON (' Package.json '); Grunt.initconfig ({ pkg:pkg, concat: { domop: { src: [' src/search.js ', ' src/searchbar.js ', ' src/ Showimg.js '], dest: ' Dest/domop.js ' } , uglify: { options: { banner: ' \ n ', mangle: False }, build:{ src: ' dest/domop.js ', dest: ' Dest/dest.js '}} ); Load concat and Uglify plugins, respectively, for merging and compressing grunt.loadnpmtasks (' Grunt-contrib-concat '); Grunt.loadnpmtasks (' grunt-contrib-uglify '); Register Task grunt.registertask (' Default ', [' concat ', ' uglify ']);
The uglify compression and concat merge are used at this time
Where mangle is the substitution variable, false does not replace the variable, and is replaced by default. You can also set an array
Mangle{ except[' jQuery '' Backbone '}
The jquery,backbone is not replaced when it encounters a file.
Banner: To add a comment
Options{ banner}
See Https://github.com/gruntjs/grunt-contrib-uglify for details.
Merge parameter link Https://github.com/gruntjs/grunt-contrib-concat
Grunt Official website address is: http://www.gruntjs.org/docs/sample-gruntfile.html
Grunt Compression js file