Skip to content

Commit

Permalink
Speed up amp task loading by lazy-requiring large dependencies (#33376
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rsimha committed Mar 19, 2021
1 parent 2fb01d1 commit c920333
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 18 deletions.
13 changes: 7 additions & 6 deletions build-system/compile/closure-compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,26 @@ const compiler = require('@ampproject/google-closure-compiler');
const vinylFs = require('vinyl-fs');
const {cyan, red, yellow} = require('kleur/colors');
const {getBabelCacheDir} = require('./pre-closure-babel');
const {highlight} = require('cli-highlight');
const {log, logWithoutTimestamp} = require('../common/logging');

/**
* Logs a closure compiler error message after formatting it into a more
* readable form by dropping the plugin's logging prefix, normalizing paths,
* emphasizing errors and warnings, and syntax highlighting the error text.
* Logs a closure compiler error message after syntax highlighting it and then
* formatting it into a more readable form by dropping the plugin's logging
* prefix, normalizing paths, and emphasizing errors and warnings.
* @param {string} message
*/
function logClosureCompilerError(message) {
log(red('ERROR:'));
const babelCacheDir = `${getBabelCacheDir()}/`;
const loggingPrefix = /^.*?gulp-google-closure-compiler.*?: /;
const formattedMessage = message
const highlight = require('cli-highlight'); // Lazy-required to speed up task loading.
const highlightedMessage = highlight(message, {ignoreIllegals: true});
const formattedMessage = highlightedMessage
.replace(loggingPrefix, '')
.replace(new RegExp(babelCacheDir, 'g'), '')
.replace(/ ERROR /g, red(' ERROR '))
.replace(/ WARNING /g, yellow(' WARNING '));
logWithoutTimestamp(highlight(formattedMessage, {ignoreIllegals: true}));
logWithoutTimestamp(formattedMessage);
}

/**
Expand Down
11 changes: 7 additions & 4 deletions build-system/compile/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

const fs = require('fs-extra');
const path = require('path');
const ts = require('typescript');
/** @type {*} */
const tsickle = require('tsickle');
const {log} = require('../common/logging');
const {red} = require('kleur/colors');

Expand All @@ -31,7 +28,9 @@ const {red} = require('kleur/colors');
* @param {string} srcFilename
* @return {!Promise}
*/
exports.transpileTs = async function (srcDir, srcFilename) {
async function transpileTs(srcDir, srcFilename) {
const ts = require('typescript'); // Lazy-required to speed up task loading.
const tsickle = require('tsickle'); // Lazy-required to speed up task loading.
const tsEntry = path.join(srcDir, srcFilename).replace(/\.js$/, '.ts');
const tsConfig = ts.convertCompilerOptionsFromJson(
{
Expand Down Expand Up @@ -86,4 +85,8 @@ exports.transpileTs = async function (srcDir, srcFilename) {
if (diagnostics.length) {
log(red('TSickle:'), tsickle.formatDiagnostics(diagnostics));
}
}

module.exports = {
transpileTs,
};
5 changes: 3 additions & 2 deletions build-system/tasks/bundle-size/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ const {cyan, red, yellow} = require('kleur/colors');
const {log, logWithoutTimestamp} = require('../../common/logging');
const {report, NoTTYReport} = require('@ampproject/filesize');

const requestPost = util.promisify(require('request').post);

const filesizeConfigPath = require.resolve('./filesize.json');
const fileGlobs = require(filesizeConfigPath).filesize.track;
const normalizedRtvNumber = '1234567890123';
Expand Down Expand Up @@ -117,6 +115,7 @@ async function storeBundleSize() {
const commitHash = gitCommitHash();
log('Storing bundle sizes for commit', cyan(shortSha(commitHash)) + '...');
try {
const requestPost = util.promisify(require('request').post); // Lazy-required to speed up task loading.
const response = await requestPost({
uri: url.resolve(
bundleSizeAppBaseUrl,
Expand Down Expand Up @@ -147,6 +146,7 @@ async function skipBundleSize() {
cyan(shortSha(commitHash)) + '...'
);
try {
const requestPost = util.promisify(require('request').post); // Lazy-required to speed up task loading.
const response = await requestPost(
url.resolve(
bundleSizeAppBaseUrl,
Expand Down Expand Up @@ -184,6 +184,7 @@ async function reportBundleSize() {
cyan(shortSha(mergeSha)) + '...'
);
try {
const requestPost = util.promisify(require('request').post); // Lazy-required to speed up task loading.
const response = await requestPost({
uri: url.resolve(
bundleSizeAppBaseUrl,
Expand Down
7 changes: 4 additions & 3 deletions build-system/tasks/css/jsify-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
'use strict';

const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const fs = require('fs-extra');
const postcss = require('postcss');
Expand All @@ -24,7 +23,7 @@ const {log} = require('../../common/logging');
const {red} = require('kleur/colors');

// NOTE: see https://github.com/ai/browserslist#queries for `browsers` list
const cssprefixer = autoprefixer({
const browsersList = {
overrideBrowserslist: [
'last 5 ChromeAndroid versions',
'last 5 iOS versions',
Expand All @@ -34,7 +33,7 @@ const cssprefixer = autoprefixer({
'last 2 OperaMobile versions',
'last 2 OperaMini versions',
],
});
};

const cssNanoDefaultOptions = {
autoprefixer: false,
Expand Down Expand Up @@ -71,6 +70,8 @@ function transformCss(cssStr, opt_cssnano, opt_filename) {
opt_cssnano
);
const cssnanoTransformer = cssnano({preset: ['default', cssnanoOptions]});
const autoprefixer = require('autoprefixer'); // Lazy-required to speed up task loading.
const cssprefixer = autoprefixer(browsersList);
const transformers = [postcssImport, cssprefixer, cssnanoTransformer];
return postcss.default(transformers).process(cssStr, {
'from': opt_filename,
Expand Down
2 changes: 1 addition & 1 deletion build-system/tasks/performance/copy-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

const fs = require('fs');
const {CONTROL, maybeCopyImageToCache, urlToCachePath} = require('./helpers');
const {JSDOM} = require('jsdom');

/**
* Lookup URL from cache. Inspect tags that could use images.
Expand All @@ -26,6 +25,7 @@ const {JSDOM} = require('jsdom');
function copyImagesFromTags(url) {
const cachePath = urlToCachePath(url, CONTROL);
const document = fs.readFileSync(cachePath);
const {JSDOM} = require('jsdom'); // Lazy-required to speed up task loading.
const dom = new JSDOM(document);

copyImagesFromAmpImg(url, dom);
Expand Down
2 changes: 1 addition & 1 deletion build-system/tasks/performance/rewrite-analytics-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const {
getLocalVendorConfig,
urlToCachePath,
} = require('./helpers');
const {JSDOM} = require('jsdom');

/**
* Return local vendor config.
Expand Down Expand Up @@ -65,6 +64,7 @@ async function maybeMergeAndRemoveVendorConfig(tag, script) {
async function alterAnalyticsTags(url, version, extraUrlParams) {
const cachePath = urlToCachePath(url, version);
const document = fs.readFileSync(cachePath);
const {JSDOM} = require('jsdom'); // Lazy-required to speed up task loading.
const dom = new JSDOM(document);

const analyticsTags = Array.from(
Expand Down
3 changes: 2 additions & 1 deletion build-system/tasks/performance/rewrite-script-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const {
getLocalPathFromExtension,
urlToCachePath,
} = require('./helpers');
const {JSDOM} = require('jsdom');

/**
* Lookup URL from cache and rewrite URLs to build from working branch
Expand All @@ -38,6 +37,7 @@ const {JSDOM} = require('jsdom');
async function useLocalScripts(url) {
const cachePath = urlToCachePath(url, EXPERIMENT);
const document = fs.readFileSync(cachePath);
const {JSDOM} = require('jsdom'); // Lazy-required to speed up task loading.
const dom = new JSDOM(document);

const scripts = Array.from(dom.window.document.querySelectorAll('script'));
Expand Down Expand Up @@ -68,6 +68,7 @@ async function useLocalScripts(url) {
async function useRemoteScripts(url) {
const cachePath = urlToCachePath(url, CONTROL);
const document = fs.readFileSync(cachePath);
const {JSDOM} = require('jsdom'); // Lazy-required to speed up task loading.
const dom = new JSDOM(document);

const scripts = Array.from(dom.window.document.querySelectorAll('script'));
Expand Down

0 comments on commit c920333

Please sign in to comment.