Introduction to various gulp plugins


gulp module

const gulp = require('gulp');
// 引用插件
const htmlmin = require('gulp-htmlmin');
const fileinclude = require('gulp-file-include');
const less = require('gulp-less');
const csso = require('gulp-csso');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');

// The following code is executed, not the entire file

Use gulp.task to create tasks

1. The name of the task

2. Task callback function

gulp.task('first', async () => {
    
    
    await console.log("人生中的第一个gulp任务执行完毕");
    // 1.使用gulp.src获取要处理的文件
    gulp.src('./src/css/style.css', {
    
    
            allowEmpty: true
        })
        // 通过pipe方法处理base.css文件
        // pipe方法里面写的是 如何处理代码  
        .pipe(gulp.dest('dist/css')); // 让base.css在dist/css文件夹中输出
});

html task

1. Code compression operation in html file

2. Extract the common code of html file

gulp.task('htmlmin', async () => {
    
    
    gulp.src('./src/*.html', {
    
    
            allowEmpty: true
        })
        .pipe(fileinclude())
        .pipe(htmlmin({
    
    
            collapseWhitespace: true
        }))
        .pipe(gulp.dest('./dist'));

});

css task

1.less syntax conversion

2. Extract the common code in the html file

gulp.task('cssmin', async () => {
    
    
    // 选择css目录下的less和css文件
    gulp.src(['./src/css/*.less', './src/css/*.css'])
        // 将less语法转换为css语法
        .pipe(less())
        // 将css代码进行压缩
        .pipe(csso())
        // 将处理的结果进行输出
        .pipe(gulp.dest('dist/css'));
})

js task

1.es6 code conversion

2. Code compression

gulp.task('jsmin', async () => {
    
    
    gulp.src('./src/js/*.js')
        // 将es5代码转换为es6代码
        .pipe(babel({
    
    
            // 可以判断当前代码的运行环境,将代码转换为当前运行环境所支持的代码
            presets: ['@babel/env']
        }))
        // 把转换的结果进行压缩
        .pipe(uglify())
        .pipe(gulp.dest('dist/js'))
})

Remaining folders and remaining files

// 复制文件夹
gulp.task('copy', async () => {
    
    
    gulp.src('./src/img.*')
        .pipe(gulp.dest('dist/img'));
    gulp.src('./src/*.md')
        .pipe(gulp.dest('dist'));
});

Perform all the above functions at once

// 构建任务
gulp.task('default', gulp.series("htmlmin", "jsmin", "cssmin", "copy"));
// 错误代码
// gulp.task('default', ["htmlmin", "jsmin", "cssmin", "copy"]);

Self-motivation

Don't be stupid, if you abandon everything, you will definitely learn the technology well. Come on, you look the most handsome when you work hard! !

Guess you like

Origin blog.csdn.net/weixin_50001396/article/details/112094532