Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 59 additions & 12 deletions packages/qwik-nx/src/plugins/qwik-nx-vite.plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,109 @@
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<ProjectGraph> {
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<ProjectsConfigurations> {
return {
projects: {
'tmp-test-app-a': {
root: 'apps/test-app-a',
name: 'tmp-test-app-a',
projectType: 'application',
sourceRoot: 'apps/test-app-a/src',
prefix: 'tmp',
tags: ['tag1', 'tag2'],
},
'tmp-test-lib-a': {
root: 'libs/test-lib-a',
name: 'tmp-test-lib-a',
projectType: 'library',
sourceRoot: 'libs/test-lib-a/src',
prefix: 'tmp',
tags: ['tag2'],
},
'tmp-test-lib-b': {
root: 'libs/test-lib-b',
name: 'tmp-test-lib-b',
projectType: 'library',
sourceRoot: 'libs/test-lib-b/src',
prefix: 'tmp',
tags: ['tag2', 'tag3'],
},
'tmp-test-lib-c-nested-1': {
root: 'libs/test-lib-c/nested',
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': {
root: 'libs/test-lib-c/nested',
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': {
root: 'libs/other/test-lib-a',
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
Expand All @@ -67,7 +112,6 @@ function getWorkspaceConfig() {
name: 'tmp-always-excluded',
projectType: 'library',
sourceRoot: 'libs/always/excluded/src',
prefix: 'tmp',
tags: [],
},
},
Expand All @@ -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 (
Expand All @@ -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!)))
);

/**
Expand Down
15 changes: 4 additions & 11 deletions packages/qwik-nx/src/plugins/utils/current-project-name.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
18 changes: 11 additions & 7 deletions packages/qwik-nx/src/plugins/utils/get-vendor-roots.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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()
Expand All @@ -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}"`
Expand All @@ -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);

Expand Down
Loading