Created
May 29, 2017 02:09
-
-
Save josemunozr/8021c39bfa004a81ce90c4b0782a5f56 to your computer and use it in GitHub Desktop.
gulp boilerplate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// File: Gulpfile.js | |
'use strict'; | |
var gulp = require('gulp'), | |
webserver = require('gulp-webserver'), | |
connect = require('gulp-connect'), | |
stylus = require('gulp-stylus'), | |
nib = require('nib'), | |
jshint = require('gulp-jshint'), | |
stylish = require('jshint-stylish'), | |
inject = require('gulp-inject'), | |
wiredep = require('wiredep').stream, | |
gulpif = require('gulp-if'), | |
minifyCss = require('gulp-minify-css'), | |
useref = require('gulp-useref'), | |
uglify = require('gulp-uglify'), | |
uncss = require('gulp-uncss'), | |
angularFilesort = require('gulp-angular-filesort'), | |
templateCache = require('gulp-angular-templatecache'), | |
historyApiFallback = require('connect-history-api-fallback'); | |
var path = { | |
root : './app/', | |
dist : './dist/' | |
} | |
// Servidor web de desarrollo | |
gulp.task('server', function(){ | |
gulp.src( path.root ) | |
.pipe(webserver({ | |
host : "0.0.0.0", | |
port : 8080, | |
livereload : true, | |
fallback : 'index.html', | |
middleware : [ historyApiFallback ] | |
})); | |
}); | |
// Servidor web para probar el entorno de producción | |
gulp.task('server-dist', function() { | |
gulp.src( path.dist ) | |
.pipe(webserver({ | |
host : "0.0.0.0", | |
port : 8080, | |
livereload : true, | |
fallback : 'index.html', | |
middleware : [ historyApiFallback ] | |
})); | |
}); | |
// Busca errores en el JS y nos los muestra por pantalla | |
gulp.task('jshint', function() { | |
return gulp.src('./app/scripts/**/*.js') | |
.pipe(jshint('.jshintrc')) | |
.pipe(jshint.reporter('jshint-stylish')) | |
.pipe(jshint.reporter('fail')); | |
}); | |
// Preprocesa archivos Stylus a CSS y recarga los cambios | |
gulp.task('css', function() { | |
gulp.src('./app/stylesheets/main.styl') | |
.pipe(stylus({ use: nib() })) | |
.pipe(gulp.dest('./app/stylesheets')) | |
.pipe(connect.reload()); | |
}); | |
// Recarga el navegador cuando hay cambios en el HTML | |
gulp.task('html', function() { | |
gulp.src('./app/**/*.html') | |
.pipe(connect.reload()); | |
}); | |
// Busca en las carpetas de estilos y javascript los archivos | |
// para inyectarlos en el index.html | |
gulp.task('inject', function() { | |
return gulp.src('index.html', {cwd: './app'}) | |
.pipe(inject( | |
gulp.src(['./app/scripts/**/*.js']).pipe(angularFilesort()), { | |
read: false, | |
ignorePath: '/app' | |
})) | |
.pipe(inject( | |
gulp.src(['./app/stylesheets/**/*.css']), { | |
read: false, | |
ignorePath: '/app' | |
} | |
)) | |
.pipe(gulp.dest('./app')); | |
}); | |
// Inyecta las librerias que instalemos vía Bower | |
gulp.task('wiredep', function () { | |
gulp.src('./app/index.html') | |
.pipe(wiredep({ | |
directory: './app/lib' | |
})) | |
.pipe(gulp.dest('./app')); | |
}); | |
// Compila las plantillas HTML parciales a JavaScript | |
// para ser inyectadas por AngularJS y minificar el código | |
gulp.task('templates', function() { | |
gulp.src('./app/views/**/*.tpl.html') | |
.pipe(templateCache({ | |
root: 'views/', | |
module: 'blog.templates', | |
standalone: true | |
})) | |
.pipe(gulp.dest('./app/scripts')); | |
}); | |
// Comprime los archivos CSS y JS enlazados en el index.html | |
// y los minifica. | |
gulp.task('compress', function() { | |
gulp.src('./app/index.html') | |
.pipe(useref()) | |
.pipe(gulpif('*.js', uglify({mangle: false }))) | |
.pipe(gulpif('*.css', minifyCss())) | |
.pipe(gulp.dest('./dist')); | |
}); | |
// Elimina el CSS que no es utilizado para reducir el peso del archivo | |
gulp.task('uncss', function() { | |
gulp.src('./dist/css/style.min.css') | |
.pipe(uncss({ | |
html: ['./app/index.html', './app/views/post-list.tpl.html', './app/views/post-detail.tpl.html'] | |
})) | |
.pipe(gulp.dest('./dist/css')); | |
}); | |
// Copia el contenido de los estáticos e index.html al directorio | |
// de producción sin tags de comentarios | |
gulp.task('copy', function() { | |
gulp.src('./app/index.html') | |
.pipe(useref()) | |
.pipe(gulp.dest('./dist')); | |
gulp.src('./app/lib/fontawesome/fonts/**') | |
.pipe(gulp.dest('./dist/fonts')); | |
}); | |
// Vigila cambios que se produzcan en el código | |
// y lanza las tareas relacionadas | |
gulp.task('watch', function() { | |
gulp.watch(['./app/**/*.html'], ['html', 'templates']); | |
gulp.watch(['./app/stylesheets/**/*.styl'], ['css', 'inject']); | |
gulp.watch(['./app/scripts/**/*.js', './Gulpfile.js'], ['jshint', 'inject']); | |
gulp.watch(['./bower.json'], ['wiredep']); | |
}); | |
gulp.task('default', ['server', 'templates', 'inject', 'wiredep', 'watch']); | |
gulp.task('build', ['templates', 'compress', 'copy', 'uncss']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment