Nodejs compression css and JS tools

Source: Internet
Author: User

1. Installing Nodejs

2. Install the cssmin. NPM Install Cssmin-g

3. Install the yuicompressor. NPM Install Yuicompressor-g

4. Set Node_path to Global C:\Users\Administrator\AppData\Roaming\npm\node_modules (default Nodejs global directory)

5. Create the source directory and create the Dist directory

6. Create Compress.js

Var fs = require (' FS '); Var cssmin = require (' cssmin '); var compressor =  require (' yuicompressor ');var sourcedir =  "source";var distdir =  "dist"; var  filenames = fs.readdirsync (SourceDir); Var fileindex = 0;compressfile (fileNames[ FileIndex]); Function compressfile (FileName) {if (filename==null) {return;} var sourcefilename = filename;var sourcefilepath = sourcedir+ "/" +sourceFileName; Var suffix = getsuffix (filename), if (suffix== "CSS") {compresscssfile (filename); return;} Else if (suffix== "JS") {compressjsfile (fileName); Return}compressfile (Filenames[++fileindex]);} Function compresscssfile (fileName) {Var sourcefilename = filename;var sourcefilepath  = sourcedir+ "/" +sourcefilename;var compressedfilename = sourcefilename.replace (". css "," ") +". Min.css "; var compressedfilepath = distdir+"/"+Sourcefilename;var css = fs.readfilesync (sourcefilepath, encoding= ' UTF8 '); var min  = cssmin (CSS); Fs.writefile (Compressedfilepath,min,function (Err) {if (err==null) {Console.log ("compress  file["+sourcefilename+"] to file["+compressedfilename+"] successfully  ... "); Compressfile (Filenames[++fileindex]);} Else{console.log ("encounter error : " +err);}); Function compressjsfile (fileName) {Var sourcefilename = filename;var sourcefilepath  = sourcedir+ "/" +sourcefilename;var compressedfilename = sourcefilename.replace (". js") , "") + ". Min.js"; var compressedfilepath = distdir+ "/" +compressedfilename;compressor.compress ( sourcefilepath, {charset:  ' UTF8 ',type:  ' js ', nomunge: true, ' Line-break ': 80},  function (Err, data, extra)  {fs.writefile (compressedfilepath,data,function (Err) {if (err==null) { Console.log ("compress file[" +sourcefilEname+ "] to file[" +compressedfilename+ "] successfully  ..."); Compressfile (fileNames[++ FileIndex]);} Else{console.log ("encounter error : " +err);});} Function getsuffix (fileName) {Var index = filename.lastindexof ("."); Return filename.substring (index+1);}

7. Place the file you want to put in the source directory, execute compress.js (Command node compress), and compress the file into the Dist directory.

Nodejs compression css and JS tools

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.