-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
369 lines (311 loc) · 11.6 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* jshint strict: false */
var gulp = require('gulp'),
map = require('map-stream'),
del = require('del'),
plugins = require('gulp-load-plugins')(),
server = require('tiny-lr')(),
config = require('./config.json'),
pkg = require('./package.json');
// Prepare CSS
// ===========
var processWinPath = function (file) {
// Fix for bug with paths on Windows
var path = require('path');
if (process.platform === 'win32') {
file.path = path.relative('.', file.path);
file.path = file.path.replace(/\\/g, '/');
}
};
// Compile SASS and add prefixes
var fnSass = function (path) {
return gulp.src(path)
.on('data', processWinPath)
.pipe(plugins.plumber())
.pipe(plugins.sourcemaps.init())
.pipe(plugins.sass())
.pipe(plugins.sourcemaps.write())
.on('error', function (err) {
console.log(err.message);
// process.exit(1);
})
.pipe(plugins.size({ showFiles: true, title: '[CSS]' }))
.pipe(gulp.dest(config.build + '/assets'))
.on('end', function () {
require('fs').unlink(path);
});
};
gulp.task('styles:sass:imports', function () {
var files = [config.app + '/+(sass|app|common)/**/*.scss', '!' + config.app + '/sass/includes/*.scss', '!' + config.app + '/+(app|common)/**/_*.scss'];
return gulp.src(files, { read: false })
.on('data', processWinPath)
.pipe(plugins.intercept(function (file) {
file.contents = new Buffer('@import \'' + file.path + '\';');
return file;
}))
.pipe(plugins.concat(pkg.name + '-' + pkg.version + '.scss'))
.pipe(gulp.dest(config.build + '/assets'));
});
gulp.task('styles:sass', ['styles:sass:imports'], function () {
var files = config.build + '/assets/' + pkg.name + '-' + pkg.version + '.scss';
return fnSass(files);
});
// Minify CSS
gulp.task('styles', ['styles:sass', 'vendor:css'], function () {
var arr = (config.vendor_files.css).concat(config.build + '/assets/' + pkg.name + '-' + pkg.version + '.css');
return gulp.src(arr)
.pipe(plugins.concat(pkg.name + '-' + pkg.version + '.css'))
.pipe(plugins.autoprefixer({
browsers: ['last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4']
}))
.pipe(plugins.minifyCss({ keepSpecialComments: 0 }))
.pipe(plugins.rename({ suffix: '.min' }))
.pipe(plugins.size({ showFiles: true, title: '[CSS]' }))
.pipe(gulp.dest(config.dist + '/assets'));
});
// Prepare vendor files
// ====================
// Copy vendor JS files to /build/
gulp.task('vendor:js', function () {
if (!config.vendor_files.js.length) {
return;
}
return gulp.src(config.vendor_files.js, { base: '.' })
.pipe(gulp.dest(config.build));
});
// Copy vendor css to /build/
gulp.task('vendor:css', function () {
if (!config.vendor_files.css.length) {
return;
}
return gulp.src(config.vendor_files.css, { base: '.' })
.pipe(gulp.dest(config.build));
});
// Copy vendor assets to /build/
gulp.task('vendor:assets', function () {
if (!config.vendor_files.assets.length) {
return;
}
return gulp.src(config.vendor_files.assets, { base: '.' })
.pipe(gulp.dest(config.build + '/assets'));
});
// Prepare JavaScript
// ==================
// Cache AngularJS templates
var fnCacheTpls = function (path) {
return gulp.src(path)
.pipe(plugins.minifyHtml({
empty: true,
spare: true,
quotes: true
}))
.pipe(plugins.angularTemplatecache({
module: 'templates.app',
standalone: true
}))
.pipe(plugins.concat('templates.js'))
.pipe(gulp.dest(config.build + '/app'));
};
gulp.task('scripts:cacheTpls', function () {
return fnCacheTpls(config.paths.templates);
});
// Check JavaScript code quality with JSHint
var fnLint = function (path, exitOnError) {
return gulp.src(path, { base: config.app })
.pipe(plugins.plumber())
.pipe(plugins.jshint())
.pipe(plugins.jshint.reporter('jshint-stylish-ex'))
.pipe(map(function (file, cb) {
if (!file.jshint.success && exitOnError) {
process.exit(1);
}
cb(null, file);
}))
.pipe(gulp.dest(config.build));
};
gulp.task('scripts:lint', function () {
return fnLint(config.paths.scripts, true);
});
// Perform final maintenance
gulp.task('scripts:tidy', ['scripts:lint', 'scripts:cacheTpls'], function () {
var files = config.paths.scripts.concat(config.build + '/app/templates.js');
return gulp.src(files)
.pipe(plugins.ngAnnotate())
.pipe(plugins.concatUtil(pkg.name + '-' + pkg.version + '.js', {
process: function (src) {
return (src.trim() + '\n').replace(/(^|\n)[ \t]*('use strict'|"use strict");?\s*/g, '$1');
}
}))
.pipe(plugins.concatUtil.header('(function(window, document, undefined) {\n\'use strict\';\n'))
.pipe(plugins.concatUtil.footer('\n})(window, document);\n'))
.pipe(gulp.dest(config.dist + '/assets'));
});
// Concat and minify JavaScript
gulp.task('scripts', ['scripts:tidy', 'vendor:js'], function () {
var arr = (config.vendor_files.js).concat(config.dist + '/assets/' + pkg.name + '-' + pkg.version + '.js');
return gulp.src(arr)
.pipe(plugins.concat(pkg.name + '-' + pkg.version + '.js'))
.pipe(plugins.size({ showFiles: true, title: '[JS]' }))
.pipe(plugins.uglify({
mangle: false,
compress: {
drop_console: true
}
}))
.pipe(plugins.rename({ suffix: '.min' }))
.pipe(plugins.size({ showFiles: true, title: '[JS]' }))
.pipe(gulp.dest(config.dist + '/assets'))
.on('end', function () {
del(config.dist + '/assets/' + pkg.name + '-' + pkg.version + '.js', { force: true });
});
});
// Prepare assets
// ==============
// Copy assets
var fnImg = function (path) {
return gulp.src(path, { base: config.app })
.pipe(gulp.dest(config.build));
};
gulp.task('assets:img', function () {
return fnImg(config.paths.assets);
});
// Compress images
gulp.task('assets', ['assets:img', 'vendor:assets'], function () {
return gulp.src([config.build + '/assets/**', '!' + config.build + '/assets/*.+(css|scss)'])
.pipe(plugins.plumber())
// .pipe(plugins.bytediff.start())
.pipe(plugins.newer(config.dist + '/assets'))
// .pipe(plugins.imagemin({ optimizationLevel: 7, progressive: true, interlaced: true }))
// .pipe(plugins.bytediff.stop())
.pipe(gulp.dest(config.dist + '/assets'));
});
// Prepare HTML
// ============
// Inject CSS & JS to index.html source
var fnInject = function (path) {
var inject = {
css : (config.vendor_files.css).concat(config.build + '/assets/*.css'),
js : (config.vendor_files.js).concat(config.build + '/+(app|common)/**/*.module.js').concat(config.build + '/+(app|common)/**/*.js')
};
var sources = gulp.src(inject.css.concat(inject.js), { read: false });
return gulp.src(path)
.pipe(plugins.inject(sources, {
addRootSlash: false,
ignorePath: ['/', config.build + '/']
}))
.pipe(gulp.dest(config.build));
};
gulp.task('html:inject', ['styles:sass', 'scripts:lint', 'scripts:cacheTpls'], function () {
return fnInject(config.paths.html);
});
// Replace non-optimized HTML blocks
gulp.task('html:replace', ['html:inject'], function () {
return gulp.src(config.build + '/index.html')
.pipe(plugins.htmlReplace({
css: 'assets/' + pkg.name + '-' + pkg.version + '.min.css',
js: 'assets/' + pkg.name + '-' + pkg.version + '.min.js'
}))
.pipe(gulp.dest(config.dist));
});
// Compile and minify HTML
gulp.task('html', ['html:replace'], function () {
return gulp.src(config.dist + '/index.html')
.pipe(plugins.plumber())
.pipe(plugins.minifyHtml({
empty: true,
spare: true,
quotes: true
}))
.pipe(gulp.dest(config.dist));
});
// Karma
// ============
var testFiles = [
config.build + '/vendor/**/*.js',
config.build + '/+(app|common)/**/*.module.js',
config.build + '/+(app|common)/**/*.js',
config.mocks,
config.paths.tests
];
gulp.task('test:run', ['scripts:lint', 'scripts:cacheTpls', 'vendor:assets', 'styles:sass', 'html:inject'] , function () {
// Be sure to return the stream
return gulp.src(testFiles)
.pipe(plugins.karma({
configFile: 'karma.conf.js',
action: 'run'
}))
.on('error', function (err) {
process.exit(1);
});
});
gulp.task('test:watch', ['scripts:lint', 'scripts:cacheTpls', 'vendor:assets', 'styles:sass', 'html:inject'], function () {
gulp.src(testFiles)
.pipe(plugins.karma({
configFile: 'karma.conf.js',
action: 'watch'
}));
});
// Set up Watch
// ============
// Add files to Watch
gulp.task('watch', ['styles:sass', 'scripts:lint', 'scripts:cacheTpls', 'assets:img', 'vendor:css', 'vendor:js', 'vendor:assets', 'test:watch', 'html:inject'], function () {
require('./server.js')(server);
// watch for JS changes
gulp.watch(config.paths.scripts, function (event) {
if (event.path.lastIndexOf('.js') === event.path.length - 3) {
if (event.type === 'deleted') {
del(event.path.replace(config.app, config.build));
} else {
return fnLint(event.path).pipe(plugins.livereload(server));
}
}
});
// remove deleted JS files from index.html
gulp.watch(config.build + '/+(app|common)/**/*.js', function (event) {
if (event.type !== 'changed') {
return fnInject(config.paths.html).pipe(plugins.livereload(server));
}
});
// watch AngularJS templates to cache
gulp.watch(config.app + '/+(app|common)/**', function (event) {
if (event.path.lastIndexOf('.tpl.html') === event.path.length - 9) {
return fnCacheTpls(config.paths.templates).pipe(plugins.livereload(server));
}
});
// watch for SASS changes
var runSequence = require('run-sequence');
gulp.watch(config.paths.sass, function (event) {
runSequence('styles:sass:imports', function () {
var files = config.build + '/assets/' + pkg.name + '-' + pkg.version + '.scss';
return fnSass(files).pipe(plugins.livereload(server));
});
});
gulp.watch(config.paths.assets, function (event) {
if (event.type === 'deleted') {
del(event.path.replace(config.app, config.build));
} else {
return fnImg(event.path).pipe(plugins.livereload(server));
}
});
gulp.watch(config.paths.html, function (event) {
return fnInject(event.path).pipe(plugins.livereload(server));
});
});
// Clean up development & production directories
// =============================================
gulp.task('clean:build', function (cb) {
del(config.build, cb);
});
gulp.task('clean:dist', function (cb) {
del(config.dist, { force: true }, cb);
});
// Main gulp tasks
// ===============
gulp.task('build', ['clean:build'], function () {
gulp.start('styles:sass', 'scripts:lint', 'scripts:cacheTpls', 'vendor:css', 'vendor:js', 'vendor:assets', 'test:run', 'assets:img', 'html:inject');
});
gulp.task('compile', ['clean:dist', 'build'], function () {
gulp.start('styles', 'scripts', 'assets', 'html');
});
gulp.task('default', function () {
gulp.start('compile');
});