perf: add dynamically loaded cache-dependencies module

This reduces the main bundle size by ⚠️  1.1 MB minified! This bundle is loaded only when caching is enabled.

The reason for this huge reduction is that caching dependencies uses the `@actions/cache` package, which is an extremely large package with big dependencies.

`setup-python` is used in `setup-cpp` as a library. This optimization reduces the bundle size for that package as well.
This commit is contained in:
Amin Yahyaabadi 2023-09-07 22:54:50 -07:00
parent 70dcb22d26
commit 9700887518
No known key found for this signature in database
GPG Key ID: F52AF77F636088F0
6 changed files with 76155 additions and 99283 deletions

View File

@ -8,7 +8,6 @@ import path from 'path';
import { import {
validateVersion, validateVersion,
validatePythonVersionFormatForPyPy, validatePythonVersionFormatForPyPy,
isCacheFeatureAvailable,
getVersionInputFromFile, getVersionInputFromFile,
getVersionInputFromPlainFile, getVersionInputFromPlainFile,
getVersionInputFromTomlFile, getVersionInputFromTomlFile,
@ -16,6 +15,7 @@ import {
IS_WINDOWS, IS_WINDOWS,
getDownloadFileName getDownloadFileName
} from '../src/utils'; } from '../src/utils';
import {isCacheFeatureAvailable} from '../src/cache-dependencies';
jest.mock('@actions/cache'); jest.mock('@actions/cache');
jest.mock('@actions/core'); jest.mock('@actions/core');

97318
dist/cache-save/index.js vendored

File diff suppressed because one or more lines are too long

78034
dist/setup/index.js vendored

File diff suppressed because one or more lines are too long

42
src/cache-dependencies.ts Normal file
View File

@ -0,0 +1,42 @@
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import {getCacheDistributor} from './cache-distributions/cache-factory';
export async function cacheDependencies(cache: string, pythonVersion: string) {
if (!isCacheFeatureAvailable()) {
return;
}
const cacheDependencyPath =
core.getInput('cache-dependency-path') || undefined;
const cacheDistributor = getCacheDistributor(
cache,
pythonVersion,
cacheDependencyPath
);
await cacheDistributor.restoreCache();
}
export function isGhes(): boolean {
const ghUrl = new URL(
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
);
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}
export function isCacheFeatureAvailable(): boolean {
if (cache.isFeatureAvailable()) {
return true;
}
if (isGhes()) {
core.warning(
'Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.'
);
return false;
}
core.warning(
'The runner was not able to contact the cache service. Caching will be skipped'
);
return false;
}

View File

@ -5,9 +5,7 @@ import * as finderGraalPy from './find-graalpy';
import * as path from 'path'; import * as path from 'path';
import * as os from 'os'; import * as os from 'os';
import fs from 'fs'; import fs from 'fs';
import {getCacheDistributor} from './cache-distributions/cache-factory';
import { import {
isCacheFeatureAvailable,
logWarning, logWarning,
IS_MAC, IS_MAC,
getVersionInputFromFile, getVersionInputFromFile,
@ -22,17 +20,6 @@ function isGraalPyVersion(versionSpec: string) {
return versionSpec.startsWith('graalpy'); return versionSpec.startsWith('graalpy');
} }
async function cacheDependencies(cache: string, pythonVersion: string) {
const cacheDependencyPath =
core.getInput('cache-dependency-path') || undefined;
const cacheDistributor = getCacheDistributor(
cache,
pythonVersion,
cacheDependencyPath
);
await cacheDistributor.restoreCache();
}
function resolveVersionInputFromDefaultFile(): string[] { function resolveVersionInputFromDefaultFile(): string[] {
const couples: [string, (versionFile: string) => string[]][] = [ const couples: [string, (versionFile: string) => string[]][] = [
['.python-version', getVersionInputFromPlainFile] ['.python-version', getVersionInputFromPlainFile]
@ -140,7 +127,8 @@ async function run() {
} }
core.endGroup(); core.endGroup();
const cache = core.getInput('cache'); const cache = core.getInput('cache');
if (cache && isCacheFeatureAvailable()) { if (cache) {
const {cacheDependencies} = await import('./cache-dependencies');
await cacheDependencies(cache, pythonVersion); await cacheDependencies(cache, pythonVersion);
} }
} else { } else {

View File

@ -1,5 +1,4 @@
/* eslint no-unsafe-finally: "off" */ /* eslint no-unsafe-finally: "off" */
import * as cache from '@actions/cache';
import * as core from '@actions/core'; import * as core from '@actions/core';
import fs from 'fs'; import fs from 'fs';
import * as path from 'path'; import * as path from 'path';
@ -112,31 +111,6 @@ export function validatePythonVersionFormatForPyPy(version: string) {
return re.test(version); return re.test(version);
} }
export function isGhes(): boolean {
const ghUrl = new URL(
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
);
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}
export function isCacheFeatureAvailable(): boolean {
if (cache.isFeatureAvailable()) {
return true;
}
if (isGhes()) {
core.warning(
'Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.'
);
return false;
}
core.warning(
'The runner was not able to contact the cache service. Caching will be skipped'
);
return false;
}
export function logWarning(message: string): void { export function logWarning(message: string): void {
const warningPrefix = '[warning]'; const warningPrefix = '[warning]';
core.info(`${warningPrefix}${message}`); core.info(`${warningPrefix}${message}`);