From 8156a6b3bb1f922b4712f2cb59a831ea91acbf3f Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Fri, 10 May 2024 17:43:42 -0400 Subject: [PATCH] fix(misc): various inference plugins caching should track changes --- packages/cypress/src/plugins/plugin.ts | 33 ++-- packages/detox/src/plugins/plugin.ts | 28 +-- packages/expo/plugins/plugin.ts | 32 ++-- packages/gradle/src/plugin/dependencies.ts | 4 +- packages/gradle/src/plugin/nodes.ts | 171 +++++++++---------- packages/jest/src/plugins/plugin.spec.ts | 2 - packages/jest/src/plugins/plugin.ts | 27 +-- packages/js/src/plugins/typescript/plugin.ts | 34 ++-- packages/next/src/plugins/plugin.ts | 34 ++-- packages/nuxt/src/plugins/plugin.ts | 34 ++-- packages/playwright/src/plugins/plugin.ts | 33 ++-- packages/react-native/plugins/plugin.ts | 37 ++-- packages/remix/src/plugins/plugin.ts | 40 ++--- packages/rollup/src/plugins/plugin.ts | 32 ++-- packages/storybook/src/plugins/plugin.ts | 41 ++--- packages/vite/src/plugins/plugin.ts | 33 ++-- packages/webpack/src/plugins/plugin.ts | 21 +-- 17 files changed, 287 insertions(+), 349 deletions(-) diff --git a/packages/cypress/src/plugins/plugin.ts b/packages/cypress/src/plugins/plugin.ts index dba51d94a2574..1151907965abf 100644 --- a/packages/cypress/src/plugins/plugin.ts +++ b/packages/cypress/src/plugins/plugin.ts @@ -31,20 +31,22 @@ export interface CypressPluginOptions { } const cachePath = join(projectGraphCacheDirectory, 'cypress.hash'); -const targetsCache = existsSync(cachePath) ? readTargetsCache() : {}; - -const calculatedTargets: Record = {}; +const targetsCache = readTargetsCache(); function readTargetsCache(): Record { - return readJsonFile(cachePath); + return existsSync(cachePath) ? readJsonFile(cachePath) : {}; } -function writeTargetsToCache(targets: Record) { - writeJsonFile(cachePath, targets); +function writeTargetsToCache() { + const oldCache = readTargetsCache(); + writeJsonFile(cachePath, { + ...oldCache, + targetsCache, + }); } export const createDependencies: CreateDependencies = () => { - writeTargetsToCache(calculatedTargets); + writeTargetsToCache(); return []; }; @@ -67,16 +69,13 @@ export const createNodes: CreateNodes = [ getLockFileName(detectPackageManager(context.workspaceRoot)), ]); - const { targets, metadata } = targetsCache[hash] - ? targetsCache[hash] - : await buildCypressTargets( - configFilePath, - projectRoot, - options, - context - ); - - calculatedTargets[hash] = { targets, metadata }; + targetsCache[hash] ??= await buildCypressTargets( + configFilePath, + projectRoot, + options, + context + ); + const { targets, metadata } = targetsCache[hash]; const project: Omit = { projectType: 'application', diff --git a/packages/detox/src/plugins/plugin.ts b/packages/detox/src/plugins/plugin.ts index 7fb2ec7f33a3e..3873b3e0b5ca9 100644 --- a/packages/detox/src/plugins/plugin.ts +++ b/packages/detox/src/plugins/plugin.ts @@ -22,31 +22,21 @@ export interface DetoxPluginOptions { } const cachePath = join(projectGraphCacheDirectory, 'detox.hash'); -const targetsCache = existsSync(cachePath) ? readTargetsCache() : {}; - -const calculatedTargets: Record< - string, - Record -> = {}; +const targetsCache = readTargetsCache(); function readTargetsCache(): Record< string, Record> > { - return readJsonFile(cachePath); + return existsSync(cachePath) ? readJsonFile(cachePath) : {}; } -function writeTargetsToCache( - targets: Record< - string, - Record> - > -) { - writeJsonFile(cachePath, targets); +function writeTargetsToCache() { + writeJsonFile(cachePath, targetsCache); } export const createDependencies: CreateDependencies = () => { - writeTargetsToCache(calculatedTargets); + writeTargetsToCache(); return []; }; @@ -66,16 +56,12 @@ export const createNodes: CreateNodes = [ getLockFileName(detectPackageManager(context.workspaceRoot)), ]); - const targets = targetsCache[hash] - ? targetsCache[hash] - : buildDetoxTargets(projectRoot, options, context); - - calculatedTargets[hash] = targets; + targetsCache[hash] ??= buildDetoxTargets(projectRoot, options, context); return { projects: { [projectRoot]: { - targets, + targets: targetsCache[hash], }, }, }; diff --git a/packages/expo/plugins/plugin.ts b/packages/expo/plugins/plugin.ts index 62ffcc692a3f3..d9f3b4bd812d2 100644 --- a/packages/expo/plugins/plugin.ts +++ b/packages/expo/plugins/plugin.ts @@ -30,31 +30,25 @@ export interface ExpoPluginOptions { } const cachePath = join(projectGraphCacheDirectory, 'expo.hash'); -const targetsCache = existsSync(cachePath) ? readTargetsCache() : {}; - -const calculatedTargets: Record< - string, - Record -> = {}; +const targetsCache = readTargetsCache(); function readTargetsCache(): Record< string, Record> > { - return readJsonFile(cachePath); + return existsSync(cachePath) ? readJsonFile(cachePath) : {}; } -function writeTargetsToCache( - targets: Record< - string, - Record> - > -) { - writeJsonFile(cachePath, targets); +function writeTargetsToCache() { + const oldCache = readTargetsCache(); + writeJsonFile(cachePath, { + ...oldCache, + targetsCache, + }); } export const createDependencies: CreateDependencies = () => { - writeTargetsToCache(calculatedTargets); + writeTargetsToCache(); return []; }; @@ -82,16 +76,12 @@ export const createNodes: CreateNodes = [ getLockFileName(detectPackageManager(context.workspaceRoot)), ]); - const targets = targetsCache[hash] - ? targetsCache[hash] - : buildExpoTargets(projectRoot, options, context); - - calculatedTargets[hash] = targets; + targetsCache[hash] ??= buildExpoTargets(projectRoot, options, context); return { projects: { [projectRoot]: { - targets, + targets: targetsCache[hash], }, }, }; diff --git a/packages/gradle/src/plugin/dependencies.ts b/packages/gradle/src/plugin/dependencies.ts index 764bfc61dc270..a09a3dafa503d 100644 --- a/packages/gradle/src/plugin/dependencies.ts +++ b/packages/gradle/src/plugin/dependencies.ts @@ -13,7 +13,7 @@ import { getGradleReport, invalidateGradleReportCache, } from '../utils/get-gradle-report'; -import { calculatedTargets, writeTargetsToCache } from './nodes'; +import { writeTargetsToCache } from './nodes'; export const createDependencies: CreateDependencies = async ( _, @@ -58,7 +58,7 @@ export const createDependencies: CreateDependencies = async ( gradleDependenciesEnd.name ); - writeTargetsToCache(calculatedTargets); + writeTargetsToCache(); if (dependencies.length) { invalidateGradleReportCache(); } diff --git a/packages/gradle/src/plugin/nodes.ts b/packages/gradle/src/plugin/nodes.ts index 1e330022e5ff9..621378c926126 100644 --- a/packages/gradle/src/plugin/nodes.ts +++ b/packages/gradle/src/plugin/nodes.ts @@ -34,39 +34,26 @@ export interface GradlePluginOptions { } const cachePath = join(projectGraphCacheDirectory, 'gradle.hash'); -const targetsCache = existsSync(cachePath) ? readTargetsCache() : {}; - -export const calculatedTargets: Record< +export const targetsCache = readTargetsCache(); +type GradleTargets = Record< string, { name: string; targets: Record; metadata: ProjectConfiguration['metadata']; } -> = {}; +>; -function readTargetsCache(): Record< - string, - { - name: string; - targets: Record; - metadata: ProjectConfiguration['metadata']; - } -> { - return readJsonFile(cachePath); +function readTargetsCache(): GradleTargets { + return existsSync(cachePath) ? readJsonFile(cachePath) : {}; } -export function writeTargetsToCache( - targets: Record< - string, - { - name: string; - targets: Record; - metadata: ProjectConfiguration['metadata']; - } - > -) { - writeJsonFile(cachePath, targets); +export function writeTargetsToCache() { + const oldCache = readTargetsCache(); + writeJsonFile(cachePath, { + ...oldCache, + ...targetsCache, + }); } export const createNodes: CreateNodes = [ @@ -83,75 +70,79 @@ export const createNodes: CreateNodes = [ options ?? {}, context ); - if (targetsCache[hash]) { - calculatedTargets[hash] = targetsCache[hash]; - return { - projects: { - [projectRoot]: targetsCache[hash], - }, - }; + targetsCache[hash] ??= createGradleProject( + gradleFilePath, + options, + context + ); + return { + projects: { + [projectRoot]: targetsCache[hash], + }, + }; + }, +]; + +function createGradleProject( + gradleFilePath: string, + options: GradlePluginOptions | undefined, + context: CreateNodesContext +) { + try { + const { + gradleProjectToTasksTypeMap, + gradleFileToOutputDirsMap, + gradleFileToGradleProjectMap, + gradleProjectToProjectName, + } = getGradleReport(); + + const gradleProject = gradleFileToGradleProjectMap.get( + gradleFilePath + ) as string; + const projectName = gradleProjectToProjectName.get(gradleProject); + if (!projectName) { + return; } - try { - const { - gradleProjectToTasksTypeMap, - gradleFileToOutputDirsMap, - gradleFileToGradleProjectMap, - gradleProjectToProjectName, - } = getGradleReport(); - - const gradleProject = gradleFileToGradleProjectMap.get( - gradleFilePath - ) as string; - const projectName = gradleProjectToProjectName.get(gradleProject); - if (!projectName) { - return; - } - - const tasksTypeMap = gradleProjectToTasksTypeMap.get( - gradleProject - ) as Map; - let tasks: GradleTask[] = []; - for (let [taskName, taskType] of tasksTypeMap.entries()) { - tasks.push({ - type: taskType, - name: taskName, - }); - } - - const outputDirs = gradleFileToOutputDirsMap.get(gradleFilePath) as Map< - string, - string - >; - - const { targets, targetGroups } = createGradleTargets( - tasks, - options, - context, - outputDirs, - gradleProject - ); - const project = { - name: projectName, - targets, - metadata: { - targetGroups, - technologies: ['gradle'], - }, - }; - calculatedTargets[hash] = project; - - return { - projects: { - [projectRoot]: project, - }, - }; - } catch (e) { - console.error(e); - return {}; + const tasksTypeMap = gradleProjectToTasksTypeMap.get(gradleProject) as Map< + string, + string + >; + let tasks: GradleTask[] = []; + for (let [taskName, taskType] of tasksTypeMap.entries()) { + tasks.push({ + type: taskType, + name: taskName, + }); } - }, -]; + + const outputDirs = gradleFileToOutputDirsMap.get(gradleFilePath) as Map< + string, + string + >; + + const { targets, targetGroups } = createGradleTargets( + tasks, + options, + context, + outputDirs, + gradleProject + ); + const project = { + name: projectName, + targets, + metadata: { + targetGroups, + technologies: ['gradle'], + }, + }; + + return project; + } catch (e) { + console.error(e); + return undefined; + } +} function createGradleTargets( tasks: GradleTask[], diff --git a/packages/jest/src/plugins/plugin.spec.ts b/packages/jest/src/plugins/plugin.spec.ts index cfc0d5ce758ca..f2e4a177a1cc3 100644 --- a/packages/jest/src/plugins/plugin.spec.ts +++ b/packages/jest/src/plugins/plugin.spec.ts @@ -33,8 +33,6 @@ describe('@nx/jest/plugin', () => { }); }); - test('foo', () => {}); - afterEach(() => { jest.resetModules(); process.chdir(cwd); diff --git a/packages/jest/src/plugins/plugin.ts b/packages/jest/src/plugins/plugin.ts index 5487852c68119..ecc420760572e 100644 --- a/packages/jest/src/plugins/plugin.ts +++ b/packages/jest/src/plugins/plugin.ts @@ -28,22 +28,24 @@ export interface JestPluginOptions { } const cachePath = join(projectGraphCacheDirectory, 'jest.hash'); -const targetsCache = existsSync(cachePath) ? readTargetsCache() : {}; +const targetsCache = readTargetsCache(); type JestTargets = Awaited>; -const calculatedTargets: JestTargets = {}; - function readTargetsCache(): Record { - return readJsonFile(cachePath); + return existsSync(cachePath) ? readJsonFile(cachePath) : {}; } -function writeTargetsToCache(targets: JestTargets) { - writeJsonFile(cachePath, targets); +function writeTargetsToCache(targetCache: Record) { + const cache = readTargetsCache(); + writeJsonFile(cachePath, { + ...cache, + ...targetCache, + }); } export const createDependencies: CreateDependencies = () => { - writeTargetsToCache(calculatedTargets); + writeTargetsToCache(targetsCache); return []; }; @@ -93,11 +95,14 @@ export const createNodes: CreateNodes = [ options = normalizeOptions(options); const hash = calculateHashForCreateNodes(projectRoot, options, context); - const { targets, metadata } = - targetsCache[hash] ?? - (await buildJestTargets(configFilePath, projectRoot, options, context)); + targetsCache[hash] ??= await buildJestTargets( + configFilePath, + projectRoot, + options, + context + ); - calculatedTargets[hash] = { targets, metadata }; + const { targets, metadata } = targetsCache[hash]; return { projects: { diff --git a/packages/js/src/plugins/typescript/plugin.ts b/packages/js/src/plugins/typescript/plugin.ts index f7914af6d6225..3c16473a0da11 100644 --- a/packages/js/src/plugins/typescript/plugin.ts +++ b/packages/js/src/plugins/typescript/plugin.ts @@ -50,28 +50,25 @@ interface NormalizedPluginOptions { } const cachePath = join(projectGraphCacheDirectory, 'tsc.hash'); -const targetsCache = existsSync(cachePath) ? readTargetsCache() : {}; - -const calculatedTargets: Record< - string, - Record -> = {}; +const targetsCache = readTargetsCache(); function readTargetsCache(): Record< string, Record> > { - return readJsonFile(cachePath); + return existsSync(cachePath) ? readJsonFile(cachePath) : {}; } -function writeTargetsToCache( - targets: Record>> -) { - writeJsonFile(cachePath, targets); +function writeTargetsToCache() { + const oldCache = readTargetsCache(); + writeJsonFile(cachePath, { + ...oldCache, + ...targetsCache, + }); } export const createDependencies: CreateDependencies = () => { - writeTargetsToCache(calculatedTargets); + writeTargetsToCache(); return []; }; @@ -113,17 +110,18 @@ export const createNodes: CreateNodes = [ // The hash is calculated at the node/project level, so we add the config file path to avoid conflicts when caching const cacheKey = `${nodeHash}_${configFilePath}`; - const targets = targetsCache[cacheKey] - ? targetsCache[cacheKey] - : buildTscTargets(fullConfigPath, projectRoot, pluginOptions, context); - - calculatedTargets[cacheKey] = targets; + targetsCache[cacheKey] ??= buildTscTargets( + fullConfigPath, + projectRoot, + pluginOptions, + context + ); return { projects: { [projectRoot]: { projectType: 'library', - targets, + targets: targetsCache[cacheKey], }, }, }; diff --git a/packages/next/src/plugins/plugin.ts b/packages/next/src/plugins/plugin.ts index 033fd47721c5e..3bfa2b07caaf5 100644 --- a/packages/next/src/plugins/plugin.ts +++ b/packages/next/src/plugins/plugin.ts @@ -26,28 +26,25 @@ export interface NextPluginOptions { } const cachePath = join(projectGraphCacheDirectory, 'next.hash'); -const targetsCache = existsSync(cachePath) ? readTargetsCache() : {}; - -const calculatedTargets: Record< - string, - Record -> = {}; +const targetsCache = readTargetsCache(); function readTargetsCache(): Record< string, Record > { - return readJsonFile(cachePath); + return existsSync(cachePath) ? readJsonFile(cachePath) : {}; } -function writeTargetsToCache( - targets: Record> -) { - writeJsonFile(cachePath, targets); +function writeTargetsToCache() { + const oldCache = readTargetsCache(); + writeJsonFile(cachePath, { + ...oldCache, + ...targetsCache, + }); } export const createDependencies: CreateDependencies = () => { - writeTargetsToCache(calculatedTargets); + writeTargetsToCache(); return []; }; @@ -70,17 +67,18 @@ export const createNodes: CreateNodes = [ getLockFileName(detectPackageManager(context.workspaceRoot)), ]); - const targets = - targetsCache[hash] ?? - (await buildNextTargets(configFilePath, projectRoot, options, context)); - - calculatedTargets[hash] = targets; + targetsCache[hash] ??= await buildNextTargets( + configFilePath, + projectRoot, + options, + context + ); return { projects: { [projectRoot]: { root: projectRoot, - targets, + targets: targetsCache[hash], }, }, }; diff --git a/packages/nuxt/src/plugins/plugin.ts b/packages/nuxt/src/plugins/plugin.ts index 02b5761c9328d..922f2d81e60bd 100644 --- a/packages/nuxt/src/plugins/plugin.ts +++ b/packages/nuxt/src/plugins/plugin.ts @@ -18,28 +18,25 @@ import { getLockFileName } from '@nx/js'; import { loadConfigFile } from '@nx/devkit/src/utils/config-utils'; const cachePath = join(projectGraphCacheDirectory, 'nuxt.hash'); -const targetsCache = existsSync(cachePath) ? readTargetsCache() : {}; - -const calculatedTargets: Record< - string, - Record -> = {}; +const targetsCache = readTargetsCache(); function readTargetsCache(): Record< string, Record > { - return readJsonFile(cachePath); + return existsSync(cachePath) ? readJsonFile(cachePath) : {}; } -function writeTargetsToCache( - targets: Record> -) { - writeJsonFile(cachePath, targets); +function writeTargetsToCache() { + const oldCache = readTargetsCache(); + writeJsonFile(cachePath, { + ...oldCache, + ...targetsCache, + }); } export const createDependencies: CreateDependencies = () => { - writeTargetsToCache(calculatedTargets); + writeTargetsToCache(); return []; }; @@ -68,17 +65,18 @@ export const createNodes: CreateNodes = [ const hash = calculateHashForCreateNodes(projectRoot, options, context, [ getLockFileName(detectPackageManager(context.workspaceRoot)), ]); - const targets = targetsCache[hash] - ? targetsCache[hash] - : await buildNuxtTargets(configFilePath, projectRoot, options, context); - - calculatedTargets[hash] = targets; + targetsCache[hash] ??= await buildNuxtTargets( + configFilePath, + projectRoot, + options, + context + ); return { projects: { [projectRoot]: { root: projectRoot, - targets, + targets: targetsCache[hash], }, }, }; diff --git a/packages/playwright/src/plugins/plugin.ts b/packages/playwright/src/plugins/plugin.ts index 861be3897a4a0..b27a4b4cc475d 100644 --- a/packages/playwright/src/plugins/plugin.ts +++ b/packages/playwright/src/plugins/plugin.ts @@ -35,22 +35,24 @@ interface NormalizedOptions { const cachePath = join(projectGraphCacheDirectory, 'playwright.hash'); -const targetsCache = existsSync(cachePath) ? readTargetsCache() : {}; +const targetsCache = readTargetsCache(); type PlaywrightTargets = Pick; -const calculatedTargets: Record = {}; - function readTargetsCache(): Record { - return readJsonFile(cachePath); + return existsSync(cachePath) ? readJsonFile(cachePath) : {}; } -function writeTargetsToCache(targets: Record) { - writeJsonFile(cachePath, targets); +function writeTargetsToCache() { + const oldCache = readTargetsCache(); + writeJsonFile(cachePath, { + ...readTargetsCache, + targetsCache, + }); } export const createDependencies: CreateDependencies = () => { - writeTargetsToCache(calculatedTargets); + writeTargetsToCache(); return []; }; @@ -74,16 +76,13 @@ export const createNodes: CreateNodes = [ getLockFileName(detectPackageManager(context.workspaceRoot)), ]); - const { targets, metadata } = - targetsCache[hash] ?? - (await buildPlaywrightTargets( - configFilePath, - projectRoot, - normalizedOptions, - context - )); - - calculatedTargets[hash] = { targets, metadata }; + targetsCache[hash] ??= await buildPlaywrightTargets( + configFilePath, + projectRoot, + normalizedOptions, + context + ); + const { targets, metadata } = targetsCache[hash]; return { projects: { diff --git a/packages/react-native/plugins/plugin.ts b/packages/react-native/plugins/plugin.ts index 6cdc96d764959..e1685b5345771 100644 --- a/packages/react-native/plugins/plugin.ts +++ b/packages/react-native/plugins/plugin.ts @@ -30,31 +30,24 @@ export interface ReactNativePluginOptions { } const cachePath = join(projectGraphCacheDirectory, 'react-native.hash'); -const targetsCache = existsSync(cachePath) ? readTargetsCache() : {}; - -const calculatedTargets: Record< - string, - Record -> = {}; - +const targetsCache = readTargetsCache(); function readTargetsCache(): Record< string, Record> > { - return readJsonFile(cachePath); + return existsSync(cachePath) ? readJsonFile(cachePath) : {}; } -function writeTargetsToCache( - targets: Record< - string, - Record> - > -) { - writeJsonFile(cachePath, targets); +function writeTargetsToCache() { + const oldCache = readTargetsCache(); + writeJsonFile(cachePath, { + ...oldCache, + ...targetsCache, + }); } export const createDependencies: CreateDependencies = () => { - writeTargetsToCache(calculatedTargets); + writeTargetsToCache(); return []; }; @@ -81,16 +74,16 @@ export const createNodes: CreateNodes = [ getLockFileName(detectPackageManager(context.workspaceRoot)), ]); - const targets = targetsCache[hash] - ? targetsCache[hash] - : buildReactNativeTargets(projectRoot, options, context); - - calculatedTargets[hash] = targets; + targetsCache[hash] ??= buildReactNativeTargets( + projectRoot, + options, + context + ); return { projects: { [projectRoot]: { - targets, + targets: targetsCache[hash], }, }, }; diff --git a/packages/remix/src/plugins/plugin.ts b/packages/remix/src/plugins/plugin.ts index 9515693f450f1..1677a37ed0698 100644 --- a/packages/remix/src/plugins/plugin.ts +++ b/packages/remix/src/plugins/plugin.ts @@ -18,27 +18,25 @@ import { existsSync, readdirSync } from 'fs'; import { loadConfigFile } from '@nx/devkit/src/utils/config-utils'; const cachePath = join(projectGraphCacheDirectory, 'remix.hash'); -const targetsCache = existsSync(cachePath) ? readTargetsCache() : {}; -const calculatedTargets: Record< - string, - Record -> = {}; +const targetsCache = readTargetsCache(); function readTargetsCache(): Record< string, Record > { - return readJsonFile(cachePath); + return existsSync(cachePath) ? readJsonFile(cachePath) : {}; } -function writeTargetsToCache( - targets: Record> -) { - writeJsonFile(cachePath, targets); +function writeTargetsToCache() { + const oldCache = readTargetsCache(); + writeJsonFile(cachePath, { + ...oldCache, + ...targetsCache, + }); } export const createDependencies: CreateDependencies = () => { - writeTargetsToCache(calculatedTargets); + writeTargetsToCache(); return []; }; @@ -71,23 +69,19 @@ export const createNodes: CreateNodes = [ const hash = calculateHashForCreateNodes(projectRoot, options, context, [ getLockFileName(detectPackageManager(context.workspaceRoot)), ]); - const targets = targetsCache[hash] - ? targetsCache[hash] - : await buildRemixTargets( - configFilePath, - projectRoot, - options, - context, - siblingFiles - ); - - calculatedTargets[hash] = targets; + targetsCache[hash] ??= await buildRemixTargets( + configFilePath, + projectRoot, + options, + context, + siblingFiles + ); return { projects: { [projectRoot]: { root: projectRoot, - targets, + targets: targetsCache[hash], }, }, }; diff --git a/packages/rollup/src/plugins/plugin.ts b/packages/rollup/src/plugins/plugin.ts index a78d85df8c5bf..b4f7e1bc97d43 100644 --- a/packages/rollup/src/plugins/plugin.ts +++ b/packages/rollup/src/plugins/plugin.ts @@ -21,27 +21,25 @@ import { type RollupOptions } from 'rollup'; import { loadConfigFile } from 'rollup/loadConfigFile'; const cachePath = join(projectGraphCacheDirectory, 'rollup.hash'); -const targetsCache = existsSync(cachePath) ? readTargetsCache() : {}; -const calculatedTargets: Record< - string, - Record -> = {}; +const targetsCache = readTargetsCache(); function readTargetsCache(): Record< string, Record > { - return readJsonFile(cachePath); + return existsSync(cachePath) ? readJsonFile(cachePath) : {}; } -function writeTargetsToCache( - targets: Record> -) { - writeJsonFile(cachePath, targets); +function writeTargetsToCache() { + const oldCache = readTargetsCache(); + writeJsonFile(cachePath, { + ...oldCache, + ...targetsCache, + }); } export const createDependencies: CreateDependencies = () => { - writeTargetsToCache(calculatedTargets); + writeTargetsToCache(); return []; }; @@ -69,16 +67,18 @@ export const createNodes: CreateNodes = [ getLockFileName(detectPackageManager(context.workspaceRoot)), ]); - const targets = targetsCache[hash] - ? targetsCache[hash] - : await buildRollupTarget(configFilePath, projectRoot, options, context); + targetsCache[hash] ??= await buildRollupTarget( + configFilePath, + projectRoot, + options, + context + ); - calculatedTargets[hash] = targets; return { projects: { [projectRoot]: { root: projectRoot, - targets, + targets: targetsCache[hash], }, }, }; diff --git a/packages/storybook/src/plugins/plugin.ts b/packages/storybook/src/plugins/plugin.ts index ff908f64c4bd1..c93749fa1e8f7 100644 --- a/packages/storybook/src/plugins/plugin.ts +++ b/packages/storybook/src/plugins/plugin.ts @@ -26,28 +26,25 @@ export interface StorybookPluginOptions { } const cachePath = join(projectGraphCacheDirectory, 'storybook.hash'); -const targetsCache = existsSync(cachePath) ? readTargetsCache() : {}; - -const calculatedTargets: Record< - string, - Record -> = {}; +const targetsCache = readTargetsCache(); function readTargetsCache(): Record< string, Record > { - return readJsonFile(cachePath); + return existsSync(cachePath) ? readJsonFile(cachePath) : {}; } -function writeTargetsToCache( - targets: Record> -) { - writeJsonFile(cachePath, targets); +function writeTargetsToCache() { + const oldCache = readTargetsCache(); + writeJsonFile(cachePath, { + ...oldCache, + ...targetsCache, + }); } export const createDependencies: CreateDependencies = () => { - writeTargetsToCache(calculatedTargets); + writeTargetsToCache(); return []; }; @@ -81,23 +78,19 @@ export const createNodes: CreateNodes = [ const projectName = buildProjectName(projectRoot, context.workspaceRoot); - const targets = targetsCache[hash] - ? targetsCache[hash] - : await buildStorybookTargets( - configFilePath, - projectRoot, - options, - context, - projectName - ); - - calculatedTargets[hash] = targets; + targetsCache[hash] ??= await buildStorybookTargets( + configFilePath, + projectRoot, + options, + context, + projectName + ); const result = { projects: { [projectRoot]: { root: projectRoot, - targets, + targets: targetsCache[hash], }, }, }; diff --git a/packages/vite/src/plugins/plugin.ts b/packages/vite/src/plugins/plugin.ts index 05859050971e9..8f89f1717b2e4 100644 --- a/packages/vite/src/plugins/plugin.ts +++ b/packages/vite/src/plugins/plugin.ts @@ -26,28 +26,25 @@ export interface VitePluginOptions { } const cachePath = join(projectGraphCacheDirectory, 'vite.hash'); -const targetsCache = existsSync(cachePath) ? readTargetsCache() : {}; - -const calculatedTargets: Record< - string, - Record -> = {}; +const targetsCache = readTargetsCache(); function readTargetsCache(): Record< string, Record > { - return readJsonFile(cachePath); + return existsSync(cachePath) ? readJsonFile(cachePath) : {}; } -function writeTargetsToCache( - targets: Record> -) { - writeJsonFile(cachePath, targets); +function writeTargetsToCache() { + const oldCache = readTargetsCache(); + writeJsonFile(cachePath, { + ...oldCache, + ...targetsCache, + }); } export const createDependencies: CreateDependencies = () => { - writeTargetsToCache(calculatedTargets); + writeTargetsToCache(); return []; }; @@ -72,17 +69,19 @@ export const createNodes: CreateNodes = [ calculateHashForCreateNodes(projectRoot, options, context, [ getLockFileName(detectPackageManager(context.workspaceRoot)), ]) + configFilePath; - const targets = targetsCache[hash] - ? targetsCache[hash] - : await buildViteTargets(configFilePath, projectRoot, options, context); - calculatedTargets[hash] = targets; + targetsCache[hash] ??= await buildViteTargets( + configFilePath, + projectRoot, + options, + context + ); return { projects: { [projectRoot]: { root: projectRoot, - targets, + targets: targetsCache[hash], }, }, }; diff --git a/packages/webpack/src/plugins/plugin.ts b/packages/webpack/src/plugins/plugin.ts index c733b193ae58f..e60b83310fa8a 100644 --- a/packages/webpack/src/plugins/plugin.ts +++ b/packages/webpack/src/plugins/plugin.ts @@ -27,28 +27,25 @@ export interface WebpackPluginOptions { } const cachePath = join(projectGraphCacheDirectory, 'webpack.hash'); -const targetsCache = existsSync(cachePath) ? readTargetsCache() : {}; - -const calculatedTargets: Record< - string, - Record -> = {}; +const targetsCache = readTargetsCache(); function readTargetsCache(): Record< string, Record > { - return readJsonFile(cachePath); + return existsSync(cachePath) ? readJsonFile(cachePath) : {}; } -function writeTargetsToCache( - targets: Record> -) { - writeJsonFile(cachePath, targets); +function writeTargetsToCache() { + const oldCache = readTargetsCache(); + writeJsonFile(cachePath, { + ...oldCache, + ...targetsCache, + }); } export const createDependencies: CreateDependencies = () => { - writeTargetsToCache(calculatedTargets); + writeTargetsToCache(); return []; };