From 5958a00ce62aceb1d7bbf69bf72ccf2abcfbe8f9 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Thu, 7 Sep 2023 22:54:50 -0700 Subject: [PATCH] perf: add dynamically loaded cache-dependencies module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- __tests__/utils.test.ts | 2 +- dist/setup/index.js | 107 ++++++++++++++++++++++++++------------ src/cache-dependencies.ts | 41 +++++++++++++++ src/setup-python.ts | 16 +----- src/utils.ts | 26 --------- 5 files changed, 119 insertions(+), 73 deletions(-) create mode 100644 src/cache-dependencies.ts diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 40ef2f6..2ef0c61 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -8,11 +8,11 @@ import path from 'path'; import { validateVersion, validatePythonVersionFormatForPyPy, - isCacheFeatureAvailable, getVersionInputFromFile, getVersionInputFromPlainFile, getVersionInputFromTomlFile } from '../src/utils'; +import {isCacheFeatureAvailable} from '../src/cache-dependencies'; jest.mock('@actions/cache'); jest.mock('@actions/core'); diff --git a/dist/setup/index.js b/dist/setup/index.js index ef8220c..8390386 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -54782,7 +54782,7 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau var Stream = _interopDefault(__nccwpck_require__(2781)); var http = _interopDefault(__nccwpck_require__(3685)); var Url = _interopDefault(__nccwpck_require__(7310)); -var whatwgUrl = _interopDefault(__nccwpck_require__(3323)); +var whatwgUrl = _interopDefault(__nccwpck_require__(629)); var https = _interopDefault(__nccwpck_require__(5687)); var zlib = _interopDefault(__nccwpck_require__(9796)); @@ -57284,7 +57284,7 @@ module.exports = { /***/ }), -/***/ 3323: +/***/ 629: /***/ ((__unused_webpack_module, exports, __nccwpck_require__) => { "use strict"; @@ -61700,7 +61700,7 @@ const minVersion = __nccwpck_require__(4179) const validRange = __nccwpck_require__(2098) const outside = __nccwpck_require__(420) const gtr = __nccwpck_require__(9380) -const ltr = __nccwpck_require__(8726) +const ltr = __nccwpck_require__(3323) const intersects = __nccwpck_require__(7008) const simplifyRange = __nccwpck_require__(5297) const subset = __nccwpck_require__(7863) @@ -62085,7 +62085,7 @@ module.exports = intersects /***/ }), -/***/ 8726: +/***/ 3323: /***/ ((module, __unused_webpack_exports, __nccwpck_require__) => { const outside = __nccwpck_require__(420) @@ -68605,6 +68605,74 @@ try { } catch (er) {} +/***/ }), + +/***/ 1852: +/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) { + +"use strict"; + +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.isCacheFeatureAvailable = exports.cacheDependencies = void 0; +const cache = __importStar(__nccwpck_require__(7799)); +const core = __importStar(__nccwpck_require__(2186)); +const cache_factory_1 = __nccwpck_require__(7549); +function cacheDependencies(cache, pythonVersion) { + return __awaiter(this, void 0, void 0, function* () { + if (isCacheFeatureAvailable()) { + const cacheDependencyPath = core.getInput('cache-dependency-path') || undefined; + const cacheDistributor = cache_factory_1.getCacheDistributor(cache, pythonVersion, cacheDependencyPath); + yield cacheDistributor.restoreCache(); + } + }); +} +exports.cacheDependencies = cacheDependencies; +function isGhes() { + const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); + return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'; +} +function isCacheFeatureAvailable() { + 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; +} +exports.isCacheFeatureAvailable = isCacheFeatureAvailable; + + /***/ }), /***/ 8953: @@ -69807,18 +69875,10 @@ const finderPyPy = __importStar(__nccwpck_require__(4003)); const path = __importStar(__nccwpck_require__(1017)); const os = __importStar(__nccwpck_require__(2037)); const fs_1 = __importDefault(__nccwpck_require__(7147)); -const cache_factory_1 = __nccwpck_require__(7549); const utils_1 = __nccwpck_require__(1314); function isPyPyVersion(versionSpec) { return versionSpec.startsWith('pypy'); } -function cacheDependencies(cache, pythonVersion) { - return __awaiter(this, void 0, void 0, function* () { - const cacheDependencyPath = core.getInput('cache-dependency-path') || undefined; - const cacheDistributor = cache_factory_1.getCacheDistributor(cache, pythonVersion, cacheDependencyPath); - yield cacheDistributor.restoreCache(); - }); -} function resolveVersionInputFromDefaultFile() { const couples = [ ['.python-version', utils_1.getVersionInputFromPlainFile] @@ -69891,7 +69951,8 @@ function run() { } core.endGroup(); const cache = core.getInput('cache'); - if (cache && utils_1.isCacheFeatureAvailable()) { + if (cache) { + const { cacheDependencies } = yield Promise.resolve().then(() => __importStar(__nccwpck_require__(1852))); yield cacheDependencies(cache, pythonVersion); } } @@ -69948,9 +70009,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.getVersionInputFromFile = exports.getVersionInputFromPlainFile = exports.getVersionInputFromTomlFile = exports.getOSInfo = exports.getLinuxInfo = exports.logWarning = exports.isCacheFeatureAvailable = exports.isGhes = exports.validatePythonVersionFormatForPyPy = exports.writeExactPyPyVersionFile = exports.readExactPyPyVersionFile = exports.getPyPyVersionFromPath = exports.isNightlyKeyword = exports.validateVersion = exports.createSymlinkInFolder = exports.WINDOWS_PLATFORMS = exports.WINDOWS_ARCHS = exports.IS_MAC = exports.IS_LINUX = exports.IS_WINDOWS = void 0; +exports.getVersionInputFromFile = exports.getVersionInputFromPlainFile = exports.getVersionInputFromTomlFile = exports.getOSInfo = exports.getLinuxInfo = exports.logWarning = exports.validatePythonVersionFormatForPyPy = exports.writeExactPyPyVersionFile = exports.readExactPyPyVersionFile = exports.getPyPyVersionFromPath = exports.isNightlyKeyword = exports.validateVersion = exports.createSymlinkInFolder = exports.WINDOWS_PLATFORMS = exports.WINDOWS_ARCHS = exports.IS_MAC = exports.IS_LINUX = exports.IS_WINDOWS = void 0; /* eslint no-unsafe-finally: "off" */ -const cache = __importStar(__nccwpck_require__(7799)); const core = __importStar(__nccwpck_require__(2186)); const fs_1 = __importDefault(__nccwpck_require__(7147)); const path = __importStar(__nccwpck_require__(1017)); @@ -70023,23 +70083,6 @@ function validatePythonVersionFormatForPyPy(version) { return re.test(version); } exports.validatePythonVersionFormatForPyPy = validatePythonVersionFormatForPyPy; -function isGhes() { - const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com'); - return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM'; -} -exports.isGhes = isGhes; -function isCacheFeatureAvailable() { - 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; -} -exports.isCacheFeatureAvailable = isCacheFeatureAvailable; function logWarning(message) { const warningPrefix = '[warning]'; core.info(`${warningPrefix}${message}`); diff --git a/src/cache-dependencies.ts b/src/cache-dependencies.ts new file mode 100644 index 0000000..7335f07 --- /dev/null +++ b/src/cache-dependencies.ts @@ -0,0 +1,41 @@ +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()) { + const cacheDependencyPath = + core.getInput('cache-dependency-path') || undefined; + const cacheDistributor = getCacheDistributor( + cache, + pythonVersion, + cacheDependencyPath + ); + await cacheDistributor.restoreCache(); + } +} + +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; +} diff --git a/src/setup-python.ts b/src/setup-python.ts index 88ffc10..86f889a 100644 --- a/src/setup-python.ts +++ b/src/setup-python.ts @@ -4,9 +4,7 @@ import * as finderPyPy from './find-pypy'; import * as path from 'path'; import * as os from 'os'; import fs from 'fs'; -import {getCacheDistributor} from './cache-distributions/cache-factory'; import { - isCacheFeatureAvailable, logWarning, IS_MAC, getVersionInputFromFile, @@ -17,17 +15,6 @@ function isPyPyVersion(versionSpec: string) { return versionSpec.startsWith('pypy'); } -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[] { const couples: [string, (versionFile: string) => string[]][] = [ ['.python-version', getVersionInputFromPlainFile] @@ -125,7 +112,8 @@ async function run() { } core.endGroup(); const cache = core.getInput('cache'); - if (cache && isCacheFeatureAvailable()) { + if (cache) { + const {cacheDependencies} = await import('./cache-dependencies'); await cacheDependencies(cache, pythonVersion); } } else { diff --git a/src/utils.ts b/src/utils.ts index 5f000c1..99c944f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,4 @@ /* eslint no-unsafe-finally: "off" */ -import * as cache from '@actions/cache'; import * as core from '@actions/core'; import fs from 'fs'; import * as path from 'path'; @@ -99,31 +98,6 @@ export function validatePythonVersionFormatForPyPy(version: string) { 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 { const warningPrefix = '[warning]'; core.info(`${warningPrefix}${message}`);