This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
forked from googleanalytics/ga-dev-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
293 lines (239 loc) · 7.34 KB
/
gulpfile.babel.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
// Copyright 2016 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.
import {spawn} from 'child_process';
import createOutputStream from 'create-output-stream';
import del from 'del';
import fse from 'fs-extra';
import glob from 'glob';
import gulp from 'gulp';
import prefix from 'gulp-autoprefixer';
import concat from 'gulp-concat';
import cssnext from 'gulp-cssnext';
import eslint from 'gulp-eslint';
import gulpIf from 'gulp-if';
import resize from 'gulp-image-resize';
import imagemin from 'gulp-imagemin';
import mocha from 'gulp-mocha';
import plumber from 'gulp-plumber';
import rename from 'gulp-rename';
import sourcemaps from 'gulp-sourcemaps';
import uglify from 'gulp-uglify';
import gutil from 'gulp-util';
import pngquant from 'imagemin-pngquant';
import merge from 'merge-stream';
import path from 'path';
import request from 'request';
import webpack from 'webpack';
let devServer;
function isProd() {
return process.env.NODE_ENV == 'production';
}
function streamError(err) {
gutil.beep();
gutil.log(err instanceof gutil.PluginError ? err.toString() : err.stack);
}
gulp.task('css', function() {
let opts = {
browsers: '> 1%, last 2 versions, Safari > 5, ie > 9, Firefox ESR',
compress: isProd(),
url: {url: 'inline'}
}
return merge(
gulp.src('./src/css/index.css')
.pipe(plumber({errorHandler: streamError}))
.pipe(cssnext(opts))
.pipe(gulp.dest('public/css')),
gulp.src('./src/css/chartjs-visualizations.css')
.pipe(plumber({errorHandler: streamError}))
.pipe(cssnext(opts))
.pipe(gulp.dest('public/css'))
);
});
gulp.task('images', function() {
return merge(
gulp.src('src/images/**/*.svg')
.pipe(gulp.dest('public/images')),
gulp.src('src/images/**/*.png')
.pipe(imagemin({use: [pngquant()]}))
.pipe(gulp.dest('public/images')),
gulp.src('src/images/**/*.png')
.pipe(resize({width : '50%'}))
.pipe(imagemin({use: [pngquant()]}))
.pipe(rename((p) => p.basename = p.basename.replace('-2x', '')))
.pipe(gulp.dest('public/images'))
);
});
gulp.task('javascript:bundle', ['javascript:webpack'], function(done) {
gulp.src([
'public/javascript/common.js',
'public/javascript/index.js'
])
.pipe(sourcemaps.init())
.pipe(concat('app.js'))
.pipe(gulpIf(isProd(), uglify()))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('public/javascript'))
.on('end', function() {
del([
'public/javascript/common*',
'public/javascript/index*'
])
.then(() => done(), (err) => done(err));
});
});
gulp.task('javascript:webpack', (function() {
let compiler;
function createCompiler() {
let sourceFiles = glob.sync('./*/index.js', {cwd: './src/javascript/'})
let entry = {index: ['babel-polyfill', './src/javascript/index.js']};
for (let filename of sourceFiles) {
let name = path.join('.', path.dirname(filename));
let filepath = './' + path.join('./src/javascript', filename);
entry[name] = ['babel-polyfill', filepath];
}
let plugins = [new webpack.optimize.CommonsChunkPlugin({
name: 'common',
minChunks: 2
})];
// Uglify and remove dev-only code in production.
if (isProd()) {
plugins.push(new webpack.optimize.UglifyJsPlugin());
plugins.push(new webpack.DefinePlugin({
'process.env': {NODE_ENV: '"production"'}
}));
}
return webpack({
entry: entry,
output: {
path: 'public/javascript',
filename: '[name].js'
},
cache: {},
devtool: '#source-map',
plugins: plugins,
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel',
query: {
cacheDirectory: true,
}
}
]
}
});
}
function compile(done) {
(compiler || (compiler = createCompiler())).run(function(err, stats) {
if (err) throw new gutil.PluginError('webpack', err);
gutil.log('[webpack]', stats.toString('minimal'));
done();
});
}
return compile;
}()));
gulp.task('javascript:embed-api-components', (function() {
let compiler;
function createCompiler() {
const COMPONENT_PATH = './javascript/embed-api/components';
let components = ['active-users', 'date-range-selector', 'view-selector2'];
let entry = {};
for (let component of components) {
entry[component] = './' + path.join('./src', COMPONENT_PATH, component);
}
return webpack({
entry: entry,
output: {
path: path.join('./public', COMPONENT_PATH),
filename: '[name].js'
},
cache: {},
devtool: '#source-map',
plugins: isProd() ? [new webpack.optimize.UglifyJsPlugin()] : [],
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}
]
}
});
}
function compile(done) {
(compiler || (compiler = createCompiler())).run(function(err, stats) {
if (err) throw new gutil.PluginError('webpack', err);
gutil.log('[webpack]', stats.toString('minimal'));
done();
});
}
return compile;
}()));
gulp.task('javascript', [
'javascript:bundle',
'javascript:embed-api-components'
]);
gulp.task('json', function() {
const PARAMETER_REFERENCE_URL =
'https://developers.google.com/analytics' +
'/devguides/collection/protocol/v1/parameters.json';
request(PARAMETER_REFERENCE_URL)
.pipe(createOutputStream('public/json/parameter-reference.json'));
});
gulp.task('lint', function () {
return gulp.src(['src/javascript/**/*.js'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task('test', function() {
return gulp.src('test/**/*.js', {read: false})
.pipe(mocha());
});
gulp.task('serve', [], function(done) {
devServer = spawn('dev_appserver.py', ['.']);
devServer.stderr.on('data', (data) => {
if (data.includes('Starting module')) done();
process.stdout.write(data);
});
});
gulp.task('watch', ['build:core', 'serve'], function() {
gulp.watch('src/css/**/*.css', ['css']);
gulp.watch('src/images/**/*', ['images']);
gulp.watch('src/javascript/**/*', ['javascript']);
});
gulp.task('build:embed-api-components', ['javascript'], function() {
gulp.src('public/javascript/embed-api/components/*')
.pipe(gulp.dest('build/javascript/embed-api/components'));
});
gulp.task('build:core', [
'javascript',
'css',
'images',
'json',
'build:embed-api-components'
])
gulp.task('build:all', [
'lint',
'test',
'build:core'
]);
gulp.task('build', function() {
// Force production mode if NODE_ENV isn't set.
if (!('NODE_ENV' in process.env)) process.env.NODE_ENV = 'production';
gulp.start('build:all');
});