pro.base.js 4.5 KB
Newer Older
Y
Your Name 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
'use strict';

var gulp = require('gulp'),
  path = require('path'),
  fs = require('fs'),
  config = require('./config'),
  _ = require('lodash'),
  $ = require('gulp-load-plugins')({
    pattern: ['gulp-*', 'event-stream', 'main-bower-files', 'uglify-save-license', 'del']
  }),
  browserSync = require('browser-sync'),
  gulpsync = $.sync(gulp),
  reload = browserSync.reload; //实时刷新

const babel = require('gulp-babel');

/**
 * [编译之前将scss注入index.scss]
 */

gulp.task('clear_inject_sass', function () {
  var injectFiles = gulp.src([
    path.join(config.paths.src, 'module/_default/**/*.scss')
  ], {
    read: false
  });

  var injectOptions = {
    transform: function (filePath) {
      filePath = filePath.replace(config.paths.src + '/module/', '../module/');
      return '@import "' + filePath + '";';
    },
    starttag: '//injector',
    endtag: '//endinjector',
    addRootSlash: false
  };
  return gulp.src([path.join(config.paths.src, 'app/index.scss')])
    .pipe($.inject(injectFiles, injectOptions))
    .pipe(gulp.dest(path.join(config.paths.src, 'app/')))
});
/**
 * [代码质量管理]
 */

gulp.task('build:jshint', function () {
  return gulp.src([
    path.join(config.paths.src, '/module/_default/**/*.js'),
    path.join(config.paths.src, 'app/**/*.js')
  ])
    .pipe($.plumber(config.errorHandler()))
    .pipe($.jshint())
    .pipe(reload({
      stream: true
    }))
    .pipe($.size())
    
});


/**
 * [SASS预编译模块,依赖compass模块编译]
 */

gulp.task('build:styles:compass', function () {
  return gulp.src(path.join(config.paths.src, 'app/index.scss'))
    .pipe($.plumber(config.errorHandler()))
    .pipe($.compass({
      config_file: path.join(__dirname, '/../config.rb'),
      css: path.join(config.paths.tmp, '/serve/app/'),
      sass: path.join(config.paths.src, '/app/'),
    }))
    //sprite图片路径修复
    .pipe($.replace('../../../src/assets/images/', '../assets/images/'))
    .pipe(gulp.dest(path.join(config.paths.tmp, '/serve/app/')))
    //css改变时无刷新改变页面
    .pipe(reload({
      stream: true
    }));
});

gulp.task('build:styles:theme:compass', function () {
  return gulp.src(path.join(config.paths.src, '/theme/*.scss'))
    .pipe($.plumber(config.errorHandler()))
    .pipe($.compass({
      config_file: path.join(__dirname, '/../config.rb'),
      css: path.join(config.paths.tmp, '/serve/app/theme/'),
      sass: path.join(config.paths.src, '/theme/')
    }))
    .pipe(gulp.dest(path.join(config.paths.dist, '/theme/')))
});

/**
 * [生成Html模版文件]
 */
gulp.task('build:tmpHtml:app', function () {
  return gulp.src([
          path.join(config.paths.src, '/app/**/*.tmp.html')
      ])
      .pipe($.minifyHtml({
          empty: true,
          spare: true,
          quotes: true
      }))
      .pipe($.angularTemplatecache('eoAppTmpHtml.js', {
          module: config.modules.templateModuleName,
          root: 'app'
      }))
      .pipe(gulp.dest(config.paths.tmp + '//serve/'));
});
gulp.task('build:tmpHtml:module', function () {
  return gulp.src([
          path.join(config.paths.src, '/module/**/*.tmp.html')
      ])
      .pipe($.minifyHtml({
          empty: true,
          spare: true,
          quotes: true
      }))
      .pipe($.angularTemplatecache('eoModuleTmpHtml.js', {
          module: config.modules.templateModuleName,
          root: 'module'
      }))
      .pipe(gulp.dest(config.paths.tmp + '//serve/'));
});
/**
 * [Html中的CSS以及JS注入]
 */

gulp.task('build:inject', ['build:jshint', 'build:styles:compass', 'vendor:base'], function () {
  var injectStyles = gulp.src([
    path.join(config.paths.tmp, '/serve/app/**/*.css'),
    path.join(config.paths.src, 'module/_default/**/*.scss'),
    '!' + path.join(config.paths.tmp, '/serve/app/theme/*.css')
  ], {
    read: false
  });

  var injectScripts = gulp.src([
    path.join(config.paths.src, '/app/**/*.js'),
    path.join(config.paths.src, '/module/_default/**/*.js'),
    path.join('!' + config.paths.src, '/app/vendor.js'),
  ])
  .pipe(babel({
    "presets": ["stage-3", "es2015"]
  }))
  .pipe($.angularFilesort())
  .pipe(gulp.dest(config.paths.tmp + '/serve/app/'))
  

  var injectOptions = {
    ignorePath: [config.paths.src, path.join(config.paths.tmp, '/serve')],
    addRootSlash: false
  };

  return gulp.src(path.join(config.paths.src, '/*.html'))
    .pipe($.plumber(config.errorHandler()))
    .pipe($.inject($.eventStream.merge(
      injectStyles,
      injectScripts
    ), injectOptions))
    .pipe(gulp.dest(path.join(config.paths.tmp, '/serve')));

});