Skip to content

Commit 5958a00

Browse files
committed
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.
1 parent 65d7f2d commit 5958a00

5 files changed

Lines changed: 119 additions & 73 deletions

File tree

__tests__/utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import path from 'path';
88
import {
99
validateVersion,
1010
validatePythonVersionFormatForPyPy,
11-
isCacheFeatureAvailable,
1211
getVersionInputFromFile,
1312
getVersionInputFromPlainFile,
1413
getVersionInputFromTomlFile
1514
} from '../src/utils';
15+
import {isCacheFeatureAvailable} from '../src/cache-dependencies';
1616

1717
jest.mock('@actions/cache');
1818
jest.mock('@actions/core');

dist/setup/index.js

Lines changed: 75 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -54782,7 +54782,7 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
5478254782
var Stream = _interopDefault(__nccwpck_require__(2781));
5478354783
var http = _interopDefault(__nccwpck_require__(3685));
5478454784
var Url = _interopDefault(__nccwpck_require__(7310));
54785-
var whatwgUrl = _interopDefault(__nccwpck_require__(3323));
54785+
var whatwgUrl = _interopDefault(__nccwpck_require__(629));
5478654786
var https = _interopDefault(__nccwpck_require__(5687));
5478754787
var zlib = _interopDefault(__nccwpck_require__(9796));
5478854788

@@ -57284,7 +57284,7 @@ module.exports = {
5728457284

5728557285
/***/ }),
5728657286

57287-
/***/ 3323:
57287+
/***/ 629:
5728857288
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {
5728957289

5729057290
"use strict";
@@ -61700,7 +61700,7 @@ const minVersion = __nccwpck_require__(4179)
6170061700
const validRange = __nccwpck_require__(2098)
6170161701
const outside = __nccwpck_require__(420)
6170261702
const gtr = __nccwpck_require__(9380)
61703-
const ltr = __nccwpck_require__(8726)
61703+
const ltr = __nccwpck_require__(3323)
6170461704
const intersects = __nccwpck_require__(7008)
6170561705
const simplifyRange = __nccwpck_require__(5297)
6170661706
const subset = __nccwpck_require__(7863)
@@ -62085,7 +62085,7 @@ module.exports = intersects
6208562085

6208662086
/***/ }),
6208762087

62088-
/***/ 8726:
62088+
/***/ 3323:
6208962089
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {
6209062090

6209162091
const outside = __nccwpck_require__(420)
@@ -68605,6 +68605,74 @@ try {
6860568605
} catch (er) {}
6860668606

6860768607

68608+
/***/ }),
68609+
68610+
/***/ 1852:
68611+
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
68612+
68613+
"use strict";
68614+
68615+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
68616+
if (k2 === undefined) k2 = k;
68617+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
68618+
}) : (function(o, m, k, k2) {
68619+
if (k2 === undefined) k2 = k;
68620+
o[k2] = m[k];
68621+
}));
68622+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
68623+
Object.defineProperty(o, "default", { enumerable: true, value: v });
68624+
}) : function(o, v) {
68625+
o["default"] = v;
68626+
});
68627+
var __importStar = (this && this.__importStar) || function (mod) {
68628+
if (mod && mod.__esModule) return mod;
68629+
var result = {};
68630+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
68631+
__setModuleDefault(result, mod);
68632+
return result;
68633+
};
68634+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
68635+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
68636+
return new (P || (P = Promise))(function (resolve, reject) {
68637+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
68638+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
68639+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
68640+
step((generator = generator.apply(thisArg, _arguments || [])).next());
68641+
});
68642+
};
68643+
Object.defineProperty(exports, "__esModule", ({ value: true }));
68644+
exports.isCacheFeatureAvailable = exports.cacheDependencies = void 0;
68645+
const cache = __importStar(__nccwpck_require__(7799));
68646+
const core = __importStar(__nccwpck_require__(2186));
68647+
const cache_factory_1 = __nccwpck_require__(7549);
68648+
function cacheDependencies(cache, pythonVersion) {
68649+
return __awaiter(this, void 0, void 0, function* () {
68650+
if (isCacheFeatureAvailable()) {
68651+
const cacheDependencyPath = core.getInput('cache-dependency-path') || undefined;
68652+
const cacheDistributor = cache_factory_1.getCacheDistributor(cache, pythonVersion, cacheDependencyPath);
68653+
yield cacheDistributor.restoreCache();
68654+
}
68655+
});
68656+
}
68657+
exports.cacheDependencies = cacheDependencies;
68658+
function isGhes() {
68659+
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
68660+
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
68661+
}
68662+
function isCacheFeatureAvailable() {
68663+
if (cache.isFeatureAvailable()) {
68664+
return true;
68665+
}
68666+
if (isGhes()) {
68667+
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.');
68668+
return false;
68669+
}
68670+
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
68671+
return false;
68672+
}
68673+
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
68674+
68675+
6860868676
/***/ }),
6860968677

6861068678
/***/ 8953:
@@ -69807,18 +69875,10 @@ const finderPyPy = __importStar(__nccwpck_require__(4003));
6980769875
const path = __importStar(__nccwpck_require__(1017));
6980869876
const os = __importStar(__nccwpck_require__(2037));
6980969877
const fs_1 = __importDefault(__nccwpck_require__(7147));
69810-
const cache_factory_1 = __nccwpck_require__(7549);
6981169878
const utils_1 = __nccwpck_require__(1314);
6981269879
function isPyPyVersion(versionSpec) {
6981369880
return versionSpec.startsWith('pypy');
6981469881
}
69815-
function cacheDependencies(cache, pythonVersion) {
69816-
return __awaiter(this, void 0, void 0, function* () {
69817-
const cacheDependencyPath = core.getInput('cache-dependency-path') || undefined;
69818-
const cacheDistributor = cache_factory_1.getCacheDistributor(cache, pythonVersion, cacheDependencyPath);
69819-
yield cacheDistributor.restoreCache();
69820-
});
69821-
}
6982269882
function resolveVersionInputFromDefaultFile() {
6982369883
const couples = [
6982469884
['.python-version', utils_1.getVersionInputFromPlainFile]
@@ -69891,7 +69951,8 @@ function run() {
6989169951
}
6989269952
core.endGroup();
6989369953
const cache = core.getInput('cache');
69894-
if (cache && utils_1.isCacheFeatureAvailable()) {
69954+
if (cache) {
69955+
const { cacheDependencies } = yield Promise.resolve().then(() => __importStar(__nccwpck_require__(1852)));
6989569956
yield cacheDependencies(cache, pythonVersion);
6989669957
}
6989769958
}
@@ -69948,9 +70009,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
6994870009
return (mod && mod.__esModule) ? mod : { "default": mod };
6994970010
};
6995070011
Object.defineProperty(exports, "__esModule", ({ value: true }));
69951-
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;
70012+
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;
6995270013
/* eslint no-unsafe-finally: "off" */
69953-
const cache = __importStar(__nccwpck_require__(7799));
6995470014
const core = __importStar(__nccwpck_require__(2186));
6995570015
const fs_1 = __importDefault(__nccwpck_require__(7147));
6995670016
const path = __importStar(__nccwpck_require__(1017));
@@ -70023,23 +70083,6 @@ function validatePythonVersionFormatForPyPy(version) {
7002370083
return re.test(version);
7002470084
}
7002570085
exports.validatePythonVersionFormatForPyPy = validatePythonVersionFormatForPyPy;
70026-
function isGhes() {
70027-
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
70028-
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
70029-
}
70030-
exports.isGhes = isGhes;
70031-
function isCacheFeatureAvailable() {
70032-
if (cache.isFeatureAvailable()) {
70033-
return true;
70034-
}
70035-
if (isGhes()) {
70036-
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.');
70037-
return false;
70038-
}
70039-
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
70040-
return false;
70041-
}
70042-
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
7004370086
function logWarning(message) {
7004470087
const warningPrefix = '[warning]';
7004570088
core.info(`${warningPrefix}${message}`);

src/cache-dependencies.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as cache from '@actions/cache';
2+
import * as core from '@actions/core';
3+
import {getCacheDistributor} from './cache-distributions/cache-factory';
4+
5+
export async function cacheDependencies(cache: string, pythonVersion: string) {
6+
if (isCacheFeatureAvailable()) {
7+
const cacheDependencyPath =
8+
core.getInput('cache-dependency-path') || undefined;
9+
const cacheDistributor = getCacheDistributor(
10+
cache,
11+
pythonVersion,
12+
cacheDependencyPath
13+
);
14+
await cacheDistributor.restoreCache();
15+
}
16+
}
17+
18+
function isGhes(): boolean {
19+
const ghUrl = new URL(
20+
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
21+
);
22+
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
23+
}
24+
25+
export function isCacheFeatureAvailable(): boolean {
26+
if (cache.isFeatureAvailable()) {
27+
return true;
28+
}
29+
30+
if (isGhes()) {
31+
core.warning(
32+
'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.'
33+
);
34+
return false;
35+
}
36+
37+
core.warning(
38+
'The runner was not able to contact the cache service. Caching will be skipped'
39+
);
40+
return false;
41+
}

src/setup-python.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import * as finderPyPy from './find-pypy';
44
import * as path from 'path';
55
import * as os from 'os';
66
import fs from 'fs';
7-
import {getCacheDistributor} from './cache-distributions/cache-factory';
87
import {
9-
isCacheFeatureAvailable,
108
logWarning,
119
IS_MAC,
1210
getVersionInputFromFile,
@@ -17,17 +15,6 @@ function isPyPyVersion(versionSpec: string) {
1715
return versionSpec.startsWith('pypy');
1816
}
1917

20-
async function cacheDependencies(cache: string, pythonVersion: string) {
21-
const cacheDependencyPath =
22-
core.getInput('cache-dependency-path') || undefined;
23-
const cacheDistributor = getCacheDistributor(
24-
cache,
25-
pythonVersion,
26-
cacheDependencyPath
27-
);
28-
await cacheDistributor.restoreCache();
29-
}
30-
3118
function resolveVersionInputFromDefaultFile(): string[] {
3219
const couples: [string, (versionFile: string) => string[]][] = [
3320
['.python-version', getVersionInputFromPlainFile]
@@ -125,7 +112,8 @@ async function run() {
125112
}
126113
core.endGroup();
127114
const cache = core.getInput('cache');
128-
if (cache && isCacheFeatureAvailable()) {
115+
if (cache) {
116+
const {cacheDependencies} = await import('./cache-dependencies');
129117
await cacheDependencies(cache, pythonVersion);
130118
}
131119
} else {

src/utils.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* eslint no-unsafe-finally: "off" */
2-
import * as cache from '@actions/cache';
32
import * as core from '@actions/core';
43
import fs from 'fs';
54
import * as path from 'path';
@@ -99,31 +98,6 @@ export function validatePythonVersionFormatForPyPy(version: string) {
9998
return re.test(version);
10099
}
101100

102-
export function isGhes(): boolean {
103-
const ghUrl = new URL(
104-
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
105-
);
106-
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
107-
}
108-
109-
export function isCacheFeatureAvailable(): boolean {
110-
if (cache.isFeatureAvailable()) {
111-
return true;
112-
}
113-
114-
if (isGhes()) {
115-
core.warning(
116-
'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.'
117-
);
118-
return false;
119-
}
120-
121-
core.warning(
122-
'The runner was not able to contact the cache service. Caching will be skipped'
123-
);
124-
return false;
125-
}
126-
127101
export function logWarning(message: string): void {
128102
const warningPrefix = '[warning]';
129103
core.info(`${warningPrefix}${message}`);

0 commit comments

Comments
 (0)