Guest User

Untitled

a guest
Nov 15th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. var gulp = require('gulp'),
  2. uglify = require('gulp-uglify-es').default,
  3. rename = require("gulp-rename"),
  4. gutil = require('gulp-util'),
  5. less = require('gulp-less'),
  6. cleanCSS = require('gulp-clean-css'),
  7. babel = require('gulp-babel'),
  8. c = require('ansi-colors'),
  9. tap = require('gulp-tap'),
  10. path = require('path'),
  11. changed = require('gulp-changed'),
  12. plumber = require('gulp-plumber');
  13.  
  14. gulp.task('js', function () {
  15. return gulp.src(['./o/js/*.js'])
  16. .pipe(changed('./js/', {extension: '.min.js'}))
  17. .pipe(plumber())
  18. .pipe(babel({
  19. presets: ['@babel/env']
  20. }))
  21. .pipe(uglify())
  22. .on('error', function (err) {
  23. gutil.log(gutil.colors.red('[Error]'), err.toString());
  24. })
  25. .pipe(rename(function (path) {
  26. path.basename += '.min';
  27. path.extname = '.js';
  28. }))
  29. .pipe(gulp.dest('./js/'))
  30. .pipe(tap(function (file, t) {
  31. console.log(c.bold(`⚡️ ${c.grey(' [JS]')} ${c.red.bold(path.basename(file.path))} ${c.blue('✔')}`));
  32. }));
  33. });
  34.  
  35. var lessFiles = [
  36. './o/LESS/index.less',
  37. './o/LESS/default.less'
  38. ];
  39.  
  40. gulp.task('less', function () {
  41. return gulp.src(lessFiles) // path to your file
  42. .pipe(changed('./css/', {extension: '.min.css'}))
  43. .pipe(plumber())
  44. .pipe(less().on('error', function (err) {
  45. gutil.log(gutil.colors.red('[Error]'), err.toString());
  46. this.emit('end');
  47. }))
  48. .pipe(cleanCSS({ rebaseTo: './pathIsIrrevelant' }))
  49. .pipe(rename({ suffix: '.min' }))
  50. .pipe(gulp.dest('./css/'))
  51. .pipe(tap(function (file, t) {
  52. console.log(c.bold(`⚡️ ${c.grey(' [LESS]')} ${c.red.bold(path.basename(file.path))} ${c.blue('✔')}`));
  53. }));
  54. });
  55.  
  56. gulp.task('build', function () {
  57. return Promise.all([
  58. new Promise(function (resolve, reject) {
  59. gulp.src(lessFiles) // path to your file
  60. .pipe(less().on('error', function (err) {
  61. gutil.log(gutil.colors.red('[Error]'), err.toString());
  62. this.emit('end');
  63. }))
  64. .pipe(cleanCSS({ rebaseTo: './pathIsIrrevelant' }))
  65. .pipe(rename({ suffix: '.min' }))
  66. .pipe(gulp.dest('./css/'))
  67. .on('end', function () {
  68. console.log(c.grey.bold('======== LESS 編譯完成 ========'));
  69. })
  70. .on('end', resolve)
  71. .pipe(tap(function (file, t) {
  72. console.log(c.bold(`⚡️ ${c.grey(' [LESS]')} ${c.red.bold(path.basename(file.path))} ${c.blue('✔')}`));
  73. }));
  74. }),
  75. new Promise(function (resolve, reject) {
  76. gulp.src(['./o/js/*.js'])
  77. .pipe(babel({
  78. presets: ['@babel/env']
  79. }))
  80. .pipe(uglify())
  81. .on('error', function (err) {
  82. gutil.log(gutil.colors.red('[Error]'), err.toString());
  83. })
  84. .pipe(rename(function (path) {
  85. path.basename += '.min';
  86. path.extname = '.js';
  87. }))
  88. .pipe(gulp.dest('./js/'))
  89. .on('end', function () {
  90. console.log(c.grey.bold('======== JS 編譯完成 ========'));
  91. })
  92. .on('end', resolve)
  93. .pipe(tap(function (file, t) {
  94. console.log(c.bold(`⚡️ ${c.grey(' [JS]')} ${c.red.bold(path.basename(file.path))} ${c.blue('✔')}`));
  95. }));
  96. })
  97. ]).then(function () {
  98. console.log();
  99. console.log(c.bold(`Build Completed 🎉 🎉 🎉`));
  100. console.log();
  101. })
  102. })
  103.  
  104.  
  105. gulp.task('watch', function (done) {
  106. gulp.watch('o/js/*.js', ['js']);
  107. gulp.watch('./o/LESS/*.less', ['less']);
  108. });
  109.  
  110. gulp.task('default', ['build', 'watch']);
Add Comment
Please, Sign In to add comment