File size: 4,189 Bytes
597ee18 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | var gulp = require('gulp'),
sass = require('gulp-sass'),
concat = require('gulp-concat'),
jshint = require('gulp-jshint'),
runSequence = require('run-sequence'),
del = require('del'),
refresh = require('gulp-livereload'),
lrserver = require('tiny-lr')(),
express = require('express'),
livereload = require('connect-livereload'),
copy = require('gulp-copy'),
uglify = require('gulp-uglify'),
usemin = require('gulp-usemin'),
annotate = require('gulp-ng-annotate'),
path = require('path'),
minifyCss = require('gulp-minify-css'),
minifyHtml = require('gulp-minify-html'),
rev = require('gulp-rev'),
sourcemaps = require('gulp-sourcemaps'),
templateCache = require('gulp-angular-templatecache'),
awspublish = require('gulp-awspublish');
var s3 = require('./s3');
var livereloadport = 35729,
serverport = 5000;
// DEV SERVER
var devServer = express();
devServer.use(livereload({port: livereloadport}));
devServer.use(express.static('./src'));
devServer.all('/*', function (req, res) {
res.sendFile('index.html', {root: 'src'});
});
// PATHS
var pathToIndexFile = 'src/index.html';
var pathToJsSource = 'src/app/**/*.js';
var pathToStyleSource = 'src/app/**/*.scss';
var pathToTemplates = 'src/app/**/*.html';
gulp.task('default', ['dev'], function () {
});
gulp.task('dev', [
'buildDev',
'startDevServer',
'watchSource'
], function () {
});
gulp.task('buildDev', [
'buildJs',
'buildStyle',
'cacheTemplates'
], function () {
});
gulp.task('buildJs', function () {
gulp.src(pathToJsSource)
.pipe(sourcemaps.init())
.pipe(concat('all-source.js'))
.pipe(sourcemaps.write())
.pipe(gulp.dest('src/build'))
.pipe(refresh(lrserver));
});
gulp.task('buildStyle', function () {
gulp.src('src/app/style.scss')
.pipe(sass())
.pipe(gulp.dest('src/build'));
gulp.src(pathToIndexFile)
.pipe(refresh(lrserver));
});
gulp.task('cacheTemplates', function () {
gulp.src(pathToTemplates)
.pipe(templateCache({module: 'app'}))
.pipe(gulp.dest('src/build'))
.pipe(refresh(lrserver));
});
gulp.task('startDevServer', function () {
devServer.listen(serverport);
lrserver.listen(livereloadport);
});
gulp.task('watchSource', function () {
gulp.watch(pathToJsSource, ['buildJs', 'lint']);
gulp.watch(pathToStyleSource, ['buildStyle']);
gulp.watch(pathToIndexFile, ['reloadIndex']);
gulp.watch(pathToTemplates, ['cacheTemplates']);
});
gulp.task('reloadIndex', function () {
gulp.src(pathToIndexFile)
.pipe(refresh(lrserver));
});
gulp.task('lint', function () {
gulp.src(pathToJsSource)
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
/////////////////////////////////////
/////////////// PROD ///////////////
///////////////////////////////////
gulp.task('prod', [
'dist',
'startProdServer'
], function () {
});
gulp.task('dist', function () {
runSequence(
'cleanDistFolder',
'buildDev',
'buildDist'
);
}
);
gulp.task('cleanDistFolder', function (cb) {
del('dist', cb);
});
gulp.task('buildDist', function () {
gulp.src('src/index.html')
.pipe(usemin({
css: [minifyCss()],
html: [minifyHtml({empty: true})],
js: [annotate(), uglify(), rev()]
}))
.pipe(gulp.dest('dist'));
gulp.src('src/img/**')
.pipe(copy('dist', {prefix: 1}));
});
gulp.task('startProdServer', function () {
var server = express();
server.use(express.static('./dist'));
server.all('/*', function (req, res) {
res.sendFile('index.html', {root: 'dist'});
});
server.listen(serverport);
});
gulp.task('s3', function () {
var publisher = awspublish.create(s3);
var headers = {
'Cache-Control': 'max-age=5, no-transform, public' // 5 seconds cache TTL
};
return gulp.src('dist/**/*')
.pipe(publisher.publish(headers))
.pipe(publisher.cache()) // create a cache file to speed up consecutive uploads
.pipe(awspublish.reporter());
}); |