-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
249 lines (196 loc) · 7.18 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*=====================================
= Default Configuration =
=====================================*/
// Please use config.js to override these selectively:
var config = {
dest: 'www',
cordova: false,
minify_images: true,
vendor: {
js: [
'./bower_components/angular/angular.js',
'./bower_components/angular-route/angular-route.js',
'./bower_components/mobile-angular-ui/dist/js/mobile-angular-ui.js',
'./bower_components/mobile-angular-ui/dist/js/mobile-angular-ui.gestures.js'
],
fonts: [
'./bower_components/font-awesome/fonts/fontawesome-webfont.*'
]
}
};
/*----- End of Configuration ------*/
/*========================================
= Requiring stuffs =
========================================*/
var gulp = require('gulp'),
seq = require('run-sequence'),
less = require('gulp-less'),
uglify = require('gulp-uglify'),
sourcemaps = require('gulp-sourcemaps'),
cssmin = require('gulp-cssmin'),
order = require('gulp-order'),
concat = require('gulp-concat'),
ignore = require('gulp-ignore'),
rimraf = require('gulp-rimraf'),
imagemin = require('gulp-imagemin'),
pngcrush = require('imagemin-pngcrush'),
templateCache = require('gulp-angular-templatecache'),
mobilizer = require('gulp-mobilizer'),
ngAnnotate = require('gulp-ng-annotate'),
replace = require('gulp-replace'),
ngFilesort = require('gulp-angular-filesort'),
streamqueue = require('streamqueue'),
rename = require('gulp-rename'),
path = require('path'),
gls = require('gulp-live-server'),
del = require('del');
/*================================================
= Report Errors to Console =
================================================*/
gulp.on('error', function(e) {
throw(e);
});
/*=========================================
= Clean dest folder =
=========================================*/
gulp.task('clean', function () {
return del([
'www/**/*'
]);
});
/*==========================================
= Start a web server =
==========================================*/
gulp.task('server', function() {
var server = gls.new('app.js');
server.start();
//use gulp.watch to trigger server actions(notify, start or stop)
gulp.watch(['www/**/*.css', 'www/**/*.html'], server.notify);
gulp.watch('app.js', server.start); //restart my server
});
/*==============================================================
= Setup live reloading on source changes =
==============================================================*/
gulp.task('livereload', function () {
gulp.src(path.join(config.dest, '*.html'))
.pipe(gls.notify());
});
/*=====================================
= Minify images =
=====================================*/
gulp.task('images', function () {
var stream = gulp.src('src/images/**/*');
if (config.minify_images) {
stream = stream.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngcrush()]
}));
}
return stream.pipe(gulp.dest(path.join(config.dest, 'images')));
});
/*=================================================
= Copy html files to dest =
=================================================*/
gulp.task('html', function() {
var inject = [];
if (typeof config.weinre === 'object') {
inject.push('<script src="http://'+config.weinre.boundHost+':'+config.weinre.httpPort+'/target/target-script-min.js"></script>');
}
if (config.cordova) {
inject.push('<script src="cordova.js"></script>');
}
gulp.src(['src/html/**/*.html'])
.pipe(replace('<!-- inject:js -->', inject.join('\n ')))
.pipe(gulp.dest(config.dest));
});
/*======================================================================
= Compile, minify, mobilize less =
======================================================================*/
gulp.task('less', function () {
gulp.src(['./src/less/app.less'])
.pipe(less({
paths: [ path.resolve(__dirname, 'src/less'), path.resolve(__dirname, 'bower_components') ]
}))
.pipe(mobilizer('app.css', {
'app.css': {
hover: 'exclude',
screens: ['0px']
},
'hover.css': {
hover: 'only',
screens: ['0px']
}
}))
.pipe(cssmin())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest(path.join(config.dest, 'css')));
});
/*====================================================================
= Compile and minify js generating source maps =
====================================================================*/
// - Orders ng deps automatically
// - Precompile templates to ng templateCache
gulp.task('js', function() {
streamqueue({ objectMode: true },
gulp.src(config.vendor.js),
gulp.src('./src/js/**/*.js').pipe(ngFilesort()),
gulp.src(['src/templates/**/*.html']).pipe(templateCache({ module: 'Rasparmony' }))
)
.pipe(sourcemaps.init())
.pipe(concat('app.js'))
.pipe(ngAnnotate())
.pipe(uglify())
.pipe(rename({suffix: '.min'}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(path.join(config.dest, 'js')));
});
/*===================================================================
= Watch for source changes and rebuild/reload =
===================================================================*/
gulp.task('watch', function () {
if (typeof config.server === 'object') {
gulp.watch([config.dest + '/**/*'], ['livereload']);
}
gulp.watch(['./src/html/**/*'], ['html']);
gulp.watch(['./src/less/**/*'], ['less']);
gulp.watch(['./src/js/**/*', './src/templates/**/*', config.vendor.js], ['js']);
gulp.watch(['./src/images/**/*'], ['images']);
});
/*===================================================
= Starts a Weinre Server =
===================================================*/
gulp.task('weinre', function() {
if (typeof config.weinre === 'object') {
var weinre = require('./node_modules/weinre/lib/weinre');
weinre.run(config.weinre);
} else {
throw new Error('Weinre is not configured');
}
});
/*==================================
= Copy fonts =
==================================*/
gulp.task('fonts', function() {
return gulp.src(config.vendor.fonts)
.pipe(gulp.dest(path.join(config.dest, 'fonts')));
});
/*======================================
= Build Sequence =
======================================*/
gulp.task('build', function(done) {
var tasks = ['html', 'images', 'less', 'js', 'fonts'];
seq('clean', tasks, done);
});
/*====================================
= Default Task =
====================================*/
gulp.task('default', function(done){
var tasks = [];
if (typeof config.weinre === 'object') {
tasks.push('weinre');
}
tasks.push('server');
tasks.push('watch');
seq('build', tasks, done);
});