-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
329 lines (272 loc) · 8.68 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
'use strict';
var appName = 'OnMars';
var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();
var del = require('del');
var beep = require('beepbeep');
var express = require('express');
var path = require('path');
var open = require('open');
var stylish = require('jshint-stylish');
var connectLr = require('connect-livereload');
var streamqueue = require('streamqueue');
var runSequence = require('run-sequence');
var merge = require('merge-stream');
var ripple = require('ripple-emulator');
var wiredep = require('wiredep');
var gulpUtil = require('gulp-util');
/**
* Parse arguments
*/
var args = require('yargs')
.alias('e', 'emulate')
.alias('b', 'build')
.alias('r', 'run')
// remove all debug messages (console.logs, alerts etc) from release build
.alias('release', 'strip-debug')
.default('build', false)
.default('port', 9000)
.default('strip-debug', false)
.argv;
var build = !!(args.build || args.emulate || args.run);
var emulate = args.emulate;
var run = args.run;
var port = args.port;
var stripDebug = !!args.stripDebug;
var targetDir = path.resolve(build ? 'www' : '.tmp');
// if we just use emualate or run without specifying platform, we assume iOS
// in this case the value returned from yargs would just be true
if (emulate === true) {
emulate = 'ios';
}
if (run === true) {
run = 'ios';
}
// global error handler
var errorHandler = function(error) {
if (build) {
throw error;
} else {
beep(2, 170);
plugins.util.log(error);
}
};
// clean target dir
gulp.task('clean', function(done) {
return del([targetDir], done);
});
// precompile .scss and concat with ionic.css
gulp.task('styles', function() {
var options = build ? { style: 'compressed' } : { style: 'expanded' };
var sassStream = gulp.src('app/styles/main.scss')
.pipe(plugins.sass(options))
.on('error', function(err) {
console.log('err: ', err);
beep();
});
// build ionic css dynamically to support custom themes
var ionicStream = gulp.src('app/styles/ionic-styles.scss')
.pipe(plugins.cached('ionic-styles'))
.pipe(plugins.sass(options))
// cache and remember ionic .scss in order to cut down re-compile time
.pipe(plugins.remember('ionic-styles'))
.on('error', function(err) {
console.log('err: ', err);
beep();
});
return streamqueue({ objectMode: true }, ionicStream, sassStream)
.pipe(plugins.autoprefixer('last 1 Chrome version', 'last 3 iOS versions', 'last 3 Android versions'))
.pipe(plugins.concat('main.css'))
.pipe(plugins.if(build, plugins.stripCssComments()))
.pipe(plugins.if(build && !emulate, plugins.rev()))
.pipe(gulp.dest(path.join(targetDir, 'styles')))
.on('error', errorHandler);
});
// build templatecache, copy scripts.
// if build: concat, minsafe, uglify and versionize
gulp.task('scripts', function() {
var dest = path.join(targetDir, 'scripts');
var minifyConfig = {
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeComments: true
};
// prepare angular template cache from html templates
// (remember to change appName var to desired module name)
var templateStream = gulp
.src('**/*.html', { cwd: 'app/templates'})
.pipe(plugins.angularTemplatecache('templates.js', {
root: 'templates/',
module: appName,
htmlmin: build && minifyConfig
}));
var scriptStream = gulp
.src(['templates.js', 'app.js', '**/*.js'], { cwd: 'app/scripts' })
.pipe(plugins.if(!build, plugins.changed(dest)));
return streamqueue({ objectMode: true }, scriptStream, templateStream)
.pipe(plugins.if(build, plugins.ngAnnotate()))
.pipe(plugins.if(stripDebug, plugins.stripDebug()))
.pipe(plugins.if(build, plugins.concat('app.js')))
// .pipe(plugins.if(build, plugins.uglify().on('error', gulpUtil.log)))
.pipe(plugins.if(build && !emulate, plugins.rev()))
.pipe(gulp.dest(dest))
.on('error', errorHandler);
});
// copy fonts
gulp.task('fonts', function() {
return gulp
.src(['app/fonts/*.*', 'bower_components/ionic/release/fonts/*.*'])
.pipe(gulp.dest(path.join(targetDir, 'fonts')))
.on('error', errorHandler);
});
// generate iconfont
gulp.task('iconfont', function(){
return gulp.src('app/icons/*.svg', {
buffer: false
})
.pipe(plugins.iconfontCss({
fontName: 'ownIconFont',
path: 'app/icons/own-icons-template.css',
targetPath: '../styles/own-icons.css',
fontPath: '../fonts/'
}))
.pipe(plugins.iconfont({
fontName: 'ownIconFont'
}))
.pipe(gulp.dest(path.join(targetDir, 'fonts')))
.on('error', errorHandler);
});
// copy images
gulp.task('images', function() {
return gulp.src('app/images/**/*.*')
.pipe(gulp.dest(path.join(targetDir, 'images')))
.on('error', errorHandler);
});
// lint js sources based on .jshintrc ruleset
gulp.task('jsHint', function(done) {
return gulp
.src('app/scripts/**/*.js')
.pipe(plugins.jshint())
.pipe(plugins.jshint.reporter(stylish))
.on('error', errorHandler);
done();
});
// concatenate and minify vendor sources
gulp.task('vendor', function() {
var vendorFiles = wiredep().js;
return gulp.src(vendorFiles)
.pipe(plugins.concat('vendor.js'))
// .pipe(plugins.if(build, plugins.uglify()))
.pipe(plugins.if(build, plugins.rev()))
.pipe(gulp.dest(targetDir))
.on('error', errorHandler);
});
// inject the files in index.html
gulp.task('index', ['jsHint', 'scripts'], function() {
// build has a '-versionnumber' suffix
var cssNaming = 'styles/main*';
// injects 'src' into index.html at position 'tag'
var _inject = function(src, tag) {
return plugins.inject(src, {
starttag: '<!-- inject:' + tag + ':{{ext}} -->',
read: false,
addRootSlash: false
});
};
// get all our javascript sources
// in development mode, it's better to add each file seperately.
// it makes debugging easier.
var _getAllScriptSources = function() {
var scriptStream = gulp.src(['scripts/app.js', 'scripts/**/*.js'], { cwd: targetDir });
return streamqueue({ objectMode: true }, scriptStream);
};
return gulp.src('app/index.html')
// inject css
.pipe(_inject(gulp.src(cssNaming, { cwd: targetDir }), 'app-styles'))
// inject vendor.js
.pipe(_inject(gulp.src('vendor*.js', { cwd: targetDir }), 'vendor'))
// inject app.js (build) or all js files indivually (dev)
.pipe(plugins.if(build,
_inject(gulp.src('scripts/app*.js', { cwd: targetDir }), 'app'),
_inject(_getAllScriptSources(), 'app')
))
.pipe(gulp.dest(targetDir))
.on('error', errorHandler);
});
// start local express server
gulp.task('serve', function() {
express()
.use(!build ? connectLr() : function(){})
.use(express.static(targetDir))
.listen(port);
open('http://localhost:' + port + '/');
});
// ionic emulate wrapper
gulp.task('ionic:emulate', plugins.shell.task([
'ionic emulate ' + emulate + ' --livereload --consolelogs'
]));
// ionic run wrapper
gulp.task('ionic:run', plugins.shell.task([
'ionic run ' + run
]));
// ionic resources wrapper
gulp.task('icon', plugins.shell.task([
'ionic resources --icon'
]));
gulp.task('splash', plugins.shell.task([
'ionic resources --splash'
]));
gulp.task('resources', plugins.shell.task([
'ionic resources'
]));
// select emulator device
gulp.task('select', plugins.shell.task([
'./helpers/emulateios'
]));
// ripple emulator
gulp.task('ripple', ['scripts', 'styles', 'watchers'], function() {
var options = {
keepAlive: false,
open: true,
port: 4400
};
// Start the ripple server
ripple.emulate.start(options);
open('http://localhost:' + options.port + '?enableripple=true');
});
// start watchers
gulp.task('watchers', function() {
plugins.livereload.listen();
gulp.watch('app/styles/**/*.scss', ['styles']);
gulp.watch('app/fonts/**', ['fonts']);
gulp.watch('app/icons/**', ['iconfont']);
gulp.watch('app/images/**', ['images']);
gulp.watch('app/scripts/**/*.js', ['index']);
gulp.watch('./bower.json', ['vendor']);
gulp.watch('app/templates/**/*.html', ['index']);
gulp.watch('app/index.html', ['index']);
gulp.watch(targetDir + '/**')
.on('change', plugins.livereload.changed)
.on('error', errorHandler);
});
// no-op = empty function
gulp.task('noop', function() {});
// our main sequence, with some conditional jobs depending on params
gulp.task('default', function(done) {
runSequence(
'clean',
'iconfont',
[
'fonts',
'styles',
'images',
'vendor'
],
'index',
build ? 'noop' : 'watchers',
build ? 'noop' : 'serve',
emulate ? ['ionic:emulate', 'watchers'] : 'noop',
run ? 'ionic:run' : 'noop',
done);
});