記錄下本身寫的gulp打包腳本

var c = {
    rootPath: 'src',//項目文件夾
    outputPath: 'output',//文件編譯後的輸出目錄
    revPath: 'manifest',//rev映射文件目錄
    appjs: 'app.js',// run JS
    homePage: 'index.html',
    cssFolderPaths: ['css'],//須要壓縮的CSS目錄
    jsFolderPaths: ['model'],//須要壓縮的JS目錄
    ngjsFolderPaths: ['app', 'common', 'directives', 'filters'],//須要壓縮的ngJS目錄
    htmlFolderPaths: ['app', 'layout'],
    filterFolderPaths: ['frameworks', 'images']
};


var root = function (path) {
    if (path)
        return c.rootPath + '/' + path
    return c.rootPath;
};

var output = function (path) {
    if (path)
        return c.outputPath + '/' + path
    return c.outputPath;
}

var fs = require('fs');
var path = require('path');
var gulp = require("gulp"),
  clean = require('gulp-clean'),
  ngAnnotate = require('gulp-ng-annotate'),//壓縮ngjs
  util = require('gulp-util'),
  filter = require('gulp-filter'),
  ngmin = require('gulp-ngmin'),
  stripDebug = require('gulp-strip-debug'),
  minifyhtml = require("gulp-minify-html"),//壓縮html
  minify = require("gulp-minify-css"),//壓縮css
  uglify = require("gulp-uglify"),//壓縮代碼
  rename = require("gulp-rename"),//重命名
  concat = require("gulp-concat"),//合併代碼
  sourcemaps = require("gulp-sourcemaps"),
  rev = require("gulp-rev"),//對文件名加MD5後綴
  revReplace = require("gulp-rev-replace"),//路徑替換
  merge = require("merge-stream");


//入口命令
gulp.task('run', ['clear'],
  function () {
      setTimeout(function () {
          gulp.run([
            'filter',
            'css',
            'html',
            'global',
            'model',
            'ngjs'], function () {
                var manifest = gulp.src("./" + c.revPath + '/*.json');
                return gulp.src([root(c.homePage)])
                  .pipe(revReplace({
                      manifest: manifest
                  }))
                  .pipe(gulp.dest(c.outputPath));
            });
      }, 2000);
  });

//全部不須要壓縮及合併的目錄
gulp.task('filter', function () {
    return c.filterFolderPaths.map(function (cpath) {
        return gulp.src([root(cpath + '/**')])
          .pipe(gulp.dest(output(cpath)));
    });
});

//壓縮css
gulp.task('css', function () {
    var tasks = c.cssFolderPaths.map(function (cpath) {
        return gulp.src([root(cpath + '/**/*.css')])
          .pipe(minify())
          .pipe(rev())
          .pipe(gulp.dest(output(cpath)))
          .pipe(rev.manifest(cpath + '.json'))
          .pipe(gulp.dest('./' + c.revPath));
    });
    return merge(tasks);
});

//壓縮html
gulp.task('html', function () {
    var tasks = c.htmlFolderPaths.map(function (cpath) {
        return gulp.src([root(cpath + '/**/*.html')])
          .pipe(minifyhtml())
          .pipe(gulp.dest(output(cpath)));
    });
    return merge(tasks);
});

//壓縮global js文件,並替換index.html中的引用
gulp.task('global', function () {
    return gulp.src([root('/*.js')])
      .pipe(ngAnnotate())
      .pipe(ngmin({ dynamic: false }))
      .pipe(stripDebug())
      .pipe(uglify({ outSourceMap: false }))//壓縮ngjs
    // .pipe(concat(c.appjs))
      .pipe(rev()) //文件名加MD5後綴
      .pipe(gulp.dest(c.outputPath))
      .pipe(rev.manifest('global.json'))//生成manifest.json
      .pipe(gulp.dest('./' + c.revPath));
});

//壓縮model
gulp.task('model', function () {
    var tasks = c.jsFolderPaths.map(function (cpath) {
        return gulp.src([root(cpath + '/**/*.js')])
          .pipe(uglify())
          .pipe(rev())
          .pipe(gulp.dest(output(cpath)))
          .pipe(rev.manifest(cpath + '.json'))
          .pipe(gulp.dest('./' + c.revPath));
    });
    return merge(tasks);
});

//壓縮ngjs
gulp.task('ngjs', function () {
    var tasks = c.ngjsFolderPaths.map(function (cpath) {
        return gulp.src([root(cpath + '/**/*.js')])
          .pipe(uglify())
          .pipe(rename(function (path) {
              if (path.basename.toLowerCase().indexOf('login') > -1) {
                  path.basename = path.basename.toLowerCase();
              }
              return path;
          }))
          .pipe(rev())
          .pipe(gulp.dest(output(cpath)))
          .pipe(rev.manifest(cpath + '.json'))
          .pipe(gulp.dest('./' + c.revPath));
    });
    return merge(tasks);
});

//清理目錄
gulp.task('clear', function () {
    gulp.src(c.outputPath, { read: false })
      .pipe(clean());
    gulp.src(c.revPath, { read: false })
      .pipe(clean());
    gulp.src(['dist', 'rev'], { read: false })
      .pipe(clean());
    //gulp.src(root('/**/*.*scc'), { read: false })
     // .pipe(clean());
});
相關文章
相關標籤/搜索