Home > Web Front-end > HTML Tutorial > Gulp Study Guide CSS Merger, Compression, MD5 Naming and Path Replacement_html/css_WEB-ITnose

Gulp Study Guide CSS Merger, Compression, MD5 Naming and Path Replacement_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:42:20
Original
1204 people have browsed it

Continued from the previous article: Gulp Learning Guide, here we mainly record the use of gulp to merge, compress CSS, MD5 naming and replace the CSS files introduced in the file. Of course, this series of operations are based on gulp For plug-ins, the following plug-ins are mainly used:

var concat = require('gulp-concat');//- 多个文件合并为一个;var minifyCss = require('gulp-minify-css');//- 压缩CSS为一行; var rev = require('gulp-rev');//- 对文件名加MD5后缀var revCollector = require('gulp-rev-collector');//- 路径替换
Copy after login

Install the Gulp plug-in into the local project

npm init//- 生成一个 package.json,里面是一些常规的配置信息npm install gulp gulp-concat gulp-minify-css gulp-rev gulp-rev-collector --save-dev//- 安装插件到项目目录内
Copy after login

After completing the above two steps, it will be generated in our project A package.json file and a node_modules directory

Create the configuration file gulpfile.js

Create a gulpfile.js file in the project root directory (must be this file name~), the content is Configuration information of the above plug-ins:

var gulp = require('gulp');var concat = require('gulp-concat');                            //- 多个文件合并为一个;var minifyCss = require('gulp-minify-css');                     //- 压缩CSS为一行;var rev = require('gulp-rev');                                  //- 对文件名加MD5后缀var revCollector = require('gulp-rev-collector');               //- 路径替换gulp.task('concat', function() {                                //- 创建一个名为 concat 的 task    gulp.src(['./css/wap_v3.1.css', './css/wap_v3.1.3.css'])    //- 需要处理的css文件,放到一个字符串数组里        .pipe(concat('wap.min.css'))                            //- 合并后的文件名        .pipe(minifyCss())                                      //- 压缩处理成一行        .pipe(rev())                                            //- 文件名加MD5后缀        .pipe(gulp.dest('./css'))                               //- 输出文件本地        .pipe(rev.manifest())                                   //- 生成一个rev-manifest.json        .pipe(gulp.dest('./rev'));                              //- 将 rev-manifest.json 保存到 rev 目录内});gulp.task('rev', function() {    gulp.src(['./rev/*.json', './application/**/header.php'])   //- 读取 rev-manifest.json 文件以及需要进行css名替换的文件        .pipe(revCollector())                                   //- 执行文件内css名的替换        .pipe(gulp.dest('./application/'));                     //- 替换后的文件输出的目录});gulp.task('default', ['concat', 'rev']);
Copy after login

Run results

After passing the above configuration, you can start running gulp to perform related operations on our project;

Use the gulp command to run the Gulp.js build program

First run the concat task to generate a rev-manifest.json file
Then run the rev task to replace the css introduced in the file

YuanWingdeMacBook-Pro:m YuanWing$ gulp concat[11:47:02] Using gulpfile ~/SVN/JKD/m/gulpfile.js[11:47:03] Starting 'concat'...[11:47:03] Finished 'concat' after 12 msYuanWingdeMacBook-Pro:m YuanWing$ gulp rev[11:47:17] Using gulpfile ~/SVN/JKD/m/gulpfile.js[11:47:17] Starting 'rev'...[11:47:17] Finished 'rev' after 10 msYuanWingdeMacBook-Pro:m YuanWing$ 
Copy after login

rev-manifest.jsonFile content:

{  "wap.min.css": "wap.min-c49f62a273.css"}
Copy after login

header.phpComparison before and after replacement:

Before replacement :

After replacement:

gulp-rev-collector's file path replacement is based on rev-manifest.json, so the .json file must be generated first and then replaced;

Conclusion

The plug-ins used above only use a very small part of their own functions. If you are interested, you can experience it yourself:

gulp- rev
gulp-rev-collector
gulp-minify-css
gulp-concat

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template