Skip to content

Instantly share code, notes, and snippets.

@sirgalleto
Created October 20, 2015 16:29
Show Gist options
  • Save sirgalleto/f96acc1a49e1051402c7 to your computer and use it in GitHub Desktop.
Save sirgalleto/f96acc1a49e1051402c7 to your computer and use it in GitHub Desktop.
Gulpfile for angular based aplication
var gulp = require('gulp-help')(require('gulp'))
, browserSync = require('browser-sync')
, jshint = require('gulp-jshint')
, inject = require('gulp-inject')
, wiredep = require('wiredep').stream
, templateCache = require('gulp-angular-templatecache')
, gulpif = require('gulp-if')
, minifyCss = require('gulp-minify-css')
, useref = require('gulp-useref')
, uglify = require('gulp-uglify')
, uncss = require('gulp-uncss')
, ngConstant = require('gulp-ng-constant');
/* serve */
gulp.task('serve', 'Start a serve for dev enviroment', function(){
browserSync({
server: {
baseDir: __dirname + '/app/',
directory: true
},
ghostMode: false,
notify: false,
debounce: 200,
port: 8901,
startPath: 'index.html'
});
gulp.watch([
__dirname + '/app/**/*.{js,html,css,svg,png,gif,jpg,jpeg}'
], {
debounceDelay: 400
}, function() {
browserSync.reload();
});
});
/* dist */
gulp.task('dist', 'Start a serve for production enviroment', function(){
browserSync({
server: {
baseDir: __dirname + '/app/',
directory: true
},
ghostMode: false,
notify: false,
debounce: 200,
port: 8901,
startPath: 'index.html'
});
gulp.watch([
__dirname + '/app/**/*.{js,html,css,svg,png,gif,jpg,jpeg}'
], {
debounceDelay: 400
}, function() {
browserSync.reload();
});
});
/* index */
gulp.task('index', 'Copy index.html to app/', function(){
gulp.src('./app/views/index.html')
.pipe(gulp.dest('./app'));
});
/* inject */
gulp.task('inject', 'Inject dependencies', ['index', 'config'], function() {
var sources = gulp.src(['./app/js/**/*.js','./app/style/**/*.css']);
return gulp.src('index.html', {cwd: './app'})
.pipe(inject(sources, {
read: false,
ignorePath: '/app'
}))
.pipe(gulp.dest('./app'));
});
/* wiredep */
gulp.task('wiredep', 'Inject bower dependencies', ['index'], function () {
gulp.src('./app/index.html')
.pipe(wiredep({
directory: './app/lib'
}))
.pipe(gulp.dest('./app'));
});
/* templates */
gulp.task('templates', 'compiles templates to a angular module', ['index'], function(){
gulp.src('./app/views/**/*.html')
.pipe(templateCache({
root: 'views/',
module: 'psAdvance.templates',
standalone: true
}))
.pipe(gulp.dest('./app/js/templates'));
});
/* compress */
gulp.task('compress', 'Minify and uglify dependencies', ['index'], function(){
gulp.src('./app/index.html')
.pipe(useref.assets())
.pipe(gulpif('*.js', uglify({mangle: false})))
.pipe(gulpif('*.css', minifyCss()))
.pipe(gulp.dest('./dist'));
});
/* copy */
gulp.task('copy', 'Copy files to dist enviroment', function(){
gulp.src('./app/index.html')
.pipe(useref())
.pipe(gulp.dest('./dist'));
gulp.src('./app/img/**')
.pipe(gulp.dest('./dist/img'));
gulp.src('./app/fonts/**')
.pipe(gulp.dest('./dist/fonts'));
gulp.src('./app/fonts/**')
.pipe(gulp.dest('./dist/fonts'));
gulp.src('./app/style/**')
.pipe(gulp.dest('./dist/style'));
});
/* config */
gulp.task('config', 'Use de config.json in angular constant module', function () {
gulp.src('config.json')
.pipe(ngConstant())
.pipe(gulp.dest('./app/js/core/'));
});
/* watch */
gulp.task('watch', 'Watch files to execute task', function(){
gulp.watch(['./bower.json'], ['wiredep']);
gulp.watch(['./app/js/**/*.js'], ['inject']);
gulp.watch(['config.json'], ['config','inject']);
gulp.watch(['./app/views/**/*.html'], ['templates']);
});
/*
* body
*/
gulp.task('prepare', 'Prepare task', ['config', 'wiredep', 'inject', 'templates']);
gulp.task('build', 'Build application to production enviroment', ['prepare', 'compress', 'copy']);
gulp.task('default', 'Execute a dev enviroment', ['index', 'prepare', 'serve', 'watch', 'inject']);
@sirgalleto
Copy link
Author

Usage

Use $gulp help to describe the tasks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment