glup 基礎插件應用 隨筆

var gulp = require("gulp"),//全局安裝gulp(必須,沒有它別的都廢啦)
    concat = require("gulp-concat"),//合併js文件
    uglify = require("gulp-uglify"),//壓縮js文件
    rename = require('gulp-rename'),//重命名文件流文件
    cssmin = require('gulp-minify-css'),//壓縮css文件
    htmlmin = require('gulp-htmlmin'),//壓縮html文件
    clean = require("gulp-clean"),//刪除文件
    copy = require("gulp-copy");//複製文件
zip = require("gulp-zip");//壓縮打包成zip文件

var paths = ["jquery/jquery.js", "jquery/jquery.fullscreen.js", "js/FullScreen.js", "js/SecondManager.js", "js/Label.js", "js/Differences.js", "js/Scene.js", "js/StartScene.js", "js/GameScene.js", "js/TimeoutScene.js", "js/CompleteScene.js", "js/Audio.js", "js/Game.js", "js/GameSceneDatas.js", "js/Main.js"];

gulp.task("test", function () {
    console.log("concat      合併js文件");
    console.log("uglify      壓縮js文件");
    console.log("cssmin      壓縮css文件");
    console.log("htmlmin     壓縮html文件");
    console.log("clean       刪除文件");
    console.log("copy        複製文件");
    console.log("zip         壓縮打包成zip文件");
    console.log("watch       監控文件修改");
})

gulp.task("concat", function () {
    return gulp.src(paths)
        .pipe(concat("index.js"))
        .pipe(gulp.dest("dist/"));
})

gulp.task("uglify",["concat"], function () {//執行壓縮前會先執行一邊合併
    return gulp.src("dist/index.js")
        .pipe(uglify())
        .pipe(rename("index.min.js"))
        .pipe(gulp.dest("dist/"));
})

gulp.task("cssmin", function () {
    return gulp.src("index.css")
        .pipe(cssmin())
        .pipe(rename("index.min.css"))
        .pipe(gulp.dest("dist/"))
})


gulp.task('htmlmin', function () {
    var options = {
        removeComments: true,//清除HTML註釋
        collapseWhitespace: true,//壓縮HTML
        //省略布爾屬性的值 <input checked="true"/> ==> <input />
        collapseBooleanAttributes: true,
        //刪除全部空格做屬性值 <input id="" /> ==> <input />
        removeEmptyAttributes: true,
        //刪除<script>的type="text/javascript"
        removeScriptTypeAttributes: true,
        //刪除<style>和<link>的type="text/css"
        removeStyleLinkTypeAttributes: true,
        minifyJS: true,//壓縮頁面JS
        minifyCSS: true//壓縮頁面CSS
    };
    return gulp.src('index.html')
        .pipe(htmlmin(options))
        .pipe(rename("index.min.html"))
        .pipe(gulp.dest(''));
});

gulp.task("clean",["uglify"],function () {//刪除文件前會先執行壓縮,確保刪除文件以備壓縮執行
    return gulp.src("dist/index.js")
        .pipe(clean());
})

gulp.task("copy", function () {
    return gulp.src(["mp3/**", "images/**"])
        .pipe(copy("./dist"));
})


gulp.task("zip", ["cssmin", "htmlmin","clean", "copy"], function () {
    return gulp.src(["dist/**","index.min.html"])
        .pipe(zip("jaja.zip"))
        .pipe(gulp.dest(""))
})


gulp.task("watch", function () {
    gulp.watch("js/*.js", ['concat', 'uglify'])
    gulp.watch("index.css", ['cssmin'])
    gulp.watch("index.html", ['htmlmin'])
})
相關文章
相關標籤/搜索