From 0c05a66b76daecefdc68779bda05484c3dd4ca4c Mon Sep 17 00:00:00 2001 From: Edouard Bozon Date: Tue, 13 May 2025 12:18:46 +0200 Subject: [PATCH] fix(qwik-nx): drop deprecated `readWorkspaceConfig` in favor of `readCachedProjectGraph` --- .../src/plugins/qwik-nx-vite.plugin.spec.ts | 71 +++++++++++++++---- .../src/plugins/utils/current-project-name.ts | 15 ++-- .../src/plugins/utils/get-vendor-roots.ts | 18 +++-- 3 files changed, 74 insertions(+), 30 deletions(-) diff --git a/packages/qwik-nx/src/plugins/qwik-nx-vite.plugin.spec.ts b/packages/qwik-nx/src/plugins/qwik-nx-vite.plugin.spec.ts index 915de213..36dd6e95 100644 --- a/packages/qwik-nx/src/plugins/qwik-nx-vite.plugin.spec.ts +++ b/packages/qwik-nx/src/plugins/qwik-nx-vite.plugin.spec.ts @@ -1,16 +1,67 @@ -import { workspaceRoot } from '@nx/devkit'; +import { + ProjectGraph, + ProjectsConfigurations, + workspaceRoot, +} from '@nx/devkit'; import { join } from 'path'; import { qwikNxVite } from './qwik-nx-vite.plugin'; import { QwikNxVitePluginOptions } from './models/qwik-nx-vite'; // eslint-disable-next-line @typescript-eslint/no-var-requires -const fileUtils = require('nx/src/project-graph/file-utils'); +const nxDevkit = require('@nx/devkit'); // eslint-disable-next-line @typescript-eslint/no-var-requires const fs = require('fs'); // eslint-disable-next-line @typescript-eslint/no-var-requires const getProjectDependenciesModule = require('./utils/get-project-dependencies'); -function getWorkspaceConfig() { +function getProjectsGraph(): Partial { + return { + nodes: { + 'tmp-test-app-a': { + name: 'tmp-test-app-a', + type: 'app', + data: { + root: 'apps/test-app-a', + sourceRoot: 'apps/test-app-a/src', + projectType: 'application', + tags: ['tag1', 'tag2'], + }, + }, + 'tmp-test-lib-a': { + name: 'tmp-test-lib-a', + type: 'lib', + data: { + root: 'libs/test-lib-a', + sourceRoot: 'libs/test-lib-a/src', + projectType: 'library', + tags: ['tag2'], + }, + }, + 'tmp-test-lib-b': { + name: 'tmp-test-lib-b', + type: 'lib', + data: { + root: 'libs/test-lib-b', + sourceRoot: 'libs/test-lib-b/src', + projectType: 'library', + tags: ['tag2', 'tag3'], + }, + }, + 'tmp-test-lib-c-nested-1': { + name: 'tmp-test-lib-c-nested-1', + type: 'lib', + data: { + root: 'libs/test-lib-c/nested', + sourceRoot: 'libs/test-lib-c/nested-1/src', + projectType: 'library', + tags: ['tag4'], + }, + }, + }, + }; +} + +function getWorkspaceConfig(): Partial { return { projects: { 'tmp-test-app-a': { @@ -18,7 +69,6 @@ function getWorkspaceConfig() { name: 'tmp-test-app-a', projectType: 'application', sourceRoot: 'apps/test-app-a/src', - prefix: 'tmp', tags: ['tag1', 'tag2'], }, 'tmp-test-lib-a': { @@ -26,7 +76,6 @@ function getWorkspaceConfig() { name: 'tmp-test-lib-a', projectType: 'library', sourceRoot: 'libs/test-lib-a/src', - prefix: 'tmp', tags: ['tag2'], }, 'tmp-test-lib-b': { @@ -34,7 +83,6 @@ function getWorkspaceConfig() { name: 'tmp-test-lib-b', projectType: 'library', sourceRoot: 'libs/test-lib-b/src', - prefix: 'tmp', tags: ['tag2', 'tag3'], }, 'tmp-test-lib-c-nested-1': { @@ -42,7 +90,6 @@ function getWorkspaceConfig() { name: 'tmp-test-lib-c-nested-1', projectType: 'library', sourceRoot: 'libs/test-lib-c/nested-1/src', - prefix: 'tmp', tags: ['tag4'], }, 'tmp-test-lib-c-nested-2': { @@ -50,7 +97,6 @@ function getWorkspaceConfig() { name: 'tmp-test-lib-c-nested-2', projectType: 'library', sourceRoot: 'libs/test-lib-c/nested-2/src', - prefix: 'tmp', tags: ['tag1', 'tag2', 'tag3'], }, 'tmp-other-test-lib-a': { @@ -58,7 +104,6 @@ function getWorkspaceConfig() { name: 'tmp-other-test-lib-a', projectType: 'library', sourceRoot: 'libs/other/test-lib-a/src', - prefix: 'tmp', tags: [], }, // it will be excluded because it is not set in our mock tsconfig.json @@ -67,7 +112,6 @@ function getWorkspaceConfig() { name: 'tmp-always-excluded', projectType: 'library', sourceRoot: 'libs/always/excluded/src', - prefix: 'tmp', tags: [], }, }, @@ -90,8 +134,11 @@ function getTsConfigString() { describe('qwik-nx-vite plugin', () => { jest - .spyOn(fileUtils, 'readWorkspaceConfig') + .spyOn(nxDevkit, 'readProjectsConfigurationFromProjectGraph') .mockReturnValue(getWorkspaceConfig()); + jest + .spyOn(nxDevkit, 'readCachedProjectGraph') + .mockReturnValue(getProjectsGraph()); const originalReadFileSync = jest.requireActual('fs').readFileSync; jest.spyOn(fs, 'readFileSync').mockImplementation((fileName, ...args) => { if ( @@ -105,7 +152,7 @@ describe('qwik-nx-vite plugin', () => { jest .spyOn(getProjectDependenciesModule, 'getProjectDependencies') .mockReturnValue( - Promise.resolve(new Set(Object.keys(getWorkspaceConfig().projects))) + Promise.resolve(new Set(Object.keys(getWorkspaceConfig().projects!))) ); /** diff --git a/packages/qwik-nx/src/plugins/utils/current-project-name.ts b/packages/qwik-nx/src/plugins/utils/current-project-name.ts index ecc6f856..173d068d 100644 --- a/packages/qwik-nx/src/plugins/utils/current-project-name.ts +++ b/packages/qwik-nx/src/plugins/utils/current-project-name.ts @@ -1,22 +1,15 @@ +import { ProjectGraph, normalizePath, workspaceRoot } from '@nx/devkit'; import { - ProjectsConfigurations, - normalizePath, - workspaceRoot, -} from '@nx/devkit'; -import { - createProjectRootMappingsFromProjectConfigurations, + createProjectRootMappings, findProjectForPath, } from 'nx/src/project-graph/utils/find-project-for-path'; import { relative } from 'path'; export function getCurrentProjectName( - projectsConfigurations: ProjectsConfigurations, + projectGraph: ProjectGraph, projectRootDir: string ): string { - const projectRootMappings = - createProjectRootMappingsFromProjectConfigurations( - projectsConfigurations.projects - ); + const projectRootMappings = createProjectRootMappings(projectGraph.nodes); const relativeDirname = relative(workspaceRoot, projectRootDir); const normalizedRelativeDirname = normalizePath(relativeDirname); const currentProjectName = findProjectForPath( diff --git a/packages/qwik-nx/src/plugins/utils/get-vendor-roots.ts b/packages/qwik-nx/src/plugins/utils/get-vendor-roots.ts index db58deea..266ea273 100644 --- a/packages/qwik-nx/src/plugins/utils/get-vendor-roots.ts +++ b/packages/qwik-nx/src/plugins/utils/get-vendor-roots.ts @@ -1,11 +1,14 @@ -import { readWorkspaceConfig } from 'nx/src/project-graph/file-utils'; import { QwikNxVitePluginOptions, QwikVitePluginOptionsStub, } from '../models/qwik-nx-vite'; import { readFileSync } from 'fs'; import { join } from 'path'; -import { workspaceRoot } from '@nx/devkit'; +import { + readCachedProjectGraph, + readProjectsConfigurationFromProjectGraph, + workspaceRoot, +} from '@nx/devkit'; import { getCurrentProjectName } from './current-project-name'; import { filterProjects } from './filter-projects'; import { getProjectDependencies } from './get-project-dependencies'; @@ -20,8 +23,9 @@ export async function getVendorRoots( console.debug(`[QWIK-NX-VITE PLUGIN:]`, ...str); } }; - - const workspaceConfig = readWorkspaceConfig({ format: 'nx' }); + const projectGraph = readCachedProjectGraph(); + const projectsConfiguration = + readProjectsConfigurationFromProjectGraph(projectGraph); const baseTsConfig = JSON.parse( readFileSync(join(workspaceRoot, 'tsconfig.base.json')).toString() @@ -30,11 +34,11 @@ export async function getVendorRoots( baseTsConfig.compilerOptions.paths ).flat(); - let projects = Object.values(workspaceConfig.projects); + let projects = Object.values(projectsConfiguration.projects); if ( options?.currentProjectName && - !workspaceConfig.projects[options.currentProjectName] + !projectsConfiguration.projects[options.currentProjectName] ) { throw new Error( `Could not find project with name "${options.currentProjectName}"` @@ -43,7 +47,7 @@ export async function getVendorRoots( const currentProjectName = options?.currentProjectName ?? - getCurrentProjectName(workspaceConfig, qwikOptions.rootDir); + getCurrentProjectName(projectGraph, qwikOptions.rootDir); projects = projects.filter((p) => p.name !== currentProjectName);