Skip to content

Commit

Permalink
[kbn/optimizer] ensure build script can prime cache (#67020)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spencer authored and spalger committed May 21, 2020
1 parent d5aac4c commit b879d8d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/kbn-optimizer/src/common/worker_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export interface WorkerConfig {
readonly optimizerCacheKey: unknown;
}

export type CacheableWorkerConfig = Omit<WorkerConfig, 'watch' | 'profileWebpack'>;

export function parseWorkerConfig(json: string): WorkerConfig {
try {
if (typeof json !== 'string') {
Expand Down
2 changes: 0 additions & 2 deletions packages/kbn-optimizer/src/optimizer/cache_keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ describe('getOptimizerCacheKey()', () => {
"cache": true,
"dist": false,
"optimizerCacheKey": "♻",
"profileWebpack": false,
"repoRoot": <absolute path>,
"watch": false,
},
}
`);
Expand Down
16 changes: 13 additions & 3 deletions packages/kbn-optimizer/src/optimizer/cache_keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import stripAnsi from 'strip-ansi';

import jestDiff from 'jest-diff';
import jsonStable from 'json-stable-stringify';
import { ascending, WorkerConfig } from '../common';
import { ascending, CacheableWorkerConfig } from '../common';

import { getMtimes } from './get_mtimes';
import { getChanges } from './get_changes';
Expand All @@ -37,6 +37,16 @@ import { OptimizerConfig } from './optimizer_config';
const OPTIMIZER_DIR = Path.dirname(require.resolve('../../package.json'));
const RELATIVE_DIR = Path.relative(REPO_ROOT, OPTIMIZER_DIR);

function omit<T, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> {
const result: any = {};
for (const [key, value] of Object.entries(obj) as any) {
if (!keys.includes(key)) {
result[key] = value;
}
}
return result as Omit<T, K>;
}

export function diffCacheKey(expected?: unknown, actual?: unknown) {
if (jsonStable(expected) === jsonStable(actual)) {
return;
Expand Down Expand Up @@ -119,7 +129,7 @@ export function reformatJestDiff(diff: string | null) {
export interface OptimizerCacheKey {
readonly lastCommit: string | undefined;
readonly bootstrap: string | undefined;
readonly workerConfig: WorkerConfig;
readonly workerConfig: CacheableWorkerConfig;
readonly deletedPaths: string[];
readonly modifiedTimes: Record<string, number>;
}
Expand Down Expand Up @@ -164,11 +174,11 @@ export async function getOptimizerCacheKey(config: OptimizerConfig) {
}

const cacheKeys: OptimizerCacheKey = {
workerConfig: config.getWorkerConfig('♻'),
lastCommit,
bootstrap,
deletedPaths,
modifiedTimes: {} as Record<string, number>,
workerConfig: omit(config.getWorkerConfig('♻'), ['watch', 'profileWebpack']),
};

const mtimes = await getMtimes(modifiedPaths);
Expand Down

0 comments on commit b879d8d

Please sign in to comment.