Use gulp to compress html, js, css, and images of the project,

Source: Internet
Author: User
Tags install node

Use gulp to compress html, js, css, and images of the project,
1, download and install node access http://nodejs.org, and then click the big greeninstallClick to directly run the program after the download is complete, and everything is ready. Npm will be installed along with the installation package. 2. Open the code line.Node-v // view the node version. if the version is displayed, the installation is successful.npm -v   //Check the npm version. if the version number is displayed, the installation is successful.Locate your project file, for example:Cd/Users/ydz/WebstormProjects/bucketMasterSudo npm install-g gulp // download and install gulpgulp -v //Check the gulp version. if the version number is displayed, the installation is successful.Npm init // create a local repository and generate the package. json file for configurationNpm install save-dev gulp // install gulp locally to the Projectnpm install Gulp-htmlmin // install the compressed html plug-innpm install Gulp-uglify// Install the compressed js plug-innpm install Gulp-minify-css // install the compressed css plug-innpm install Gulp-imagemin // install the compressed image pluginnpm install Del// Install the compression purge plug-in3. Create a New gulpfile. js file.Write in:Var gulp = require ('gulp '),Htmlmin = require ('Gulp-htmlmin '),Minify = require ('Gulp-minify-css '),Uglify = require ('Gulp-uglify '),Imagemin = require ('Gulp-imagemin '),Del = require ('del ');Gulp. task ('testhtmlmin', function (){Var options = {RemoveComments: true, // clear HTML comments

CollapseWhitespace: true, // compress HTML
CollapseBooleanAttributes: true, // The Boolean attribute value is omitted <input checked = "true"/>=> <input/>
RemoveEmptyAttributes: true, // delete all spaces as attribute values <input id = ""/>=> <input/>
RemoveScriptTypeAttributes: true, // Delete <script> type = "text/javascript"
RemoveStyleLinkTypeAttributes: true // Delete <style> and <link> type = "text/css"
};
Return gulp. src (['bucketsrc/*. html ', 'bucketsrc/*. html'])
. Pipe (htmlmin (options ))
. Pipe (gulp. dest ('bucketdist '));
});
Gulp. task ('ifycss ', function (){
Return gulp. src (['bucketsrc/s/c/*. css ', 'bucketsrc/s/c/*. css']) // compressed file
. Pipe (minify ())
. Pipe (gulp. dest ('bucketdist/s/C'); // Output Folder
});
Gulp. task ('ifyjs', function (){
Return gulp. src ('bucketsrc/s/j/*. js ')
. Pipe (uglify ())
. Pipe (gulp. dest ('bucketdist/s/J '))
});
Gulp. task ('clean', function (cb ){
Del (['bucketdist /*/*. html ', 'bucketdist /*. html ', 'bucketdist/s/img /*. * ', 'bucketdist/s/c /*/*. css ', 'bucketdist/s/c /*. css ', 'bucketdist/s/j /*/*. js'], cb)
});
Gulp. task ('images', function (){
Return gulp. src ('bucketsrc/s/img /*.*')
. Pipe (imagemin ({
Progressive: false
}))
. Pipe (gulp. dest ('bucketdist/s/img '));
});
Gulp. task ('default', ['clean', 'testhtmlmin', 'minifycss ', 'images', 'minifyjs']);
4. Run gulp in the command line.
Default
 
 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.