forked from googleanalytics/ga-dev-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
158 lines (135 loc) · 4.97 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
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
require('babel/register');
var babelify = require('babelify');
var browserify = require('browserify');
var buffer = require('vinyl-buffer');
var cleancss = require('gulp-cleancss');
var concat = require('gulp-concat');
var DeepWatch = require('deep-watch');
var gulp = require('gulp');
var gulpIf = require('gulp-if');
var gutil = require('gulp-util');
var inline = require('rework-plugin-inline');
var jshint = require('gulp-jshint');
var mocha = require('gulp-mocha');
var plumber = require('gulp-plumber');
var prefix = require('gulp-autoprefixer');
var rework = require('gulp-rework');
var source = require('vinyl-source-stream');
var sourcemaps = require('gulp-sourcemaps');
var suit = require('rework-suit');
var uglify = require('gulp-uglify');
function isProd(transform) {
return gulpIf(process.env.NODE_ENV == 'production', transform);
}
function streamError(err) {
gutil.beep();
gutil.log(err instanceof gutil.PluginError ? err.toString() : err.stack);
}
gulp.task('css', function() {
gulp.src('./src/css/main.css')
.pipe(plumber({errorHandler: streamError}))
.pipe(rework(suit(), inline('./src/images'), {sourcemap: true}))
.pipe(prefix('> 1%', 'last 2 versions', 'Safari >= 5.1',
'ie >= 10', 'Firefox ESR'))
.pipe(isProd(cleancss({keepSpecialComments: 0})))
.pipe(gulp.dest('./public/css'));
});
gulp.task('images', function() {
gulp.src('./src/images/**/*')
.pipe(gulp.dest('./public/images'));
});
gulp.task('lint', function() {
return gulp.src([
'./gulpfile.js',
'./src/javascript/**/*.js'
])
.pipe(jshint())
.pipe(jshint.reporter('default'))
.pipe(jshint.reporter('fail'));
});
gulp.task('javascript:bundle', function() {
browserify('./src/javascript', {debug: true})
.transform(babelify)
.bundle()
.on('error', streamError)
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(isProd(uglify()))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./public/javascript/'));
});
gulp.task('javascript:vendor', function() {
gulp.src([
'node_modules/jquery/dist/jquery.min.js',
'node_modules/jquery/dist/jquery.min.map',
'node_modules/moment/min/moment.min.js',
'node_modules/chart.js/Chart.min.js',
'node_modules/Select2/select2.js'
])
.pipe(gulp.dest('./public/javascript'));
});
gulp.task('javascript:build', function() {
gulp.src([
'./src/javascript/embed-api/components/active-users.js',
'./src/javascript/embed-api/components/date-range-selector.js'
])
.pipe(plumber({errorHandler: streamError}))
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./public/javascript/embed-api/components'))
.pipe(gulp.dest('./build/javascript/embed-api/components'));
browserify('./src/javascript/embed-api/components/view-selector2', {
debug: true
})
.bundle()
.on('error', streamError)
.pipe(source('view-selector2.js'))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(uglify())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./public/javascript/embed-api/components'))
.pipe(gulp.dest('./build/javascript/embed-api/components'));
});
gulp.task('javascript', [
'javascript:bundle',
'javascript:build',
'javascript:vendor'
]);
gulp.task('test', function() {
return gulp.src('test/**/*.js', {read: false})
.pipe(mocha());
});
gulp.task('watch', ['javascript', 'css', 'images'], function() {
// gulp.watch('./src/css/**/*.css', ['css']);
// gulp.watch('./src/images/**/*', ['images']);
// gulp.watch('./src/javascript/**/*.js', ['javascript']);
// This is a temporary workaround until this `gulp.watch` is fixed:
// https://github.com/babel/babel/issues/489#issuecomment-69919417
var watchOptions = {
exclude: ['lib', 'node_modules', 'public', 'templates', 'test']
};
function onChange(event, filename) {
if (filename.indexOf('src/css') === 0) gulp.start('css');
if (filename.indexOf('src/images') === 0) gulp.start('images');
if (filename.indexOf('src/javascript') === 0) gulp.start('javascript');
}
new DeepWatch('.', watchOptions, onChange).start();
});
// Disable JSHint since it doesn't handle JSX syntax at the moment.
gulp.task('build', [/*'lint',*/ 'test', 'javascript', 'css', 'images']);