forked from VonRosenchild/santa-tracker-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
executable file
·406 lines (373 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
* 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.
*/
/* jshint node: true */
var gulp = require('gulp');
var gutil = require('gulp-util');
var vulcanize = require('gulp-vulcanize');
var compass = require('gulp-compass');
var path = require('path');
var autoprefixer = require('gulp-autoprefixer');
var foreach = require('gulp-foreach');
var del = require('del');
var i18n_replace = require('./gulp_scripts/i18n_replace');
var closureCompiler = require('gulp-closure-compiler');
var mergeStream = require('merge-stream');
var argv = require('yargs').argv;
var replace = require('gulp-replace');
var newer = require('gulp-newer');
var COMPILER_PATH = 'components/closure-compiler/compiler.jar';
var COMPASS_FILES = '{scenes,sass,elements}/**/*.scss';
var CLOSURE_FILES = 'scenes/*/js/**/*.js';
var STATIC_VERSION = 80;
var VERSION = argv.build || STATIC_VERSION;
var STATIC_BASE_URL = argv.baseurl ? argv.baseurl : '';
var STATIC_URL = argv.pretty ? '' : (STATIC_BASE_URL + VERSION + '/');
var PROD_DIR = 'dist_prod';
var STATIC_DIR = 'dist_static';
var PRETTY_DIR = 'dist_pretty';
// path for files (mostly index_*.html) with short cache periods
var DIST_PROD_DIR = argv.pretty ? PRETTY_DIR : PROD_DIR;
// path for static resources
var DIST_STATIC_DIR = argv.pretty ? PRETTY_DIR : (STATIC_DIR + '/' + VERSION);
// scenes are whitelisted into compilation here
var SCENE_CLOSURE_CONFIG = {
airport: {
entryPoint: 'app.Belt'
},
boatload: {
entryPoint: 'app.Game'
},
briefing: {
entryPoint: 'app.Scene'
},
callfromsanta: {
entryPoint: 'app.Scene'
},
citylights: {
entryPoint: 'app.Scene'
},
codelab: {
entryPoint: 'app.wrapper.FrameWrapper'
},
commandcentre: {
entryPoint: 'app.Scene'
},
factory: {
entryPoint: 'app.Scene'
},
glider: {
entryPoint: 'app.Game'
},
gumball: {
entryPoint: 'app.Game'
},
jamband: {
entryPoint: 'app.Game'
},
jetpack: {
entryPoint: 'app.Game'
},
latlong: {
entryPoint: 'app.Game'
},
matching: {
entryPoint: 'app.Game'
},
playground: {
entryPoint: 'app.Scene'
},
postcard: {
entryPoint: 'app.Scene'
},
presentdrop: {
entryPoint: 'app.Game'
},
mercator: {
entryPoint: 'app.Game'
},
racer: {
entryPoint: 'app.Game'
},
runner: {
entryPoint: 'app.Game'
},
santaselfie: {
entryPoint: 'app.Game'
},
seasonofgiving: {
entryPoint: 'app.Game',
},
streetview: {
entryPoint: 'app.Scene'
},
translations: {
entryPoint: 'app.Scene'
},
trivia: {
entryPoint: 'app.Game'
},
windtunnel: {
entryPoint: 'app.Scene'
}
};
gulp.task('clean', function(cleanCallback) {
del([
'{scenes,sass,elements}/**/*.css',
'scenes/*/*.min.js',
'js/service/*.min.js',
], cleanCallback);
});
gulp.task('rm-dist', function(rmCallback) {
del([PROD_DIR, STATIC_DIR, PRETTY_DIR], rmCallback);
});
gulp.task('compass', function() {
return gulp.src(COMPASS_FILES)
.pipe(compass({
project: path.join(__dirname, '/'),
css: '',
sass: '',
environment: 'production',
}))
// NOTE: autoprefixes css properties that need it
.pipe(autoprefixer({}))
.pipe(gulp.dest('.'));
});
gulp.task('compile-santa-api-service', function() {
return gulp.src([
'js/service/*.js',
'!js/service/externs.js',
'!js/service/*.min.js',
])
.pipe(newer('js/service/service.min.js'))
.pipe(closureCompiler({
compilerPath: COMPILER_PATH,
fileName: 'service.min.js',
compilerFlags: addCompilerFlagOptions({
compilation_level: 'ADVANCED_OPTIMIZATIONS',
// warning_level: 'VERBOSE',
language_in: 'ECMASCRIPT5_STRICT',
externs: ['js/service/externs.js', 'third_party/externs/jquery/jquery-1.8.js'],
define: ['crossDomainAjax.BASE="' + (argv.api_base || 'https://santa-api.appspot.com/') + '"'],
jscomp_warning: [
// https://github.com/google/closure-compiler/wiki/Warnings
'accessControls',
'const',
'visibility'
],
})
}))
.pipe(gulp.dest('js/service'));
});
gulp.task('compile-scenes', ['compile-codelab-frame'], function() {
var sceneNames = Object.keys(SCENE_CLOSURE_CONFIG);
// compile each scene, merging them into a single gulp stream as we go
return sceneNames.reduce(function(stream, sceneName) {
var config = SCENE_CLOSURE_CONFIG[sceneName];
var fileName = sceneName + '-scene.min.js';
var dest = 'scenes/' + sceneName;
return stream.add(gulp.src([
'scenes/' + sceneName + '/js/**/*.js',
// add shared scene code
'scenes/shared/js/*.js',
// these externs are annotated with @externs, so we can import them as
// source (so we can use use wildcards in the file name)
'third_party/externs/jquery/*.js',
])
.pipe(newer(dest + '/' + fileName))
.pipe(closureCompiler({
compilerPath: COMPILER_PATH,
fileName: fileName,
compilerFlags: addCompilerFlagOptions({
// all scenes need closure's base.js to get @export support, some need
// full closure library (like seasonofgiving)
// In compilerFlags since it's essentially a static library (and to work
// around gulp-closure-compiler's love of copying files to /tmp).
js: path.resolve('components/closure-library/closure/goog/**.js'),
closure_entry_point: config.entryPoint,
compilation_level: 'SIMPLE_OPTIMIZATIONS',
// warning_level: 'VERBOSE',
language_in: 'ECMASCRIPT5_STRICT',
process_closure_primitives: null,
generate_exports: null,
jscomp_warning: [
// https://github.com/google/closure-compiler/wiki/Warnings
'accessControls',
'const',
'visibility'
],
only_closure_dependencies: null,
// scenes namespace themselves to `app.*`. Move this namespace into
// the global `scenes.sceneName`
output_wrapper:
'var scenes = scenes || {};\n' +
'scenes.' + sceneName + ' = scenes.' + sceneName + ' || {};\n' +
'(function(){%output%}).call({ app: scenes.' + sceneName + ' });'
})
}))
.pipe(gulp.dest(dest)));
}, mergeStream());
});
gulp.task('compile-codelab-frame', function() {
var dest = 'scenes/codelab';
var fileName = 'codelab-frame.min.js';
return gulp.src([
'scenes/codelab/js/**/*.js',
// add shared scene code
'scenes/shared/js/*.js',
// add closure library
'components/closure-library/closure/goog/**/*.js'
])
.pipe(newer(dest + '/' + fileName))
.pipe(closureCompiler({
compilerPath: COMPILER_PATH,
fileName: fileName,
compilerFlags: addCompilerFlagOptions({
closure_entry_point: 'app.Game',
compilation_level: 'SIMPLE_OPTIMIZATIONS',
// warning_level: 'VERBOSE',
language_in: 'ECMASCRIPT5_STRICT',
process_closure_primitives: null,
generate_exports: null,
jscomp_warning: [
// https://github.com/google/closure-compiler/wiki/Warnings
'accessControls',
'const',
'visibility'
],
only_closure_dependencies: null,
output_wrapper: '(function(){%output%}).call(this);'
})
}))
.pipe(gulp.dest(dest));
});
function addCompilerFlagOptions(opts) {
// Add any compiler options specified by command line flags.
if (argv.pretty) {
opts.formatting = 'PRETTY_PRINT';
}
return opts;
}
gulp.task('vulcanize-scenes', ['rm-dist', 'compass', 'compile-scenes'], function() {
return gulp.src([
'scenes/*/*-scene*.html'
], {base: './'})
// gulp-vulcanize doesn't currently handle multiple files in multiple
// directories well right now, so vulcanize them one at a time
.pipe(foreach(function(stream, file) {
var dest = path.dirname(path.relative(__dirname, file.path));
return stream.pipe(vulcanize({
excludes: {
// these are inlined in elements.html
imports: [
'jquery.html$',
'modernizr.html$',
'polymer.html$',
'base-scene.html$',
'i18n-msg.html$',
'core-a11y-keys.html$',
'core-shared-lib.html$',
'google-maps-api.html$',
'google-client-api.html',
'google-plusone-api.html', // tracker
'google-youtube-api.html',// tracker
'google-jsapi.html', // tracker
'core-selection.html',
'core-selector.html',
'core-pages.html',
'paper-fab.html',
'paper-item.html'
]
},
strip: !argv.pretty,
csp: true,
inline: true,
dest: dest
}))
.pipe(i18n_replace({
strict: !!argv.strict,
path: '_messages',
}))
.pipe(gulp.dest(path.join(DIST_STATIC_DIR, dest)));
}));
});
gulp.task('vulcanize-codelab-frame', ['rm-dist', 'compass', 'compile-scenes'], function() {
return gulp.src('scenes/codelab/codelab-frame_en.html', {base: './'})
.pipe(argv.pretty ? gutil.noop() : replace(/window\.DEV ?= ?true.*/, ''))
.pipe(vulcanize({
strip: !argv.pretty,
csp: true,
inline: true,
dest: 'scenes/codelab'
}))
.pipe(i18n_replace({
strict: !!argv.strict,
path: '_messages'
}))
.pipe(gulp.dest(DIST_STATIC_DIR + '/scenes/codelab/'));
});
// vulcanize elements separately as we want to inline polymer.html and
// base-scene.html here
gulp.task('vulcanize-elements', ['rm-dist', 'compass', 'compile-santa-api-service'], function() {
return gulp.src('elements/elements_en.html', {base: './'})
.pipe(vulcanize({
strip: !argv.pretty,
csp: true,
inline: true,
dest: 'elements/'
}))
.pipe(i18n_replace({
strict: !!argv.strict,
path: '_messages',
}))
.pipe(gulp.dest(DIST_STATIC_DIR + '/elements/'));
});
gulp.task('vulcanize', ['vulcanize-scenes', 'vulcanize-elements', 'vulcanize-codelab-frame']);
gulp.task('i18n_index', function() {
return gulp.src(['index.html', 'error.html', 'upgrade.html'])
.pipe(argv.pretty ? gutil.noop() : replace(/window\.DEV ?= ?true.*/, ''))
.pipe(replace('<base href="">',
'<base href="' + STATIC_URL + '">'))
.pipe(i18n_replace({
strict: !!argv.strict,
path: '_messages',
}))
.pipe(gulp.dest(DIST_PROD_DIR));
});
// copy needed assets (images, sounds, polymer elements, etc) to dist directories
gulp.task('copy-assets', ['rm-dist', 'vulcanize', 'i18n_index'], function() {
var staticStream = gulp.src([
'manifest.json',
'audio/*',
'images/*.{png,svg,jpg,gif,ico}',
'third_party/**',
'sass/*.css',
'scenes/**/img/**/*.{png,jpg,svg,gif,cur}',
'elements/**/img/*.{png,jpg,svg,gif}',
'components/webcomponentsjs/webcomponents.min.js'
], {base: './'})
.pipe(gulp.dest(DIST_STATIC_DIR));
var prodStream = gulp.src([
'images/og.png',
'embed.js'
], {base: './'})
.pipe(gulp.dest(DIST_PROD_DIR));
return mergeStream(staticStream, prodStream);
});
gulp.task('watch', function() {
gulp.watch(COMPASS_FILES, ['compass']);
gulp.watch(CLOSURE_FILES, ['compile-scenes']);
});
gulp.task('default', ['copy-assets']);