Skip to content

Commit

Permalink
🏗 Clean up / replace lazy require statements in build-system/ (#33385)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsimha committed Mar 19, 2021
1 parent 8b4a5f8 commit ceb972b
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 136 deletions.
92 changes: 0 additions & 92 deletions build-system/compile/typescript.js

This file was deleted.

4 changes: 2 additions & 2 deletions build-system/tasks/amp-task-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ function startAtRepoRoot() {
*/
async function runTask(taskName, taskFunc) {
startAtRepoRoot();
log('Using task file', magenta('amphtml/amp.js'));
log('Using task file', magenta(path.join('amphtml', 'amp.js')));
const start = Date.now();
try {
log(`Starting '${cyan(taskName)}'...`);
await taskFunc();
log('Finished', `'${cyan(taskName)}'`, 'after', magenta(getTime(start)));
} catch (err) {
log(cyan(taskName), red('errored after'), magenta(getTime(start)));
log(`'${cyan(taskName)}'`, red('errored after'), magenta(getTime(start)));
log(err);
process.exit(1);
}
Expand Down
14 changes: 11 additions & 3 deletions build-system/tasks/bundle-size/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ function checkResponse(response, ...successMessages) {
}
}

/**
* Helper that lazily imports and promisifies request.post, and invokes it with
* the given args.
* @param {*} args
* @return {function()}
*/
async function requestPost(...args) {
const {default: request} = await import('request');
return util.promisify(request.post)(...args);
}

/**
* Store the bundle sizes for a commit hash in the build artifacts storage
* repository to the passed value.
Expand Down Expand Up @@ -115,7 +126,6 @@ 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 @@ -146,7 +156,6 @@ 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,7 +193,6 @@ 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
4 changes: 2 additions & 2 deletions build-system/tasks/css/jsify-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const cssNanoDefaultOptions = {
* @return {!Promise<postcss.Result>} that resolves with the css content after
* processing
*/
function transformCss(cssStr, opt_cssnano, opt_filename) {
async function transformCss(cssStr, opt_cssnano, opt_filename) {
opt_cssnano = opt_cssnano || Object.create(null);
// See http://cssnano.co/optimisations/ for full list.
// We try and turn off any optimization that is marked unsafe.
Expand All @@ -70,7 +70,7 @@ 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 {default: autoprefixer} = await import('autoprefixer'); // Lazy-imported to speed up task loading.
const cssprefixer = autoprefixer(browsersList);
const transformers = [postcssImport, cssprefixer, cssnanoTransformer];
return postcss.default(transformers).process(cssStr, {
Expand Down
22 changes: 0 additions & 22 deletions build-system/tasks/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

const argv = require('minimist')(process.argv.slice(2));
const debounce = require('debounce');
const del = require('del');
const esbuild = require('esbuild');
/** @type {Object} */
const experimentDefines = require('../global-configs/experiments-const.json');
Expand All @@ -39,7 +38,6 @@ const {jsBundles} = require('../compile/bundles.config');
const {log, logLocalDev} = require('../common/logging');
const {removeFromClosureBabelCache} = require('../compile/pre-closure-babel');
const {thirdPartyFrames} = require('../test-configs/config');
const {transpileTs} = require('../compile/typescript');
const {watch: fileWatch} = require('chokidar');

/** @type {Remapping.default} */
Expand Down Expand Up @@ -643,25 +641,6 @@ async function minifyWithTerser(destDir, destFilename, options) {
fs.writeFileSync(`${filename}.map`, remapped.toString());
}

/**
* Transpiles from TypeScript into intermediary files before compilation and
* deletes them afterwards.
*
* @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
* @return {!Promise}
*/
async function compileTs(srcDir, srcFilename, destDir, options) {
options = options || {};
const startTime = Date.now();
await transpileTs(srcDir, srcFilename);
endBuildStep('Transpiled', srcFilename, startTime);
await compileJs(srcDir, srcFilename, destDir, options);
del.sync(path.join(srcDir, '**/*.js'));
}

/**
* Bundles (max) or compiles (min) a given JavaScript file entry point.
*
Expand Down Expand Up @@ -847,7 +826,6 @@ module.exports = {
compileCoreRuntime,
compileJs,
compileJsWithEsbuild,
compileTs,
doBuildJs,
endBuildStep,
maybePrintCoverageMessage,
Expand Down
10 changes: 6 additions & 4 deletions build-system/tasks/performance/copy-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ const {CONTROL, maybeCopyImageToCache, urlToCachePath} = require('./helpers');
* Lookup URL from cache. Inspect tags that could use images.
*
* @param {string} url
* @return {Promise<void>}
*/
function copyImagesFromTags(url) {
async 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 {JSDOM} = await import('jsdom'); // Lazy-imported to speed up task loading.
const dom = new JSDOM(document);

copyImagesFromAmpImg(url, dom);
Expand Down Expand Up @@ -79,9 +80,10 @@ function copyImagesFromAmpVideo(url, dom) {
* Copy locally stored images found in the markup to cache.
*
* @param {!Array<string>} urls
* @return {Promise<void>}
*/
function copyLocalImages(urls) {
urls.forEach(copyImagesFromTags);
async function copyLocalImages(urls) {
await Promise.all(urls.map(copyImagesFromTags));
}

module.exports = copyLocalImages;
2 changes: 1 addition & 1 deletion build-system/tasks/performance/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function performance() {
const urlsAndAdsUrls = urls.concat(config.adsUrls || []);
await cacheDocuments(urlsAndAdsUrls);
await compileScripts(urlsAndAdsUrls);
copyLocalImages(urlsAndAdsUrls);
await copyLocalImages(urlsAndAdsUrls);
await rewriteScriptTags(urlsAndAdsUrls);
await rewriteAnalyticsTags(config.handlers);
await getMetrics(urls, config);
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 @@ -64,7 +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 {JSDOM} = await import('jsdom'); // Lazy-imported to speed up task loading.
const dom = new JSDOM(document);

const analyticsTags = Array.from(
Expand Down
4 changes: 2 additions & 2 deletions build-system/tasks/performance/rewrite-script-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const {
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 {JSDOM} = await import('jsdom'); // Lazy-imported to speed up task loading.
const dom = new JSDOM(document);

const scripts = Array.from(dom.window.document.querySelectorAll('script'));
Expand Down Expand Up @@ -68,7 +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 {JSDOM} = await import('jsdom'); // Lazy-imported to speed up task loading.
const dom = new JSDOM(document);

const scripts = Array.from(dom.window.document.querySelectorAll('script'));
Expand Down
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@
"terser": "5.6.0",
"text-table": "0.2.0",
"traverse": "0.6.6",
"tsickle": "0.39.1",
"typescript": "4.2.3",
"util": "0.12.3",
"vinyl-fs": "3.0.3"
Expand Down

0 comments on commit ceb972b

Please sign in to comment.