-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
222 lines (187 loc) · 6.91 KB
/
gulpfile.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var gulp = require('gulp'),
del = require('del'),
sass = require('gulp-sass'),
jshint = require('gulp-jshint'),
sourcemaps = require('gulp-sourcemaps'),
browserify = require('browserify'),
source = require('vinyl-source-stream'),
buffer = require('vinyl-buffer'),
uglify = require('gulp-uglify'),
gutil = require('gulp-util'),
ngAnnotate = require('browserify-ngannotate'),
plumber = require('gulp-plumber'),
neat = require('node-neat').includePaths,
browserSync = require('browser-sync').create();
var CacheBuster = require('gulp-cachebust');
var cachebust = new CacheBuster();
/////////////////////////////////////////////////////////////////////////////////////
//
// error handling
//
/////////////////////////////////////////////////////////////////////////////////////
var gulp_src = gulp.src;
gulp.src = function() {
return gulp_src.apply(gulp, arguments)
.pipe(plumber(function(error) {
// Output an error message
gutil.log(gutil.colors.red('******** Error (' + error.plugin + '): ' + error.message));
// emit the end event, to properly end the task
this.emit('end');
})
);
};
/* -------- END Error Handling -------- */
/////////////////////////////////////////////////////////////////////////////////////
//
// cleans the build output
//
/////////////////////////////////////////////////////////////////////////////////////
gulp.task('clean', function (cb) {
del([
'dist'
], cb);
});
/////////////////////////////////////////////////////////////////////////////////////
//
// runs bower to install frontend dependencies
//
/////////////////////////////////////////////////////////////////////////////////////
gulp.task('bower', function() {
var install = require("gulp-install");
return gulp.src(['./bower.json'])
.pipe(install());
});
/////////////////////////////////////////////////////////////////////////////////////
//
// runs sass, creates css source maps
//
/////////////////////////////////////////////////////////////////////////////////////
gulp.task('build-css', function() {
return gulp.src('./app/styles/*')
.pipe(sourcemaps.init())
.pipe(sass({
includePaths: ['build-css'].concat(neat)
}))
.pipe(cachebust.resources())
.pipe(sourcemaps.write('./maps'))
.pipe(gulp.dest('./dist'));
});
/////////////////////////////////////////////////////////////////////////////////////
//
// simple sass
//
/////////////////////////////////////////////////////////////////////////////////////
gulp.task('sass', function(){
return gulp.src('./app/styles/application.scss')
.pipe(sass({
includePaths: ['sass'].concat(neat)
}))
.pipe(gulp.dest('./dist/css'))
.pipe(browserSync.stream());
});
/////////////////////////////////////////////////////////////////////////////////////
//
// fills in the Angular template cache, to prevent loading the html templates via
// separate http requests
//
/////////////////////////////////////////////////////////////////////////////////////
gulp.task('build-template-cache', ['clean'], function() {
var ngHtml2Js = require("gulp-ng-html2js"),
concat = require("gulp-concat");
return gulp.src("./app/states/*.html")
.pipe(ngHtml2Js({
prefix: "/states/"
}))
.pipe(concat("states.js"))
.pipe(gulp.dest("./dist"));
});
/////////////////////////////////////////////////////////////////////////////////////
//
// runs jshint
//
/////////////////////////////////////////////////////////////////////////////////////
gulp.task('jshint', function() {
gulp.src('/app/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
/////////////////////////////////////////////////////////////////////////////////////
//
// Build a minified Javascript bundle - the order of the js files is determined
// by browserify
//
/////////////////////////////////////////////////////////////////////////////////////
gulp.task('build-js', function() {
var b = browserify({
entries: './app/app.js',
debug: true,
paths: ['./app/controllers/*.js', './app/services/*.js', './app/directives/*.js'],
transform: [ngAnnotate]
});
return b.bundle()
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(cachebust.resources())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(uglify())
.on('error', gutil.log)
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./dist/js/'));
});
/////////////////////////////////////////////////////////////////////////////////////
//
// full build applies cache busting to the main page css and js bundles
//
/////////////////////////////////////////////////////////////////////////////////////
gulp.task('build', [ 'clean', 'bower','build-css','build-template-cache', 'jshint', 'build-js'], function() {
return gulp.src('index.html')
.pipe(cachebust.references())
.pipe(gulp.dest('./dist'));
});
/////////////////////////////////////////////////////////////////////////////////////
//
// watches file system and triggers a build when a modification is detected
//
/////////////////////////////////////////////////////////////////////////////////////
gulp.task('watch', function() {
return gulp.watch(['./index.html','./app/states/*.html', './app/styles/*.*css', './app/**/*.js'], ['build']);
});
/////////////////////////////////////////////////////////////////////////////////////
//
// launches a server that serves files in the current directory
//
/////////////////////////////////////////////////////////////////////////////////////
// Static server
gulp.task('serve',['sass'], function() {
browserSync.init({
server: {
baseDir: "./"
}
});
gulp.watch("app/styles/*.*css", ['sass']);
gulp.watch(["app/states/*.html", "./index.html"]).on('change', browserSync.reload);
gulp.watch(['./app/controllers/*.js', './app/services/*.js', './app/directives/*.js']).on('change', browserSync.reload);
});
// Production server
gulp.task('dist-serve', function() {
browserSync.init({
server: {
baseDir: "./dist"
}
});
gulp.watch("app/styles/*.*css", ['build-css']);
gulp.watch(["app/states/*.html", "./index.html"]).on('change', browserSync.reload);
gulp.watch(['./app/controllers/*.js', './app/services/*.js', './app/directives/*.js']).on('change', browserSync.reload);
});
/////////////////////////////////////////////////////////////////////////////////////
//
// launch a build upon modification and publish it to a running server
//
/////////////////////////////////////////////////////////////////////////////////////
gulp.task('dev', ['watch', 'serve']);
/////////////////////////////////////////////////////////////////////////////////////
//
// installs and builds everything
//
/////////////////////////////////////////////////////////////////////////////////////
gulp.task('default', ['build']);