Gulp构建多页面项目

全局安装gulp

cpnm install gulp -g

然后本地在安装一个gulp添加到配置文件

cnpm install gulp --save

然后就是配置一些gulp的插件

const connect = require('gulp-connect');
const htmlmin = require('gulp-htmlmin');  
const uglify = require('gulp-uglify');
const sass = require('gulp-sass');
const babel = require('gulp-babel');
const imagemin = require('gulp-imagemin');
const pngquant = require('imagemin-pngquant');
const cache = require('gulp-cache');
const rename = require('gulp-rename');
const runSequence = require('gulp-sequence');
const del = require('del');

新增然后配置gulpfile.js

//path
let path = {
    //其他资源 path
    staticPath: ['src/build/css/*.*'],
    //html patn
    htmlPath: ['src/**/*.html'],
    //sass path
    sassPath: ['src/build/sass/*.scss'],
    //js path
    jsPath: ['src/build/js/*.js'],
    //images path
    imagesPath: ['src/build/img/*.{png,jpg,gif,ico}'],
    // clear path
    clean: ['./dist/**/*.*']
}


// default 默认执行任务
gulp.task('default',cb => {
    runSequence(
        'clean', // 第一步:清理目标目录
        'dest', // 第二步:打包
        'watch', // 第三步:监控
        cb
    );
});


//Clean target
gulp.task('clean', function(){
    del(path.clean).then(function(){
        console.log('Deleted files and folders:\n' + JSON.stringify(path));
    });
});

//拷贝静态资源文件
gulp.task('file',() => {
    //获取文件
    gulp.src(path.staticPath)
        //让文件流走向下个环节
        .pipe(gulp.dest('dist/build'))
})
//当文件修改时自动同步
gulp.task('watchFile',() => {gulp.watch(path.staticPath,['file']);})

//压缩同步html
gulp.task('html',() => {
    gulp.src(path.htmlPath)
        //html压缩
        .pipe(htmlmin({
            removeComments: true,//清除HTML注释
            collapseWhitespace: true,//压缩HTML
            removeEmptyAttributes: true,//删除所有空格作属性值  ==> 
            removeScriptTypeAttributes: true,//删除
                    
                    

你可能感兴趣的:(Gulp构建多页面项目)