forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
692 lines (637 loc) · 22 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/**
* Copyright 2015 The AMP HTML Authors. 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.
*/
checkMinVersion();
var autoprefixer = require('autoprefixer');
var babel = require('babelify');
var browserify = require('browserify');
var buffer = require('vinyl-buffer');
var closureCompile = require('./build-system/tasks/compile').closureCompile;
var cssnano = require('cssnano');
var file = require('gulp-file');
var fs = require('fs-extra');
var gulp = require('gulp-help')(require('gulp'));
var gulpWatch = require('gulp-watch');
var lazypipe = require('lazypipe');
var minimist = require('minimist');
var postcss = require('postcss');
var rename = require('gulp-rename');
var replace = require('gulp-replace');
var source = require('vinyl-source-stream');
var sourcemaps = require('gulp-sourcemaps');
var touch = require('touch')
var uglify = require('gulp-uglify');
var util = require('gulp-util');
var watchify = require('watchify');
var windowConfig = require('./build-system/window-config');
var wrap = require('gulp-wrap');
var internalRuntimeVersion = require('./build-system/internal-version').VERSION;
var internalRuntimeToken = require('./build-system/internal-version').TOKEN;
var argv = minimist(process.argv.slice(2), { boolean: ['strictBabelTransform'] });
require('./build-system/tasks');
// NOTE: see https://github.com/ai/browserslist#queries for `browsers` list
var cssprefixer = autoprefixer({
browsers: [
'last 5 ChromeAndroid versions',
'last 5 iOS versions',
'last 3 FirefoxAndroid versions',
'last 5 Android versions',
'last 2 ExplorerMobile versions',
'last 2 OperaMobile versions',
'last 2 OperaMini versions'
]
});
cssnano = cssnano({
convertValues: false,
zindex: false
});
/**
* Build all the AMP extensions
*
* @param {!Object} options
*/
function buildExtensions(options) {
// We pass watch further in to have browserify watch the built file
// and update it if any of its required deps changed.
// Each extension and version must be listed individually here.
buildExtension('amp-access', '0.1', true, options);
buildExtension('amp-analytics', '0.1', false, options);
buildExtension('amp-anim', '0.1', false, options);
buildExtension('amp-audio', '0.1', false, options);
buildExtension('amp-brightcove', '0.1', false, options);
buildExtension('amp-carousel', '0.1', true, options);
buildExtension('amp-dynamic-css-classes', '0.1', false, options);
buildExtension('amp-facebook', '0.1', false, options);
buildExtension('amp-fit-text', '0.1', true, options);
buildExtension('amp-font', '0.1', false, options);
buildExtension('amp-iframe', '0.1', false, options);
buildExtension('amp-image-lightbox', '0.1', true, options);
buildExtension('amp-instagram', '0.1', false, options);
buildExtension('amp-lightbox', '0.1', false, options);
buildExtension('amp-list', '0.1', false, options);
buildExtension('amp-mustache', '0.1', false, options);
buildExtension('amp-pinterest', '0.1', true, options);
buildExtension('amp-install-serviceworker', '0.1', false, options);
/**
* @deprecated `amp-slides` is deprecated and will be deleted before 1.0.
* Please see {@link AmpCarousel} with `type=slides` attribute instead.
*/
buildExtension('amp-slides', '0.1', false, options);
buildExtension('amp-twitter', '0.1', false, options);
buildExtension('amp-user-notification', '0.1', true, options);
buildExtension('amp-vimeo', '0.1', false, options);
buildExtension('amp-vine', '0.1', false, options);
buildExtension('amp-youtube', '0.1', false, options);
}
/**
* Compile the polyfills script and drop it in the build folder
*/
function polyfillsForTests() {
compileJs('./src/', 'polyfills.js', './build/');
}
/**
* Compile and optionally minify the stylesheets and the scripts
* and drop them in the dist folder
*
* @param {boolean} watch
* @param {boolean} shouldMinify
*/
function compile(watch, shouldMinify) {
compileCss();
// For compilation with babel we start with the amp-babel entry point,
// but then rename to the amp.js which we've been using all along.
compileJs('./src/', 'amp-babel.js', './dist', {
toName: 'amp.js',
minifiedName: 'v0.js',
includePolyfills: true,
watch: watch,
minify: shouldMinify,
// If there is a sync JS error during initial load,
// at least try to unhide the body.
wrapper: windowConfig.getTemplate() +
'try{(function(){<%= contents %>})()}catch(e){' +
'setTimeout(function(){' +
'var s=document.body.style;' +
's.opacity=1;' +
's.visibility="visible";' +
's.animation="none";' +
's.WebkitAnimation="none;"},1000);throw e};'
});
compileJs('./3p/', 'integration.js', './dist.3p/' + internalRuntimeVersion, {
minifiedName: 'f.js',
watch: watch,
minify: shouldMinify
});
thirdPartyBootstrap(watch, shouldMinify);
}
/**
* Compile all the css and drop in the build folder
*
* @return {!Promise} containing a Readable Stream
*/
function compileCss() {
console.info('Recompiling CSS.');
return jsifyCssPromise('css/amp.css').then(function(css) {
return gulp.src('css/**.css')
.pipe(file('css.js', 'export const cssText = ' + css))
.pipe(gulp.dest('build'));
});
}
/**
* 'Jsify' a CSS file - Adds vendor specific css prefixes to the css file,
* compresses the file, removes the copyright comment, and adds the sourceURL
* to the stylesheet
*
* @param {string} filename css file
* @return {!Promise} that resolves with the css content after processing
*/
function jsifyCssPromise(filename) {
var css = fs.readFileSync(filename, 'utf8');
var transformers = [cssprefixer, cssnano];
// Remove copyright comment. Crude hack to get our own copyright out
// of the string.
return postcss(transformers).process(css.toString())
.then(function(result) {
result.warnings().forEach(function(warn) {
console.warn(warn.toString());
});
var css = result.css;
return JSON.stringify(css + '\n/*# sourceURL=/' + filename + '*/');
});
}
/**
* Enables watching for file changes in css, extensions, and examples.
*/
function watch() {
gulpWatch('css/**/*.css', function() {
compileCss();
});
buildExtensions({
watch: true
});
buildExamples(true);
compile(true);
}
/**
* Copies extensions from
* extensions/$name/$version/$name.js
* to
* dist/v0/$name-$version.js
*
* Optionally copies the CSS at extensions/$name/$version/$name.css into the
* JS file marked with $CSS$ as a third argument to the registerElement call.
*
* @param {string} name Name of the extension. Must be the sub directory in
* the extensions directory and the name of the JS and optional CSS file.
* @param {string} version Version of the extension. Must be identical to
* the sub directory inside the extension directory
* @param {boolean} hasCss Whether there is a CSS file for this extension.
* @param {?Object} options
* @return {!Stream} Gulp object
*/
function buildExtension(name, version, hasCss, options) {
options = options || {};
console.log('Bundling ' + name);
var path = 'extensions/' + name + '/' + version;
var jsPath = path + '/' + name + '.js';
// Building extensions is a 2 step process because of the renaming
// and CSS inlining. This watcher watches the original file, copies
// it to the destination and adds the CSS.
if (options.watch) {
// Do not set watchers again when we get called by the watcher.
var copy = Object.create(options);
copy.watch = false;
gulpWatch(path + '/*', function() {
buildExtension(name, version, hasCss, copy);
});
}
var js = fs.readFileSync(jsPath, 'utf8');
if (hasCss) {
return jsifyCssPromise(path + '/' + name + '.css').then(function(css) {
console.assert(/\$CSS\$/.test(js),
'Expected to find $CSS$ marker in extension JS: ' + jsPath);
js = js.replace(/\$CSS\$/, css);
return buildExtensionJs(js, path, name, version, options);
});
} else {
return buildExtensionJs(js, path, name, version, options);
}
}
/**
* Build the JavaScript for the extension specified
*
* @param {string} js JavaScript file content
* @param {string} path Path to the extensions directory
* @param {string} name Name of the extension. Must be the sub directory in
* the extensions directory and the name of the JS and optional CSS file.
* @param {string} version Version of the extension. Must be identical to
* the sub directory inside the extension directory
* @param {!Object} options
* @return {!Stream} Gulp object
*/
function buildExtensionJs(js, path, name, version, options) {
var builtName = name + '-' + version + '.max.js';
var minifiedName = name + '-' + version + '.js';
var latestName = name + '-latest.js';
return gulp.src(path + '/*.js')
.pipe(file(builtName, js))
.pipe(gulp.dest('build/all/v0/'))
.on('end', function() {
compileJs('./build/all/v0/', builtName, './dist/v0', {
watch: options.watch,
minify: options.minify,
minifiedName: minifiedName,
latestName: latestName,
wrapper: '(window.AMP = window.AMP || [])' +
'.push(function(AMP) {<%= contents %>\n});',
});
});
}
/**
* Main Build
*/
function build() {
process.env.NODE_ENV = 'development';
polyfillsForTests();
buildExtensions();
buildExamples(false);
compile();
}
/**
* Dist Build
*/
function dist() {
process.env.NODE_ENV = 'production';
compile(false, true);
buildExtensions({minify: true});
buildExperiments({minify: true, watch: false});
buildLoginDone({minify: true, watch: false});
}
/**
* Build the examples
*
* @param {boolean} watch
*/
function buildExamples(watch) {
if (watch) {
gulpWatch('examples/*.html', function() {
buildExamples(false);
});
}
fs.copy('examples/', 'examples.build/', {clobber: true},
function(err) {
if (err) {
return util.log(util.colors.red('copy error: ', err));
}
util.log(util.colors.green('copied examples to examples.build'));
});
// Also update test-example-validation.js
buildExample('ads.amp.html');
buildExample('analytics-notification.amp.html');
buildExample('analytics.amp.html');
buildExample('article.amp.html');
buildExample('responsive.amp.html');
buildExample('article-access.amp.html');
buildExample('csp.amp.html');
buildExample('metadata-examples/article-json-ld.amp.html');
buildExample('metadata-examples/article-microdata.amp.html');
buildExample('metadata-examples/recipe-json-ld.amp.html');
buildExample('metadata-examples/recipe-microdata.amp.html');
buildExample('metadata-examples/review-json-ld.amp.html');
buildExample('metadata-examples/review-microdata.amp.html');
buildExample('metadata-examples/video-json-ld.amp.html');
buildExample('metadata-examples/video-microdata.amp.html');
buildExample('everything.amp.html');
buildExample('font.amp.html');
buildExample('facebook.amp.html');
buildExample('instagram.amp.html');
buildExample('pinterest.amp.html');
buildExample('released.amp.html');
buildExample('twitter.amp.html');
buildExample('user-notification.amp.html');
buildExample('vimeo.amp.html');
buildExample('vine.amp.html');
// TODO(dvoytenko, #1393): Enable for proxy-testing.
// // Examples are also copied into `c/` directory for AMP-proxy testing.
// fs.copy('examples.build/', 'c/', {clobber: true},
// copyHandler.bind(null, 'examples.build to c folder'));
}
/**
* Copies an examples file to examples.build folder and changes all
* JS references to local / minified copies.
*
* @param {string} name HTML file in examples/
*/
function buildExample(name) {
var input = 'examples/' + name;
console.log('Processing ' + name);
var html = fs.readFileSync(input, 'utf8');
var max = html;
max = max.replace(/(https:\/\/cdn.ampproject.org\/.+?).js/g, '$1.max.js');
max = max.replace('https://cdn.ampproject.org/v0.max.js', '../dist/amp.js');
max = max.replace(/https:\/\/cdn.ampproject.org\/v0\//g, '../dist/v0/');
gulp.src(input)
.pipe(file(name.replace('.html', '.max.html'),max))
.pipe(gulp.dest('examples.build/'));
var min = max;
min = min.replace(/\.max\.js/g, '.js');
min = min.replace('../dist/amp.js', '../dist/v0.js');
gulp.src(input)
.pipe(file(name.replace('.html', '.min.html'), min))
.pipe(gulp.dest('examples.build/'));
}
/**
* Copies frame.html to output folder, replaces js references to minified
* copies, and generates symlink to it.
*
* @param {boolean} watch
* @param {boolean} shouldMinify
*/
function thirdPartyBootstrap(watch, shouldMinify) {
var input = '3p/frame.max.html';
if (watch) {
gulpWatch(input, function() {
thirdPartyBootstrap(false);
});
}
console.log('Processing ' + input);
var html = fs.readFileSync(input, 'utf8');
var min = html;
min = min.replace(/\.\/integration\.js/g, './f.js');
gulp.src(input)
.pipe(file('frame.html', min))
.pipe(gulp.dest('dist.3p/' + internalRuntimeVersion))
.on('end', function() {
var aliasToLatestBuild = 'dist.3p/current';
if (shouldMinify) {
aliasToLatestBuild += '-min';
}
if (fs.existsSync(aliasToLatestBuild)) {
fs.unlinkSync(aliasToLatestBuild);
}
fs.symlinkSync(
'./' + internalRuntimeVersion,
aliasToLatestBuild,
'dir');
});
}
var activeBundleOperationCount = 0;
/**
* Compile a javascript file
*
* @param {string} srcDir Path to the src directory
* @param {string} srcFilename Name of the JS source file
* @param {string} destDir Destination folder for output script
* @param {?Object} options
*/
function compileJs(srcDir, srcFilename, destDir, options) {
options = options || {};
var bundler = browserify(srcDir + srcFilename, {debug: true})
.transform(babel, { loose: argv.strictBabelTransform ? undefined : 'all' });
if (options.watch) {
bundler = watchify(bundler);
}
var wrapper = options.wrapper || '<%= contents %>';
var lazybuild = lazypipe()
.pipe(source, srcFilename)
.pipe(buffer)
.pipe(replace, /\$internalRuntimeVersion\$/g, internalRuntimeVersion)
.pipe(replace, /\$internalRuntimeToken\$/g, internalRuntimeToken)
.pipe(wrap, wrapper)
.pipe(sourcemaps.init.bind(sourcemaps), {loadMaps: true});
var lazywrite = lazypipe()
.pipe(sourcemaps.write.bind(sourcemaps), './')
.pipe(gulp.dest.bind(gulp), destDir);
function rebundle() {
activeBundleOperationCount++;
bundler.bundle()
.on('error', function(err) { console.error(err); this.emit('end'); })
.pipe(lazybuild())
.pipe(rename(options.toName || srcFilename))
.pipe(lazywrite())
.on('end', function() {
activeBundleOperationCount--;
if (activeBundleOperationCount == 0) {
console.info('All current JS updates done.');
}
});
}
if (options.watch) {
bundler.on('update', function() {
console.log('-> bundling ' + srcDir + '...');
rebundle();
// Touch file in unit test set. This triggers rebundling of tests because
// karma only considers changes to tests files themselves re-bundle
// worthy.
touch('test/_init_tests.js');
});
}
function minify() {
console.log('Minifying ' + srcFilename);
closureCompile(srcDir + srcFilename, destDir, options.minifiedName,
options)
.then(function() {
fs.writeFileSync(destDir + '/version.txt', internalRuntimeVersion);
if (options.latestName) {
fs.copySync(
destDir + '/' + options.minifiedName,
destDir + '/' + options.latestName);
}
});
}
/*
Pre closure compiler minification. Add this back, should we have problems
with closure.
function minify() {
console.log('Minifying ' + srcFilename);
bundler.bundle()
.on('error', function(err) { console.error(err); this.emit('end'); })
.pipe(lazybuild())
.pipe(uglify({
preserveComments: 'some'
}))
.pipe(rename(options.minifiedName))
.pipe(lazywrite())
.on('end', function() {
fs.writeFileSync(destDir + '/version.txt', internalRuntimeVersion);
if (options.latestName) {
fs.copySync(
destDir + '/' + options.minifiedName,
destDir + '/' + options.latestName);
}
});
}
*/
if (options.minify) {
minify();
} else {
rebundle();
}
}
/**
* Build all the AMP experiments.html/js.
*
* @param {!Object} options
*/
function buildExperiments(options) {
options = options || {};
console.log('Bundling experiments.html/js');
function copyHandler(name, err) {
if (err) {
return util.log(util.colors.red('copy error: ', err));
}
util.log(util.colors.green('copied ' + name));
}
var path = 'tools/experiments';
var htmlPath = path + '/experiments.html';
var jsPath = path + '/experiments.js';
var watch = options.watch;
if (watch === undefined) {
watch = argv.watch || argv.w;
}
// Building extensions is a 2 step process because of the renaming
// and CSS inlining. This watcher watches the original file, copies
// it to the destination and adds the CSS.
if (watch) {
// Do not set watchers again when we get called by the watcher.
var copy = Object.create(options);
copy.watch = false;
gulpWatch(path + '/*', function() {
buildExperiments(copy);
});
}
// Build HTML.
console.log('Processing ' + htmlPath);
var html = fs.readFileSync(htmlPath, 'utf8');
var minHtml = html.replace('../../dist.tools/experiments/experiments.max.js',
'https://cdn.ampproject.org/v0/experiments.js');
gulp.src(htmlPath)
.pipe(file('experiments.cdn.html', minHtml))
.pipe(gulp.dest('dist.tools/experiments/'));
// Build JS.
var js = fs.readFileSync(jsPath, 'utf8');
var builtName = 'experiments.max.js';
var minifiedName = 'experiments.js';
return gulp.src(path + '/*.js')
.pipe(file(builtName, js))
.pipe(gulp.dest('build/experiments/'))
.on('end', function() {
compileJs('./build/experiments/', builtName, './dist.tools/experiments/', {
watch: false,
minify: options.minify || argv.minify,
minifiedName: minifiedName,
});
});
}
/**
* Build "Login Done" page.
*
* @param {!Object} options
*/
function buildLoginDone(options) {
return buildLoginDoneVersion('0.1', options);
}
/**
* Build "Login Done" page for the specified version.
*
* @param {!Object} options
*/
function buildLoginDoneVersion(version, options) {
options = options || {};
console.log('Bundling amp-login-done.html/js');
function copyHandler(name, err) {
if (err) {
return util.log(util.colors.red('copy error: ', err));
}
util.log(util.colors.green('copied ' + name));
}
var path = 'extensions/amp-access/' + version + '/';
var htmlPath = path + 'amp-login-done.html';
var jsPath = path + 'amp-login-done.js';
var watch = options.watch;
if (watch === undefined) {
watch = argv.watch || argv.w;
}
// Building extensions is a 2 step process because of the renaming
// and CSS inlining. This watcher watches the original file, copies
// it to the destination and adds the CSS.
if (watch) {
// Do not set watchers again when we get called by the watcher.
var copy = Object.create(options);
copy.watch = false;
gulpWatch(path + '/*', function() {
buildLoginDoneVersion(version, copy);
});
}
// Build HTML.
console.log('Processing ' + htmlPath);
var html = fs.readFileSync(htmlPath, 'utf8');
var minHtml = html.replace(
'../../../dist/v0/amp-login-done-' + version + '.max.js',
'https://cdn.ampproject.org/v0/amp-login-done-' + version + '.js');
function mkdirSync(path) {
try {
fs.mkdirSync(path);
} catch(e) {
if (e.code != 'EEXIST') {
throw e;
}
}
}
mkdirSync('dist');
mkdirSync('dist/v0');
fs.writeFileSync('dist/v0/amp-login-done-' + version + '.html',
minHtml);
// Build JS.
var js = fs.readFileSync(jsPath, 'utf8');
var builtName = 'amp-login-done-' + version + '.max.js';
var minifiedName = 'amp-login-done-' + version + '.js';
var latestName = 'amp-login-done-latest.js';
return gulp.src(path + '/*.js')
.pipe(file(builtName, js))
.pipe(gulp.dest('build/all/v0/'))
.on('end', function() {
compileJs('./build/all/v0/', builtName, './dist/v0/', {
watch: false,
minify: options.minify || argv.minify,
minifiedName: minifiedName,
latestName: latestName,
});
});
}
/**
* Exits the process if gulp is running with a node version lower than
* the required version. This has to run very early to avoid parse
* errors from modules that e.g. use let.
*/
function checkMinVersion() {
var majorVersion = Number(process.version.replace(/v/, '').split('.')[0]);
if (majorVersion < 4) {
console.log('Please run AMP with node.js version 4 or newer.');
console.log('Your version is', process.version);
process.exit(1);
}
}
/**
* Gulp tasks
*/
gulp.task('build', 'Builds the AMP library', build);
gulp.task('css', 'Recompile css to build directory', compileCss);
gulp.task('default', 'Same as "watch"', ['watch', 'serve']);
gulp.task('dist', 'Build production binaries', dist);
gulp.task('extensions', 'Build AMP Extensions', buildExtensions);
gulp.task('watch', 'Watches for changes in files, re-build', watch);
gulp.task('build-experiments', 'Builds experiments.html/js', buildExperiments);
gulp.task('build-login-done', 'Builds login-done.html/js', buildLoginDone);