Table of Contents
Install the Gulp plug-in into the local project
Create the configuration file gulpfile.js
Run results
Conclusion
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

Jun 24, 2016 am 11:42 AM

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to distinguish between closing a browser tab and closing the entire browser using JavaScript? How to distinguish between closing a browser tab and closing the entire browser using JavaScript? Apr 04, 2025 pm 10:21 PM

How to distinguish between closing tabs and closing entire browser using JavaScript on your browser? During the daily use of the browser, users may...

See all articles