Skip to content

Commit

Permalink
🏗 Reorganize browserify caching code (#32297)
Browse files Browse the repository at this point in the history
Co-authored-by: Jake Fried <samouri@users.noreply.github.com>
  • Loading branch information
rsimha and samouri committed Jan 29, 2021
1 parent f538b6c commit e9e76fd
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 48 deletions.
61 changes: 61 additions & 0 deletions build-system/common/browserify-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright 2021 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.
*/

const browserifyPersistFs = require('browserify-persist-fs');
const crypto = require('crypto');
const fs = require('fs-extra');
const globby = require('globby');
const {dotWrappingWidth} = require('./logging');

/**
* Used for persistent babel caching during tests. Created using a hash object
* that includes the repo package lockfile and various parts of the build-system
* so that the cache is invalidated if any of them changes, and files are
* retransformed.
* @return {function}
*/
function getPersistentBrowserifyCache() {
let wrapCounter = 0;
const createHash = (input) =>
crypto.createHash('sha1').update(input).digest('hex');
const hashObject = {
deps: createHash(fs.readFileSync('./package-lock.json')),
build: globby
.sync([
'build-system/**/*.js',
'!build-system/eslint-rules',
'!**/test/**',
])
.map((f) => createHash(fs.readFileSync(f))),
};
const logger = () => {
process.stdout.write('.');
if (++wrapCounter >= dotWrappingWidth) {
wrapCounter = 0;
process.stdout.write('\n');
}
};
const cache = browserifyPersistFs('.karma-cache', hashObject, logger);
cache.gc(
{maxAge: 1000 * 60 * 60 * 24 * 7}, // Refresh cache if more than a week old
() => {} // swallow errors
);
return cache;
}

module.exports = {
getPersistentBrowserifyCache,
};
9 changes: 9 additions & 0 deletions build-system/common/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ const log = require('fancy-log');
const {bold, yellow} = require('ansi-colors');
const {isCiBuild} = require('./ci');

/**
* Used by tests to wrap progress dots.
*/
const dotWrappingWidth = 150;

/**
* Used by CI job scripts to print a prefix before top-level logging lines.
*/
let loggingPrefix = '';

/**
Expand Down Expand Up @@ -89,6 +97,7 @@ function logWithoutTimestampLocalDev(...messages) {
}

module.exports = {
dotWrappingWidth,
getLoggingPrefix,
log,
logLocalDev,
Expand Down
5 changes: 4 additions & 1 deletion build-system/tasks/e2e/mocha-dots-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ const {
EVENT_TEST_PENDING,
} = Mocha.Runner.constants;
const {Base} = Mocha.reporters;
const {
icon,
nbDotsPerLine,
} = require('../../test-configs/karma.conf').superDotsReporter;
const {green, red, yellow} = require('ansi-colors');
const {icon, nbDotsPerLine} = require('../karma.conf').superDotsReporter;
const {reportTestFinished} = require('../report-test-status');

/**
Expand Down
2 changes: 1 addition & 1 deletion build-system/tasks/runtime-test/runtime-test-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'use strict';

const argv = require('minimist')(process.argv.slice(2));
const karmaConfig = require('../karma.conf');
const karmaConfig = require('../../test-configs/karma.conf');
const testConfig = require('../../test-configs/config');
const {
createCtrlcHandler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@
'use strict';

const argv = require('minimist')(process.argv.slice(2));
const browserifyPersistFs = require('browserify-persist-fs');
const crypto = require('crypto');
const fs = require('fs');
const globby = require('globby');
const {dotWrappingWidth} = require('../common/logging');
const {getPersistentBrowserifyCache} = require('../common/browserify-cache');
const {isCiBuild} = require('../common/ci');

const TEST_SERVER_PORT = 8081;
const DOT_WRAPPING_WIDTH = 150;
const COMMON_CHROME_FLAGS = [
// Dramatically speeds up iframe creation time.
'--disable-extensions',
Expand All @@ -33,44 +30,6 @@ const COMMON_CHROME_FLAGS = [
argv.debug ? '--auto-open-devtools-for-tabs' : null,
].filter(Boolean);

// Used by persistent browserify caching to further salt hashes with our
// environment state. Eg, when updating a babel-plugin, the environment hash
// must change somehow so that the cache busts and the file is retransformed.
const createHash = (input) =>
crypto.createHash('sha1').update(input).digest('hex');
let wrapCounter = 0;
const persistentCache = browserifyPersistFs(
'.karma-cache',
{
deps: createHash(fs.readFileSync('./package-lock.json')),
build: globby
.sync([
'build-system/**/*.js',
'!build-system/eslint-rules',
'!**/test/**',
])
.map((f) => {
return createHash(fs.readFileSync(f));
}),
},
() => {
process.stdout.write('.');
if (++wrapCounter >= DOT_WRAPPING_WIDTH) {
wrapCounter = 0;
process.stdout.write('\n');
}
}
);

persistentCache.gc(
{
maxAge: 1000 * 60 * 60 * 24 * 7,
},
() => {
// swallow errors
}
);

/**
* @param {!Object} config
*/
Expand Down Expand Up @@ -109,14 +68,13 @@ module.exports = {
transform: [['babelify', {caller: {name: 'test'}, global: true}]],
// Prevent "cannot find module" errors during CI. See #14166.
bundleDelay: isCiBuild() ? 5000 : 1200,

persistentCache,
persistentCache: getPersistentBrowserifyCache(),
},

reporters: ['super-dots', 'karmaSimpleReporter'],

superDotsReporter: {
nbDotsPerLine: DOT_WRAPPING_WIDTH,
nbDotsPerLine: dotWrappingWidth,
color: {
success: 'green',
failure: 'red',
Expand Down

0 comments on commit e9e76fd

Please sign in to comment.