-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
328 lines (286 loc) · 9.67 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
/*global -$ */
'use strict';
var ANGULAR_MODULE = 'app';
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var browserSync = require('browser-sync');
var modRewrite = require('connect-modrewrite');
var reload = browserSync.reload;
var seq = require('run-sequence');
var path = require('path');
var appRoot = require('app-root-path');
// process.env.npm_package_config_port is not available in gulp
var packageJson = require('./package.json');
// External tasks
// Build external app loader
// appRoot.require('lib/gulp/loader/tasks')(ANGULAR_MODULE);
gulp.task('styles', ['iconfont'], function () {
return gulp.src('app/styles/main.scss')
.pipe($.sourcemaps.init())
.pipe($.sass({
outputStyle: 'nested', // libsass doesn't support expanded yet
precision: 10,
includePaths: ['.'],
onError: console.error.bind(console, 'Sass error:')
}))
.pipe($.postcss([
require('autoprefixer')({browsers: ['last 1 version']})
]))
.pipe($.sourcemaps.write())
.pipe(gulp.dest('.tmp/styles'))
.on('end', function() {
reload();
});
});
gulp.task('jshint', function () {
return gulp.src('app/scripts/**/*.js')
.pipe(reload({stream: true, once: true}))
.pipe($.jshint())
.pipe($.jshint.reporter('jshint-stylish'))
.pipe($.if(!browserSync.active, $.jshint.reporter('fail')));
});
gulp.task('templates', function () {
return gulp.src('app/templates/**/*')
.pipe($.if('*.html', $.minifyHtml({conditionals: true, loose: true})))
.pipe($.if('*.html', $.angularTemplatecache({
root: '/templates/',
module: ANGULAR_MODULE + '.templates',
standalone: true
})))
.pipe(gulp.dest('.tmp/js'));
});
gulp.task('html', ['inject', 'templates', 'styles'], function () {
var assets = $.useref.assets({searchPath: ['.tmp', 'app', '.']});
var timestamp = Date.now();
return gulp.src('app/*.html')
.pipe(assets)
.pipe($.if('*.js', $.uglify()))
.pipe($.if('*.css', $.csso()))
.pipe(assets.restore())
.pipe($.useref())
.pipe($.if('*.html', $.minifyHtml({conditionals: true, loose: true})))
.pipe($.if('*.html', $.replace('scripts/vendor.js', 'scripts/vendor.js?' + timestamp)))
.pipe($.if('*.html', $.replace('scripts/app.js', 'scripts/app.js?' + timestamp)))
.pipe($.if('*.html', $.replace('styles/vendor.css', 'styles/vendor.css?' + timestamp)))
.pipe($.if('*.html', $.replace('styles/app.css', 'styles/app.css?' + timestamp)))
.pipe(gulp.dest('dist'));
});
gulp.task('images', function () {
return gulp.src('app/images/**/*.{jpg,png,gif,svg}')
.pipe($.imagemin({
progressive: true,
interlaced: true,
// don't remove IDs from SVGs, they are often used
// as hooks for embedding and styling
svgoPlugins: [{cleanupIDs: false}]
}))
.pipe(gulp.dest('dist/images'));
});
gulp.task('fonts', function () {
return gulp.src(require('main-bower-files')({
filter: '**/*.{eot,svg,ttf,woff,woff2}'
}).concat('app/fonts/**/*'))
.pipe(gulp.dest('.tmp/fonts'))
.pipe(gulp.dest('dist/fonts'));
});
gulp.task('iconfont', function() {
return gulp.src(['app/images/icons/*.svg'])
.pipe($.iconfont({
fontName: ANGULAR_MODULE + '-icons',
appendCodepoints: true,
centerHorizontally: true,
normalize: true,
fontHeight: 448,
descent: 64
}))
.on('glyphs', function(glyphs) {
gulp.src('app/styles/icon-font.css.tmpl')
.pipe($.consolidate('lodash', {
glyphs: glyphs,
fontName: 'AppIconFont',
fontFilename: ANGULAR_MODULE + '-icons',
fontPath: '/fonts',
className: 'ico'
}))
.pipe($.rename(ANGULAR_MODULE + '-icons.css'))
.pipe(gulp.dest('.tmp/styles'));
})
.pipe(gulp.dest('.tmp/fonts'))
.pipe(gulp.dest('dist/fonts'));
});
gulp.task('extras', function () {
return gulp.src([
'app/*.*',
'!app/*.html'
], {
dot: true
}).pipe(gulp.dest('dist'));
});
gulp.task('clean', require('del').bind(null, ['.tmp', 'dist']));
gulp.task('serve', ['env:development', 'templates', 'styles', 'fonts', 'inject'], function () {
browserSync({
notify: false,
port: 9000,
server: {
baseDir: ['.tmp', 'app'],
routes: {
'/bower_components': 'bower_components'
},
middleware: [
// Fonts and svgs are blocked by default for some reason
function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
next();
},
modRewrite([
'^/(auth/success.*)$ http://localhost:' + packageJson.config.port + '/$1 [P,L]',
'!\\.\\w+$ /index.html [L]'
])
]
}
});
// watch for changes
gulp.watch([
'app/*.html',
'app/templates/**/*.html',
'app/scripts/**/*.js',
'app/images/**/*',
'.tmp/fonts/**/*'
]).on('change', reload);
gulp.watch('app/images/icons/**/*.svg', ['iconfont']);
gulp.watch('app/styles/**/*.scss', ['styles']);
gulp.watch('app/fonts/**/*', ['fonts']);
gulp.watch('po/**/*.po', ['translations']);
gulp.watch('app/translations/**/*', ['inject']);
gulp.watch('app/templates/**/*.html', ['templates']);
gulp.watch('bower.json', ['wiredep', 'fonts']);
});
// inject bower components
gulp.task('wiredep', function () {
var wiredep = require('wiredep').stream;
gulp.src('app/styles/*.scss')
.pipe(wiredep({
ignorePath: /^(\.\.\/)+/
}))
.pipe(gulp.dest('app/styles'));
gulp.src('app/*.html')
.pipe(wiredep({
ignorePath: /^(\.\.\/)*\.\./,
exclude: [/bower_components\/bootstrap\/dist\/css/]
}))
.pipe(gulp.dest('app'));
});
// Inject file references into index.html
gulp.task('inject', ['templates', 'styles'], function () {
var injectStyles = gulp.src([
'.tmp/styles/'+'**'+'/'+'*.css',
'!.tmp/styles/vendor.css'
], { read: false });
var injectScripts = gulp.src([
'app/scripts/'+'**'+'/'+'*.js',
'app/translations/'+'**'+'/'+'*.js',
'.tmp/js/'+'**'+'/'+'*.js',
'!app/scripts/'+'**'+'/'+'*.spec.js',
'!app/scripts/'+'**'+'/'+'*.mock.js'])
.pipe($.naturalSort())
.pipe($.angularFilesort());
var injectOptions = {
ignorePath: ['app', '.tmp'],
addRootSlash: false
};
return gulp.src('app/*.html')
.pipe($.inject(injectStyles, injectOptions))
.pipe($.inject(injectScripts, injectOptions))
.pipe(gulp.dest('app'));
});
gulp.task('pot', function () {
var Po = require('pofile');
return gulp.src(['app/templates/**/*.html', 'app/scripts/**/*.js'])
.pipe($.angularGettext.extract('Terms.pot', {
postProcess: function (catalog) {
// Inject some convenience keys that must be present in the
// translations
var localeName = new Po.Item();
localeName.msgid = 'locale.name';
localeName.comments = [ 'The display name of this locale' ];
var rtl = new Po.Item();
rtl.msgid = 'locale.direction';
rtl.comments = [ 'Locale is RTL or LTR? Enter only \'rtl\' or \'ltr\'' ];
catalog.items.unshift(localeName, rtl);
catalog.items.forEach(function (item) {
item.references = item.references.map(function (ref) {
return path.join(path.dirname(ref), path.basename(ref))
.replace(/\\/g, '/'); // replace any Windows-style paths
});
});
}
}))
.pipe(gulp.dest('po/'));
});
gulp.task('translations', function () {
return gulp.src('po/**/*.po')
.pipe($.angularGettext.compile())
.pipe(gulp.dest('app/translations/'));
});
// ENV SPECIFIC FUNCTIONS
function configTask(env) {
return gulp.src('config/' + env + '.json')
.pipe($.ngConfig(ANGULAR_MODULE + '.config', {
wrap: '// THIS FILE IS AUTO GENERATED BY GULP. DO NOT EDIT.\n' +
'(function () {\n' +
' \'use strict\';\n' +
' /*jshint ignore:start*/\n' +
' return <%= module %>' +
' /*jshint ignore:end*/\n' +
'})();\n'
}))
.pipe($.rename('config.js'))
.pipe(gulp.dest('app/scripts/services'));
}
function robotsTask(env) {
// we don't even have a dist in dev mode
if(env === 'development') {
return;
}
var robotsContent = (function() {
switch(env) {
case 'production':
default:
return '';
case 'stage':
return 'User-agent: *\nDisallow: /';
}
})();
return $.file('robots.txt', robotsContent)
.pipe(gulp.dest('dist'));
}
// ENV TASKS
gulp.task('config:production', configTask.bind(null, 'production'));
gulp.task('config:stage', configTask.bind(null, 'stage'));
gulp.task('config:development', configTask.bind(null, 'development'));
gulp.task('robots:production', robotsTask.bind(null, 'production'));
gulp.task('robots:stage', robotsTask.bind(null, 'stage'));
// Make sure clean runs first and everything is handled by gulp api
gulp.task('env:production', ['clean'], function() {
return gulp.start('config:production');
});
gulp.task('env:stage', ['clean'], function() {
return gulp.start('config:stage');
});
gulp.task('env:development', function() {
return gulp.start('config:development');
});
// BUILD TASKS
gulp.task('build', ['jshint', 'html', 'images', 'fonts', 'extras'], function () {
return gulp.src('dist/**/*').pipe($.size({title: 'build', gzip: true}));
});
gulp.task('build:stage', ['clean', 'env:stage'], function() {
return seq('build', [/*'sitemap:stage', */'robots:stage']);
});
gulp.task('build:production', ['clean', 'env:production'], function() {
return seq('build', [/*'sitemap:production', */'robots:production']);
});
// Quick alias for production
gulp.task('default', function () {
return gulp.start('build:production');
});