Skip to content

Commit

Permalink
[WIP] Working-an-in-it-works closure compiler based compilation for AMP.
Browse files Browse the repository at this point in the history
- Currently a 15K win on main binary and likely much faster initial JS compilation due to simpler code.
- Includes a compiled compiler binary while we wait for a patch to land that is needed to compiler core-js.
- Doesn't yet activate (and benefit in code size) from type checking.
  • Loading branch information
cramforce committed Dec 24, 2015
1 parent 3016e63 commit 18b737f
Show file tree
Hide file tree
Showing 14 changed files with 331 additions and 10 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ script:
- gulp build
- gulp dist
- gulp presubmit
- gulp compile
# Unit tests with Travis' default chromium
- gulp test
# Integration tests with all saucelabs browsers
Expand Down
90 changes: 90 additions & 0 deletions build-system/tasks/compile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* 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.
*/

var closureCompiler = require('gulp-closure-compiler');
var gulp = require('gulp');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');

// Compiles AMP with the closure compiler. This is intended only for
// production use. During development we intent to continue using
// babel, as it has much faster incremental compilation.
// This currently only works for the main AMP JS binary, but should be
// straight forward to extend to the rest of our code base.
gulp.task('compile', function() {
/*eslint "google-camelcase/google-camelcase": 0*/
return gulp.src([
'ads/**/*.js',
'extensions/**/*.js',
'build/css.js',
'src/**/*.js',
// We do not want to load the entry point that loads the babel helpers.
'!src/amp-babel.js',
'builtins/**.js',
'third_party/caja/html-sanitizer.js',
'third_party/closure-library/sha384-generated.js',
'third_party/mustache/**/*.js',
'node_modules/document-register-element/build/' +
'document-register-element.max.js',
'node_modules/core-js/modules/**.js',
// Not sure what these files are, but they seem to duplicate code
// one level below and confuse the compiler.
'!node_modules/core-js/modules/library/**.js',
// Don't include tests.
'!**_test.js',
'!**/test-*.js',
])
.pipe(closureCompiler({
// Temporary shipping with our own compiler that has a single patch
// applied
compilerPath: 'third_party/closure-compiler/compiler.jar',
fileName: 'build/cc-amp.js',
compilerFlags: {
// Transpile from ES6 to ES5.
language_in: 'ECMASCRIPT6',
language_out: 'ECMASCRIPT5',
js_module_root: 'node_modules/',
common_js_entry_module: 'src/amp.js',
process_common_js_modules: true,
// This strips all files from the input set that aren't explicitly
// required.
only_closure_dependencies: true,
output_wrapper: '(function(){var process={env:{}};%output%})();'
}
}))
.on('error', function(err) {
if (/0 error\(s\)/.test(err.message)) {
// emit warning
console./*OK*/warn(err.message);
this.emit('end');
} else {
throw err;
}
})
.on('end', function() {
console./*OK*/log('Minify closure compiler result');
// Somewhat ironically we use uglify to further minify the result.
// This is needed because we currently use only very basic optimizations
// in closure compiler and it doesn't minify global variables at this
// stage.
return gulp.src(['build/cc-amp.js'])
.pipe(uglify({
preserveComments: 'some'
}))
.pipe(rename('cc-v0.js'))
.pipe(gulp.dest('dist'))
});
});
1 change: 1 addition & 0 deletions build-system/tasks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require('./babel-helpers');
require('./changelog');
require('./clean');
require('./compile');
require('./lint');
require('./make-golden');
require('./presubmit-checks');
Expand Down
6 changes: 5 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ function polyfillsForTests() {
*/
function compile(watch, shouldMinify) {
compileCss();
compileJs('./src/', 'amp.js', './dist', {
// 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',
watch: watch,
minify: shouldMinify,
Expand Down Expand Up @@ -418,6 +421,7 @@ function compileJs(srcDir, srcFilename, destDir, options) {
bundler.bundle()
.on('error', function(err) { console.error(err); this.emit('end'); })
.pipe(lazybuild())
.pipe(rename(options.toName || srcFilename))
.pipe(lazywrite());
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"finalhandler": "0.4.0",
"fs-extra": "0.26.0",
"gulp": "3.9.0",
"gulp-closure-compiler": "^0.4.0",
"gulp-eslint": "1.1.0",
"gulp-exec": "2.1.2",
"gulp-file": "0.2.0",
Expand Down
21 changes: 21 additions & 0 deletions src/amp-babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 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.
*/

// Entry point into AMP for compilation with babel. Just loads amp.js and
// Babel's helpers.

import '../third_party/babel/custom-babel-helpers';
import './amp';
2 changes: 1 addition & 1 deletion src/amp.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {installStyles, makeBodyVisible} from './styles';
import {installErrorReporting} from './error';
import {stubElements} from './custom-element';
import {adopt} from './runtime';
import {cssText} from '../build/css.js';
import {cssText} from '../build/css';
import {action} from './action';
import {maybeValidate} from './validator-integration';

Expand Down
2 changes: 1 addition & 1 deletion src/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


import {getMode} from './mode';
import {exponentialBackoff} from './exponential-backoff.js';
import {exponentialBackoff} from './exponential-backoff';
import {makeBodyVisible} from './styles';

const globalExponentialBackoff = exponentialBackoff(1.5);
Expand Down
6 changes: 0 additions & 6 deletions src/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,5 @@
* limitations under the License.
*/

/**
* @fileoverview Loads all polyfills needed by the AMP runtime.
*/


import 'document-register-element/build/document-register-element.max';
import '../third_party/babel/custom-babel-helpers';
import './custom-core-js-shim';
1 change: 1 addition & 0 deletions test/_init_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

// This must load before all other tests.
import '../third_party/babel/custom-babel-helpers';
import '../src/polyfills';
import {adopt} from '../src/runtime';

Expand Down
1 change: 0 additions & 1 deletion testing/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/


require('../src/polyfills');
import {Timer} from '../src/timer';
import {installCoreServices} from '../src/amp-core-service';
import {registerForUnitTest} from '../src/runtime';
Expand Down
Loading

0 comments on commit 18b737f

Please sign in to comment.