base.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 'use strict';
  2. var gulp = require('gulp'),
  3. path = require('path'),
  4. fs = require('fs'),
  5. config = require('./config'),
  6. _ = require('lodash'),
  7. $ = require('gulp-load-plugins')({
  8. pattern: ['gulp-*', 'event-stream', 'main-bower-files', 'uglify-save-license', 'del']
  9. }),
  10. browserSync = require('browser-sync'),
  11. gulpsync = $.sync(gulp),
  12. reload = browserSync.reload;//实时刷新
  13. gulp.task('dev-config',function () {
  14. return gulp.src('app.conf.json')
  15. .pipe($.ngConfig(config.modules.ConstantModuleName,{
  16. environment: 'development',
  17. createModule: false,
  18. wrap: true
  19. }))
  20. .pipe(gulp.dest(path.join(config.paths.src,'/app')))//join() 方法用于把数组中的所有元素放入一个字符串。
  21. });
  22. gulp.task('prod-config',function () {
  23. return gulp.src('app.conf.json')
  24. .pipe($.ngConfig(config.modules.ConstantModuleName,{
  25. environment: 'production',
  26. createModule: false,
  27. wrap: true//生成闭包
  28. }))
  29. .pipe(gulp.dest(path.join(config.paths.src,'/app')))
  30. });
  31. /**
  32. * [代码质量管理]
  33. */
  34. gulp.task('jshint',function () {
  35. return gulp.src(path.join(config.paths.src,'app/**/*.js'))
  36. .pipe($.plumber(config.errorHandler()))
  37. .pipe($.jshint())
  38. .pipe(reload({ stream: true }))
  39. .pipe($.size());
  40. });
  41. /**
  42. * [清理DIST,TEMP文件夹]
  43. */
  44. gulp.task('clean', function () {
  45. $.del([path.join(config.paths.dist, '/'), path.join(config.paths.tmp, '/')]);
  46. });
  47. /**
  48. * [编译之前将scss注入index.scss]
  49. */
  50. gulp.task('inject_sass',function () {
  51. var injectFiles = gulp.src([
  52. path.join(config.paths.src,'app/**/*.scss'),
  53. path.join('!'+ config.paths.src, 'app/index.scss')
  54. ],{read:false});
  55. var injectOptions = {
  56. transform: function(filePath) {
  57. filePath = filePath.replace(config.paths.src + '/app/', '');
  58. return '@import "' + filePath + '";';
  59. },
  60. starttag: '// injector',
  61. endtag: '// endinjector',
  62. addRootSlash: false
  63. };
  64. return gulp.src(path.join(config.paths.src,'app/index.scss'))
  65. .pipe($.inject(injectFiles,injectOptions))
  66. .pipe(gulp.dest(path.join(config.paths.src,'app/')))
  67. });
  68. gulp.task('clean', function () {
  69. $.del([path.join(config.paths.dist, '/'), path.join(config.paths.tmp, '/')]);
  70. });
  71. /**
  72. * [SASS预编译模块,依赖compass模块编译]
  73. */
  74. gulp.task('styles:compass',['inject_sass'],function () {
  75. return gulp.src(path.join(config.paths.src,'app/index.scss'))
  76. .pipe($.plumber(config.errorHandler()))
  77. .pipe($.sass())
  78. //sprite图片路径修复
  79. .pipe($.replace('../../../src/assets/images/', '../assets/images/'))
  80. .pipe(gulp.dest(path.join(config.paths.tmp,'/serve/app/')))
  81. //css改变时无刷新改变页面
  82. .pipe(reload({ stream: true }));
  83. });
  84. /**
  85. * [Html中的CSS以及JS注入]
  86. */
  87. gulp.task('inject', ['jshint', 'styles:compass','vendor:base'], function () {
  88. var injectStyles = gulp.src([
  89. path.join(config.paths.tmp, '/serve/app/**/*.css')
  90. ], { read: false });
  91. var injectScripts = gulp.src([
  92. path.join(config.paths.src, '/app/**/*.js'),
  93. path.join('!' +config.paths.src, '/app/vendor.js'),
  94. ]).pipe($.angularFilesort());
  95. var injectOptions = {
  96. ignorePath: [config.paths.src, path.join(config.paths.tmp, '/serve')],
  97. addRootSlash: false
  98. };
  99. return gulp.src(path.join(config.paths.src, '/*.html'))
  100. .pipe($.plumber(config.errorHandler()))
  101. .pipe($.inject($.eventStream.merge(
  102. injectStyles,
  103. injectScripts
  104. ),injectOptions))
  105. .pipe(gulp.dest(path.join(config.paths.tmp, '/serve')));
  106. });
  107. gulp.task('vendor', gulpsync.sync(['vendor:base', 'vendor:app']) );
  108. /**
  109. * [复制依赖文件]
  110. */
  111. gulp.task('vendor:base', function() {
  112. var jsFilter = $.filter('**/*.js',{restore: true}),
  113. cssFilter = $.filter('**/*.css',{restore: true});
  114. return gulp.src(config.vendor.base.source,{base: 'bower_components'})
  115. .pipe($.expectFile(config.vendor.base.source))
  116. .pipe(jsFilter)
  117. .pipe($.concat(config.vendor.base.name+'.js'))
  118. .pipe(jsFilter.restore)
  119. .pipe(cssFilter)
  120. .pipe($.concat(config.vendor.base.name+'.scss'))
  121. .pipe(cssFilter.restore)
  122. .pipe(gulp.dest(config.vendor.base.dest))
  123. ;
  124. });
  125. gulp.task('vendor:app', function() {
  126. var jsFilter = $.filter('*.js',{restore: true}),
  127. cssFilter = $.filter('*.css',{restore: true});
  128. return gulp.src(config.vendor.app.source, {base: 'bower_components'})
  129. .pipe($.expectFile(config.vendor.app.source))
  130. .pipe(jsFilter)
  131. .pipe(jsFilter.restore)
  132. .pipe(cssFilter)
  133. .pipe(cssFilter.restore)
  134. .pipe(gulp.dest(config.vendor.app.dest) );
  135. });