gulpfile.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. layuiAdmin pro 构建
  3. */
  4. var pkg = require('./package.json');
  5. var inds = pkg.independents;
  6. var gulp = require('gulp');
  7. var uglify = require('gulp-uglify');
  8. var minify = require('gulp-minify-css');
  9. var concat = require('gulp-concat');
  10. var rename = require('gulp-rename');
  11. var replace = require('gulp-replace');
  12. var header = require('gulp-header');
  13. var del = require('del');
  14. var gulpif = require('gulp-if');
  15. var minimist = require('minimist');
  16. //获取参数
  17. var argv = require('minimist')(process.argv.slice(2), {
  18. default: {
  19. ver: 'all'
  20. }
  21. })
  22. //注释
  23. ,note = [
  24. '/** <%= pkg.name %>-v<%= pkg.version %> <%= pkg.license %> License By <%= pkg.homepage %> */\n <%= js %>'
  25. ,{pkg: pkg, js: ';'}
  26. ]
  27. ,destDir = './dist' //构建的目标目录
  28. ,releaseDir = '../pack/layuiAdmin.pack/'+ pkg.name +'-v' + pkg.version //发行版本目录
  29. //任务
  30. ,task = {
  31. //压缩 JS
  32. minjs: function(){
  33. var src = [
  34. './src/**/*.js'
  35. ,'!./src/config.js'
  36. ,'!./src/lib/extend/echarts.js'
  37. ];
  38. return gulp.src(src).pipe(uglify())
  39. .pipe(header.apply(null, note))
  40. .pipe(gulp.dest(destDir));
  41. }
  42. //压缩 CSS
  43. ,mincss: function(){
  44. var src = [
  45. './src/**/*.css'
  46. ]
  47. ,noteNew = JSON.parse(JSON.stringify(note));
  48. noteNew[1].js = '';
  49. return gulp.src(src).pipe(minify({
  50. compatibility: 'ie7'
  51. })).pipe(header.apply(null, noteNew))
  52. .pipe(gulp.dest(destDir));
  53. }
  54. //复制文件夹
  55. ,mv: function(){
  56. gulp.src('./src/config.js')
  57. .pipe(gulp.dest(destDir));
  58. gulp.src('./src/lib/extend/echarts.js')
  59. .pipe(gulp.dest(destDir + '/lib/extend'));
  60. gulp.src('./src/style/res/**/*')
  61. .pipe(gulp.dest(destDir + '/style/res'));
  62. return gulp.src('./src/views/**/*')
  63. .pipe(gulp.dest(destDir + '/views'));
  64. }
  65. };
  66. //清理
  67. gulp.task('clear', function(cb) {
  68. return del(['./dist/*'], cb);
  69. });
  70. gulp.task('clearRelease', function(cb) {
  71. return del(['./json/*', releaseDir], cb);
  72. });
  73. gulp.task('minjs', task.minjs);
  74. gulp.task('mincss', task.mincss);
  75. gulp.task('mv', task.mv);
  76. gulp.task('src', function(){ //命令:gulp src
  77. return gulp.src('./dev-pro/**/*')
  78. .pipe(gulp.dest('./src'));
  79. });
  80. //构建核心源文件
  81. gulp.task('default', ['clear', 'src'], function(){ //命令:gulp
  82. for(var key in task){
  83. task[key]();
  84. }
  85. });
  86. //发行 - layuiAdmin 官方使用
  87. gulp.task('release', function(){ //命令:gulp && gulp release
  88. //复制核心文件
  89. gulp.src('./dist/**/*')
  90. .pipe(gulp.dest(releaseDir + '/dist'));
  91. gulp.src('./src/**/*')
  92. .pipe(gulp.dest(releaseDir + '/src'));
  93. //复制 json
  94. gulp.src('./dev/json/**/*')
  95. .pipe(gulp.dest('./json'))
  96. .pipe(gulp.dest('./start/json'))
  97. .pipe(gulp.dest(releaseDir + '/start/json'));
  98. //复制并转义宿主页面
  99. gulp.src('./dev/index.html')
  100. .pipe(replace(/\<\!-- clear s --\>([\s\S]*?)\<\!-- clear e --\>/, ''))
  101. .pipe(replace('//local.res.layui.com/layui/src', 'layui'))
  102. .pipe(replace("base: '../dev-pro/'", "base: '../dist/'"))
  103. .pipe(replace('@@version@@', pkg.version))
  104. .pipe(gulp.dest('./start'))
  105. .pipe(gulp.dest(releaseDir + '/start'));
  106. //复制帮助文件
  107. gulp.src([
  108. './帮助/*'
  109. ,'!./帮助/说明.txt'
  110. ]).pipe(gulp.dest(releaseDir + '/帮助'));
  111. gulp.src([
  112. './帮助/说明.txt'
  113. ]).pipe(gulp.dest(releaseDir));
  114. //复制 gulpfile
  115. gulp.src([
  116. 'gulpfile.js'
  117. ,'package.json'
  118. ]).pipe(gulp.dest(releaseDir));
  119. //说明
  120. gulp.src('../pack/说明.txt')
  121. .pipe(gulp.dest('../pack/layuiAdmin.pack'));
  122. //复制 layui
  123. return gulp.src('../../../../res/layui/rc/**/*')
  124. .pipe(gulp.dest('./start/layui'))
  125. .pipe(gulp.dest(releaseDir + '/start/layui'))
  126. });