This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
809 lines (751 loc) · 26.4 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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
/**
* 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 $$ = require('gulp-load-plugins')();
var babel = require('babelify');
var browserify = require('browserify');
var buffer = require('vinyl-buffer');
var closureCompile = require('./build-system/tasks/compile').closureCompile;
var cleanupBuildDir = require('./build-system/tasks/compile').cleanupBuildDir;
var jsifyCssAsync = require('./build-system/tasks/jsify-css').jsifyCssAsync;
var fs = require('fs-extra');
var gulp = $$.help(require('gulp'));
var lazypipe = require('lazypipe');
var minimatch = require('minimatch');
var minimist = require('minimist');
var source = require('vinyl-source-stream');
var touch = require('touch');
var watchify = require('watchify');
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'] });
var cssOnly = argv['css-only'];
require('./build-system/tasks');
// All declared extensions.
var extensions = {};
// Each extension and version must be listed individually here.
// NOTE: No new extensions must pass the NO_TYPE_CHECK argument.
declareExtension('amp-access', '0.1', true, 'NO_TYPE_CHECK');
declareExtension('amp-access-laterpay', '0.1', false, 'NO_TYPE_CHECK');
declareExtension('amp-accordion', '0.1', true);
declareExtension('amp-ad', '0.1', false);
declareExtension('amp-ad-network-adsense-impl', 0.1, false);
declareExtension('amp-ad-network-doubleclick-impl', 0.1, false);
declareExtension('amp-ad-network-fake-impl', 0.1, false);
declareExtension('amp-analytics', '0.1', false);
declareExtension('amp-anim', '0.1', false);
declareExtension('amp-apester-media', '0.1', true, 'NO_TYPE_CHECK');
declareExtension('amp-app-banner', '0.1', true);
declareExtension('amp-audio', '0.1', false);
declareExtension('amp-brid-player', '0.1', false);
declareExtension('amp-brightcove', '0.1', false);
declareExtension('amp-kaltura-player', '0.1', false);
declareExtension('amp-carousel', '0.1', true);
declareExtension('amp-dailymotion', '0.1', false);
declareExtension('amp-dynamic-css-classes', '0.1', false, 'NO_TYPE_CHECK');
declareExtension('amp-experiment', '0.1', false, 'NO_TYPE_CHECK');
declareExtension('amp-facebook', '0.1', false);
declareExtension('amp-fit-text', '0.1', true, 'NO_TYPE_CHECK');
declareExtension('amp-font', '0.1', false, 'NO_TYPE_CHECK');
declareExtension('amp-form', '0.1', true);
declareExtension('amp-fresh', '0.1', true);
declareExtension('amp-fx-flying-carpet', '0.1', true);
declareExtension('amp-gfycat', '0.1', false);
declareExtension('amp-iframe', '0.1', false, 'NO_TYPE_CHECK');
declareExtension('amp-image-lightbox', '0.1', true);
declareExtension('amp-instagram', '0.1', false);
declareExtension('amp-install-serviceworker', '0.1', false);
declareExtension('amp-jwplayer', '0.1', false, 'NO_TYPE_CHECK');
declareExtension('amp-lightbox', '0.1', false);
declareExtension('amp-lightbox-viewer', '0.1', true, 'NO_TYPE_CHECK');
declareExtension('amp-list', '0.1', false, 'NO_TYPE_CHECK');
declareExtension('amp-live-list', '0.1', true);
declareExtension('amp-mustache', '0.1', false, 'NO_TYPE_CHECK');
declareExtension('amp-o2-player', '0.1', false, 'NO_TYPE_CHECK');
declareExtension('amp-pinterest', '0.1', true, 'NO_TYPE_CHECK');
declareExtension('amp-reach-player', '0.1', false);
declareExtension('amp-share-tracking', '0.1', false);
declareExtension('amp-sidebar', '0.1', true);
declareExtension('amp-soundcloud', '0.1', false);
declareExtension('amp-springboard-player', '0.1', false);
declareExtension('amp-sticky-ad', '0.1', true);
/**
* @deprecated `amp-slides` is deprecated and will be deleted before 1.0.
* Please see {@link AmpCarousel} with `type=slides` attribute instead.
*/
declareExtension('amp-slides', '0.1', false, 'NO_TYPE_CHECK');
declareExtension('amp-social-share', '0.1', true);
declareExtension('amp-twitter', '0.1', false);
declareExtension('amp-user-notification', '0.1', true, 'NO_TYPE_CHECK');
declareExtension('amp-vimeo', '0.1', false, 'NO_TYPE_CHECK');
declareExtension('amp-vine', '0.1', false, 'NO_TYPE_CHECK');
declareExtension('amp-viz-vega', '0.1', true);
declareExtension('amp-google-vrview-image', '0.1', false, 'NO_TYPE_CHECK');
declareExtension('amp-youtube', '0.1', false);
/**
* @param {string} name
* @param {string} version E.g. 0.1
* @param {boolean} hasCss Whether the extension comes with CSS.
* @param {string=} opt_noTypeCheck Whether not to check types.
* No new extension must pass this.
* @param {!Array<string>=} opt_extraGlobs
*/
function declareExtension(name, version, hasCss, opt_noTypeCheck,
opt_extraGlobs) {
extensions[name + '-' + version] = {
name: name,
version: version,
hasCss: hasCss,
noTypeCheck: !!opt_noTypeCheck,
extraGlobs: opt_extraGlobs,
}
}
/**
* Build all the AMP extensions
*
* @param {!Object} options
*/
function buildExtensions(options) {
for (var key in extensions) {
var e = extensions[key];
buildExtension(e.name, e.version, e.hasCss, options, e.extraGlobs);
}
}
/**
* 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
* @param {boolean=} opt_preventRemoveAndMakeDir
* @param {boolean=} opt_checkTypes
*/
function compile(watch, shouldMinify, opt_preventRemoveAndMakeDir,
opt_checkTypes) {
compileCss();
compileJs('./3p/', 'integration.js',
'./dist.3p/' + (shouldMinify ? internalRuntimeVersion : 'current'), {
minifiedName: 'f.js',
checkTypes: opt_checkTypes,
watch: watch,
minify: shouldMinify,
preventRemoveAndMakeDir: opt_preventRemoveAndMakeDir,
externs: ['ads/ads.extern.js',],
includeBasicPolyfills: true,
include3pDirectories: true,
});
// 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,
checkTypes: opt_checkTypes,
watch: watch,
preventRemoveAndMakeDir: opt_preventRemoveAndMakeDir,
minify: shouldMinify,
// If there is a sync JS error during initial load,
// at least try to unhide the body.
wrapper: '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};'
});
if (opt_checkTypes) {
// We don't rerun type check for the shadow entry point for now.
return;
}
// Entry point for shadow runtime.
compileJs('./src/', 'amp-shadow-babel.js', './dist', {
toName: 'amp-shadow.js',
minifiedName: 'shadow-v0.js',
includePolyfills: true,
checkTypes: opt_checkTypes,
watch: watch,
preventRemoveAndMakeDir: opt_preventRemoveAndMakeDir,
minify: shouldMinify,
wrapper: '<%= contents %>'
});
var frameHtml = '3p/frame.max.html';
thirdPartyBootstrap(frameHtml, shouldMinify);
if (watch) {
$$.watch(frameHtml, function() {
thirdPartyBootstrap(frameHtml, shouldMinify);
});
}
}
/**
* Compile all the css and drop in the build folder
*
* @return {!Promise} containing a Readable Stream
*/
function compileCss() {
console.info('Recompiling CSS.');
return jsifyCssAsync('css/amp.css').then(function(css) {
return gulp.src('css/**.css')
.pipe($$.file('css.js', 'export const cssText = ' + css))
.pipe(gulp.dest('build'));
});
}
/**
* Enables watching for file changes in css, extensions.
*/
function watch() {
$$.watch('css/**/*.css', function() {
compileCss();
});
buildAlp({
watch: true,
});
buildExtensions({
watch: 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
* a generated JS file that can be required from the extensions as
* `import {CSS} from '../../../build/$name-0.1.css';`
*
* @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
* @param {!Array=} opt_extraGlobs
* @return {!Stream} Gulp object
*/
function buildExtension(name, version, hasCss, options, opt_extraGlobs) {
if (cssOnly && !hasCss) {
return Promise.resolve();
}
options = options || {};
options.extraGlobs = opt_extraGlobs;
var path = 'extensions/' + name + '/' + version;
var jsPath = path + '/' + name + '.js';
var jsTestPath = path + '/test/' + 'test-' + name + '.js';
if (argv.files && options.bundleOnlyIfListedInFiles) {
const passedFiles = Array.isArray(argv.files) ? argv.files : [argv.files];
const shouldBundle = passedFiles.some(glob => {
return minimatch(jsPath, glob) || minimatch(jsTestPath, glob);
});
if (!shouldBundle) {
return;
}
}
$$.util.log('Bundling ' + name);
// 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;
$$.watch(path + '/*', function() {
buildExtension(name, version, hasCss, copy);
});
}
if (hasCss) {
mkdirSync('build');
return jsifyCssAsync(path + '/' + name + '.css').then(function(css) {
var jsCss = 'export const CSS = ' + css + ';\n';
var builtName = 'build/' + name + '-' + version + '.css.js';
fs.writeFileSync(builtName, jsCss, 'utf-8');
if (cssOnly) {
return Promise.resolve();
}
return buildExtensionJs(path, name, version, options);
});
} else {
return buildExtensionJs(path, name, version, options);
}
}
/**
* Build the JavaScript for the extension specified
*
* @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(path, name, version, options) {
var filename = options.filename || name + '.js';
compileJs(path + '/', filename, './dist/v0', {
watch: options.watch,
preventRemoveAndMakeDir: options.preventRemoveAndMakeDir,
minify: options.minify,
toName: name + '-' + version + '.max.js',
minifiedName: name + '-' + version + '.js',
latestName: name + '-latest.js',
extraGlobs: options.extraGlobs,
// Wrapper that either registers the extension or schedules it for
// execution after the main binary comes back.
// The `function` is wrapped in `()` to avoid lazy parsing it,
// since it will be immediately executed anyway.
// See https://github.com/ampproject/amphtml/issues/3977
wrapper: options.noWrapper ? '' : ('(window.AMP = window.AMP || [])' +
'.push({n:"' + name + '", f:(function(AMP) {<%= contents %>\n})});'),
});
}
/**
* Main Build
*/
function build() {
process.env.NODE_ENV = 'development';
polyfillsForTests();
buildAlp();
buildSw();
buildExtensions({bundleOnlyIfListedInFiles: true});
compile();
}
/**
* Dist Build
*/
function dist() {
process.env.NODE_ENV = 'production';
cleanupBuildDir();
compile(false, true, true);
// NOTE:
// When adding a line here, consider whether you need to include polyfills
// and whether you need to init logging (initLogConstructor).
buildAlp({minify: true, watch: false, preventRemoveAndMakeDir: true});
buildSw({minify: true, watch: false, preventRemoveAndMakeDir: true});
buildExtensions({minify: true, preventRemoveAndMakeDir: true});
buildExperiments({minify: true, watch: false, preventRemoveAndMakeDir: true});
buildLoginDone({minify: true, watch: false, preventRemoveAndMakeDir: true});
}
/**
* Dedicated type check path.
*/
function checkTypes() {
process.env.NODE_ENV = 'production';
cleanupBuildDir();
// Disabled to improve type check performance, since this provides
// little incremental value.
/*buildExperiments({
minify: true,
checkTypes: true,
preventRemoveAndMakeDir: true,
});*/
var compileSrcs = [
'./src/amp-babel.js',
'./src/amp-shadow.js',
'./ads/alp/install-alp.js',
'./src/service-worker/shell.js',
'./src/service-worker/core.js',
'./src/service-worker/kill.js',
];
var extensionSrcs = Object.values(extensions).filter(function(extension) {
return !extension.noTypeCheck;
}).map(function(extension) {
return './extensions/' + extension.name + '/' +
extension.version + '/' + extension.name + '.js';
}).sort();
closureCompile(compileSrcs.concat(extensionSrcs), './dist',
'check-types.js', {
includePolyfills: true,
checkTypes: true,
});
// Type check 3p/ads code.
closureCompile(['./3p/integration.js'], './dist',
'integration-check-types.js', {
externs: ['ads/ads.extern.js',],
includeBasicPolyfills: true,
include3pDirectories: true,
checkTypes: true,
});
}
/**
* Copies frame.html to output folder, replaces js references to minified
* copies, and generates symlink to it.
*
* @param {string} input
* @param {boolean} shouldMinify
*/
function thirdPartyBootstrap(input, shouldMinify) {
$$.util.log('Processing ' + input);
if (!shouldMinify) {
gulp.src(input)
.pipe(gulp.dest('dist.3p/current'));
return;
}
// By default we use an absolute URL, that is independent of the
// actual frame host for the JS inside the frame.
// But during testing we need a relative reference because the
// version is not available on the absolute path.
var integrationJs = argv.fortesting
? './f.js'
: `https://3p.ampproject.net/${internalRuntimeVersion}/f.js`;
// Convert default relative URL to absolute min URL.
var html = fs.readFileSync(input, 'utf8')
.replace(/\.\/integration\.js/g, integrationJs);
$$.file('frame.html', html, {src: true})
.pipe(gulp.dest('dist.3p/' + internalRuntimeVersion))
.on('end', function() {
var aliasToLatestBuild = 'dist.3p/current-min';
if (fs.existsSync(aliasToLatestBuild)) {
fs.unlinkSync(aliasToLatestBuild);
}
fs.symlinkSync(
'./' + internalRuntimeVersion,
aliasToLatestBuild,
'dir');
});
}
var activeBundleOperationCount = 0;
/**
* Synchronously concatenates the given files into the given destination
*
* @param {string} destFilePath File path to write the concatenated files to
* @param {Array<string>} files List of file paths to concatenate
*/
function concatFiles(destFilePath, files) {
var all = files.map(function(filePath) {
return fs.readFileSync(filePath, 'utf-8');
});
fs.writeFileSync(destFilePath, all.join(';'), 'utf-8');
}
/**
* Allows (ap|pre)pending to the already compiled, minified JS file
*
* @param {string} srcFilename Name of the JS source file
* @param {string} destFilePath File path to the compiled JS file
*/
function appendToCompiledFile(srcFilename, destFilePath) {
if (srcFilename == 'amp-viz-vega.js') {
// Prepend minified d3 and vega third_party to compiled amp-viz-vega.js
concatFiles(destFilePath, [
'third_party/d3/d3.js',
'third_party/d3-geo-projection/d3-geo-projection.js',
'third_party/vega/vega.js',
destFilePath,
]);
}
}
/**
* 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 || {};
if (options.minify) {
function minify() {
console.log('Minifying ' + srcFilename);
closureCompile(srcDir + srcFilename, destDir, options.minifiedName,
options)
.then(function() {
appendToCompiledFile(srcFilename, destDir + '/' + options.minifiedName);
fs.writeFileSync(destDir + '/version.txt', internalRuntimeVersion);
if (options.latestName) {
fs.copySync(
destDir + '/' + options.minifiedName,
destDir + '/' + options.latestName);
}
});
}
minify();
return;
}
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);
var destFilename = options.toName || srcFilename;
function rebundle() {
activeBundleOperationCount++;
bundler.bundle()
.on('error', function(err) {
activeBundleOperationCount--;
if (err instanceof SyntaxError) {
console.error($$.util.colors.red('Syntax error:', err.message));
} else {
console.error($$.util.colors.red(err.message));
}
})
.pipe(lazybuild())
.pipe($$.rename(destFilename))
.pipe(lazywrite())
.on('end', function() {
appendToCompiledFile(srcFilename, destDir + '/' + destFilename);
$$.util.log('Compiled ' + srcFilename);
activeBundleOperationCount--;
if (activeBundleOperationCount == 0) {
$$.util.log($$.util.colors.green('All current JS updates done.'));
}
});
}
if (options.watch) {
bundler.on('update', function() {
$$.util.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');
});
}
rebundle();
}
/**
* Build all the AMP experiments.html/js.
*
* @param {!Object} options
*/
function buildExperiments(options) {
options = options || {};
$$.util.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;
$$.watch(path + '/*', function() {
buildExperiments(copy);
});
}
// Build HTML.
$$.util.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,
includePolyfills: true,
minifiedName: minifiedName,
preventRemoveAndMakeDir: options.preventRemoveAndMakeDir,
checkTypes: options.checkTypes,
});
});
}
/**
* 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 || {};
$$.util.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;
$$.watch(path + '/*', function() {
buildLoginDoneVersion(version, copy);
});
}
// Build HTML.
$$.util.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');
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,
includePolyfills: true,
minify: options.minify || argv.minify,
minifiedName: minifiedName,
preventRemoveAndMakeDir: options.preventRemoveAndMakeDir,
latestName: latestName,
});
});
}
/**
* Build ALP JS
*
* @param {!Object} options
*/
function buildAlp(options) {
options = options || {};
$$.util.log('Bundling alp.js');
compileJs('./ads/alp/', 'install-alp.js', './dist/', {
toName: 'alp.max.js',
watch: options.watch,
minify: options.minify || argv.minify,
includePolyfills: true,
minifiedName: 'alp.js',
preventRemoveAndMakeDir: options.preventRemoveAndMakeDir,
});
}
/**
* Build ALP JS
*
* @param {!Object} options
*/
function buildSw(options) {
console.log('Bundling service-worker.js');
var opts = {};
for (var prop in options) {
opts[prop] = options[prop];
}
// The service-worker script loaded by the browser.
compileJs('./src/service-worker/', 'shell.js', './dist/', {
toName: 'sw.max.js',
minifiedName: 'sw.js',
watch: opts.watch,
minify: opts.minify || argv.minify,
preventRemoveAndMakeDir: opts.preventRemoveAndMakeDir,
});
// The service-worker kill script that may be loaded by the browser.
compileJs('./src/service-worker/', 'kill.js', './dist/', {
toName: 'sw-kill.max.js',
minifiedName: 'sw-kill.js',
watch: opts.watch,
minify: opts.minify || argv.minify,
preventRemoveAndMakeDir: opts.preventRemoveAndMakeDir,
});
// The script imported by the service-worker. This is the "core".
opts.noWrapper = true;
opts.filename = 'core.js';
buildExtensionJs('./src/service-worker', 'cache-service-worker', '0.1', opts);
}
/**
* 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) {
$$.util.log('Please run AMP with node.js version 4 or newer.');
$$.util.log('Your version is', process.version);
process.exit(1);
}
}
function mkdirSync(path) {
try {
fs.mkdirSync(path);
} catch(e) {
if (e.code != 'EEXIST') {
throw e;
}
}
}
/**
* Gulp tasks
*/
gulp.task('build', 'Builds the AMP library', build);
gulp.task('check-types', 'Check JS types', checkTypes);
gulp.task('css', 'Recompile css to build directory', compileCss);
gulp.task('default', 'Same as "watch"', ['watch', 'serve']);
gulp.task('dist', 'Build production binaries', dist, {
options: {
pseudo_names: 'Compiles with readable names. ' +
'Great for profiling and debugging production code.',
fortesting: 'Compiles with `getMode().test` set to true',
}
});
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);