forked from datanews/tik-tok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
164 lines (150 loc) · 4.63 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
/**
* Gulp file for the Tik Tok project. This handles tasks like building,
* linting, and other helpful development things.
*/
'use strict';
// Dependencies
var fs = require('fs');
var path = require('path');
var gulp = require('gulp');
var jshint = require('gulp-jshint');
var jscs = require('gulp-jscs');
var plumber = require('gulp-plumber');
var replace = require('gulp-replace');
var util = require('gulp-util');
var header = require('gulp-header');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var less = require('gulp-less');
var recess = require('gulp-recess');
var cssminify = require('gulp-minify-css');
var autoprefixer = require('gulp-autoprefixer');
var webserver = require('gulp-webserver');
var mocha = require('gulp-mocha');
var karma = require('karma').server;
// Create banner to insert into files
var pkg = require('./package.json');
var banner = ['/**',
' * <%= pkg.name %> - <%= pkg.description %>',
' * @version v<%= pkg.version %>',
' * @link <%= pkg.homepage %>',
' * @license <%= pkg.license %>',
' */',
''].join('\n');
// Browser support (for autoprefixer). This should be more defined
// for the project as a whole
var supportedBrowsers = ['> 1%', 'last 2 versions', 'Firefox ESR', 'Opera 12.1'];
// Plumber allows for better error handling and makes it so that
// gulp doesn't crash so hard. Good for watching and linting tasks
var plumberHandler = function(error) {
if (error) {
util.beep();
}
else {
this.emit('end');
}
};
// Support JS is a task to look at the supporting JS, like this
// file
gulp.task('support-js', function() {
return gulp.src(['gulpfile.js', 'tests/**/*.js'])
.pipe(plumber(plumberHandler))
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(jshint.reporter('fail'))
.pipe(jscs());
});
// Run tests through Node. This is quicker, and used for immediate
// development
gulp.task('test', ['js', 'support-js'], function() {
return gulp.src('tests/**/*.js', { read: false })
.pipe(plumber(plumberHandler))
.pipe(mocha({
ui: 'bdd',
reporter: 'spec'
}));
});
// Run tests in browsers
gulp.task('browser-test', ['js', 'support-js'], function(done) {
return karma.start({
configFile: path.join(__dirname + '/tests/karma.conf.js'),
singleRun: true
}, done);
});
// Main JS task. Takes in files from src and outputs
// to dist. Gets template and uses JSHint, JSCS, add header, minify
gulp.task('js', function() {
return gulp.src('src/**/*.js')
.pipe(plumber(plumberHandler))
.pipe(replace(
'REPLACE-DEFAULT-TEMPLATE',
fs.readFileSync('src/tik-tok.tpl.html', {
encoding: 'utf-8'
}).replace(/'/g, '\\\'').replace(/(\r\n|\n|\r|\s+)/g, ' ')
))
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(jshint.reporter('fail'))
.pipe(jscs())
.pipe(header(banner, { pkg: pkg }))
.pipe(gulp.dest('dist'))
.pipe(uglify())
.pipe(header(banner, { pkg: pkg }))
.pipe(rename({
extname: '.min.js'
}))
.pipe(gulp.dest('dist'));
});
// Styles. Recess linting, Convert LESS to CSS, minify
gulp.task('styles', function() {
return gulp.src('src/**/*.less')
.pipe(plumber(plumberHandler))
.pipe(recess({
noOverqualifying: false,
noUniversalSelectors: false
}))
.pipe(less())
.pipe(recess.reporter({
fail: true
}))
.pipe(autoprefixer({
browsers: supportedBrowsers
}))
.pipe(header(banner, { pkg: pkg }))
.pipe(gulp.dest('dist'))
.pipe(cssminify())
.pipe(header(banner, { pkg: pkg }))
.pipe(rename({
extname: '.min.css'
}))
.pipe(gulp.dest('dist'));
});
// Watch for files that need to be processed
gulp.task('watch', function() {
gulp.watch(['gulpfile.js', 'tests/**/*.js'], ['support-js']);
gulp.watch(['src/**/*.js', 'src/**/*.html'], ['js', 'test']);
gulp.watch(['tests/**/*.js'], ['test']);
gulp.watch('src/**/*.less', ['styles']);
});
// Web server for conveinence
gulp.task('webserver', function() {
return gulp.src('./')
.pipe(webserver({
port: 8089,
livereload: {
enable: true,
filter: function(file) {
// Only watch dist and examples
return (file.match(/dist|examples|index\.html/)) ? true : false;
}
},
directoryListing: true,
open: true
}));
});
// Default task is a basic build
gulp.task('default', ['support-js', 'js', 'test', 'browser-test', 'styles']);
// No browser test build
gulp.task('no-brower-build', ['support-js', 'js', 'test', 'styles']);
// Combine webserver and watch tasks for a more complete server
gulp.task('server', ['no-brower-build', 'watch', 'webserver']);