Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var gulp        = require('gulp');
  2. //var browserSync = require('browser-sync');
  3. var sass        = require('gulp-sass');
  4. var pug = require('gulp-pug');
  5. //var reload      = browserSync.reload;
  6. //var cache       = require('gulp-cached');
  7. var fs = require('fs');
  8. var path = require('path');
  9. var merge = require('merge-stream');
  10. var concat = require('gulp-concat');
  11. var rename = require('gulp-rename');
  12. var uglify = require('gulp-uglify');
  13. var babel = require("gulp-babel");
  14. var notifier = require('node-notifier');
  15.  
  16. var scriptsPath = 'js'; // Donde estan los scripts
  17. var builtPath = 'jsc'; // Donde iran los compilados
  18.  
  19.  
  20. function errorHandlerSass(error) {
  21.   notifier.notify({
  22.     'title': 'Error ' + error.relativePath.toString(),
  23.     'message': 'Line: ' + error.line.toString() + ' - ' + error.messageOriginal.toString()
  24.   });
  25.   this.emit('end');
  26. }
  27. function errorHandlerPug(error) {
  28.   try {
  29.     var filename = error.fileName.toString();
  30.     notifier.notify({
  31.       'title': 'Error ' + path.relative(__dirname, filename),
  32.       'message': 'Line: ' + error.line.toString() + ' - ' + error.msg.toString()
  33.     });
  34.   } catch(e) {
  35.     notifier.notify('Error fatal Pug');
  36.     console.log(error);
  37.   }
  38.   this.emit('end');
  39. }
  40. function errorHandlerBabel(error) {
  41.   var filename = error.fileName.toString();
  42.   notifier.notify({
  43.     'title': 'Error ' + path.relative(__dirname, filename),
  44.     'message': 'Line: ' + error.loc.line.toString() + ' - ' + error.message.toString().substr(filename.length + 2)
  45.   });
  46.   this.emit('end');
  47. }
  48.  
  49. function getFolders(dir) {
  50.   return fs.readdirSync(dir)
  51.     .filter(function(file) {
  52.       return fs.statSync(path.join(dir, file)).isDirectory();
  53.     });
  54. }
  55.  
  56. gulp.task('scripts', function() {
  57.   var folders = getFolders(scriptsPath);
  58.   var tasks = folders.map(function(folder) {
  59.     return gulp.src(path.join(scriptsPath, folder, '/**/*.js'))
  60.       .pipe(babel({presets: ['es2015', 'react'] }).on('error', errorHandlerBabel))
  61.       .pipe(uglify())
  62.       .pipe(gulp.dest(path.join(builtPath, folder)))
  63.    });
  64.  
  65.    // process all remaining files in scriptsPath root into main.js and main.min.js files
  66.    var root = gulp.src(path.join(scriptsPath, '/*.js'))
  67.         .pipe(babel({presets: ['es2015', 'react'] }).on('error', errorHandlerBabel))
  68.         .pipe(uglify())
  69.         .pipe(gulp.dest(builtPath))
  70.  
  71.    return merge(tasks, root);
  72. });
  73.  
  74. gulp.task('sass-main', function () {
  75.   return gulp.src('./assets/sass/main.sass')
  76.     .pipe(sass({indentedSyntax: true, outputStyle: 'compressed'}).on('error', errorHandlerSass))
  77.     .pipe(gulp.dest('./assets/css'));
  78.  
  79. });
  80.  
  81. gulp.task('pug-main', function() {
  82.   return gulp.src('./pug/*.pug')
  83.     .pipe(pug({
  84.       pretty: true
  85.     }).on('error', errorHandlerPug))
  86.     .pipe(gulp.dest('./html/'))
  87. });
  88.  
  89. /**
  90.  * Crea el Servidor local y mantiene los archivos listos para cambios
  91.  */
  92. // Se ejecutan los task de sass-main scripts y pug-main antes de iniciar con el task 'default'
  93. gulp.task('default', ['sass-main', 'scripts', 'pug-main'], function () {
  94. /*
  95.     browserSync({
  96.         server: './PUBLIC',
  97.         ip: "0.0.0.0",
  98.         port: 8080,//env.PORT,
  99.         ui: { port: 8081 }
  100.     });*/
  101.   // Cambia cualquier archivo .sass y compila el main.sass con dependencias
  102.   // el cwd permite hacer watching aun cuando se crean o renombran archivos
  103.   gulp.watch('assets/sass/**/*.sass', {cwd: './'}, ['sass-main']);//sass
  104.   gulp.watch('js/**/*.js', {cwd: './'}, ['scripts']);//js-babel
  105.   gulp.watch('pug/**/*.pug', {cwd: './'}, ['pug-main']);//pug (ex-jade)
  106.  
  107. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement