Skip to content

Commit

Permalink
fix(qwik-nx): vite plugin exclude its project as a vendor root (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-stepanenko committed Mar 29, 2023
1 parent a8ebc42 commit 9456a9d
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 12 deletions.
39 changes: 36 additions & 3 deletions packages/qwik-nx/src/plugins/qwik-nx-vite.plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,22 @@ describe('qwik-nx-vite plugin', () => {
jest.spyOn(fs, 'readFileSync').mockReturnValue(tsConfigString1);

const getDecoratedPaths = async (
options?: QwikNxVitePluginOptions
options?: QwikNxVitePluginOptions,
rootDir?: string
): Promise<string[]> => {
const plugin = qwikNxVite(options);
const plugin = qwikNxVite({
...options,
// do not exclude any projects by setting "currentProjectName" to a not valid value
currentProjectName:
options?.currentProjectName || rootDir
? options?.currentProjectName
: 'mock',
});
const vendorRoots: string[] = [];
const qwikViteMock = {
name: 'vite-plugin-qwik',
api: {
getOptions: () => ({ vendorRoots }),
getOptions: () => ({ vendorRoots, rootDir }),
},
};
await (plugin.configResolved as any)({ plugins: [qwikViteMock] });
Expand All @@ -104,6 +112,31 @@ describe('qwik-nx-vite plugin', () => {
]);
});

describe('Should not include current porject as a vendor root for itself', () => {
it('with project name specified', async () => {
const paths = await getDecoratedPaths({
currentProjectName: 'tmp-test-lib-a',
});

expect(paths).toEqual([
`${workspaceRoot}/libs/test-lib-b/src`,
`${workspaceRoot}/libs/test-lib-c/nested-1/src`,
`${workspaceRoot}/libs/test-lib-c/nested-2/src`,
`${workspaceRoot}/libs/other/test-lib-a/src`,
]);
});
it('implicitly by project path', async () => {
const paths = await getDecoratedPaths(undefined, 'libs/test-lib-b');

expect(paths).toEqual([
`${workspaceRoot}/libs/test-lib-a/src`,
`${workspaceRoot}/libs/test-lib-c/nested-1/src`,
`${workspaceRoot}/libs/test-lib-c/nested-2/src`,
`${workspaceRoot}/libs/other/test-lib-a/src`,
]);
});
});

describe('Name filter', () => {
describe('As string', () => {
it('Exclude', async () => {
Expand Down
71 changes: 62 additions & 9 deletions packages/qwik-nx/src/plugins/qwik-nx-vite.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { type Plugin } from 'vite';
import { join } from 'path';
import { join, relative } from 'path';
import { readWorkspaceConfig } from 'nx/src/project-graph/file-utils';
import { ProjectConfiguration, workspaceRoot } from '@nrwl/devkit';
import {
createProjectRootMappingsFromProjectConfigurations,
findProjectForPath,
} from 'nx/src/project-graph/utils/find-project-for-path';
import {
ProjectConfiguration,
ProjectsConfigurations,
workspaceRoot,
} from '@nrwl/devkit';
import { readFileSync } from 'fs';

interface QwikVitePluginStub {
api: {
getOptions: () => { vendorRoots: string[] };
getOptions: () => QwikVitePluginOptionsStub;
};
}

interface QwikVitePluginOptionsStub {
vendorRoots: string[];
rootDir: string;
debug: boolean;
}

export interface ProjectFilter {
name?: string[] | RegExp;
path?: RegExp;
Expand All @@ -18,6 +32,7 @@ export interface ProjectFilter {
}

export interface QwikNxVitePluginOptions {
currentProjectName?: string;
includeProjects?: ProjectFilter;
excludeProjects?: ProjectFilter;
debug?: boolean;
Expand All @@ -30,7 +45,7 @@ export interface QwikNxVitePluginOptions {
*
* By default `qwikNxVite` plugin will provide Qwik with paths of all Nx projects, that are specified in the tsconfig.base.json.
* However, this behavior might not be always suitable, especially in cases when you have code that you don't want the optimizer to go through.
* It is possible to use specifically exclude or include certain projects using plugin options.
* It is possible to specifically exclude or include certain projects using plugin options.
*/
export function qwikNxVite(options?: QwikNxVitePluginOptions): Plugin {
const vitePlugin: Plugin = {
Expand All @@ -45,17 +60,49 @@ export function qwikNxVite(options?: QwikNxVitePluginOptions): Plugin {
throw new Error('Missing vite-plugin-qwik');
}

const vendorRoots = getVendorRoots(options);
const pluginOptions = qwikPlugin.api.getOptions();

qwikPlugin.api.getOptions().vendorRoots.push(...vendorRoots);
const vendorRoots = getVendorRoots(options, pluginOptions);

pluginOptions.vendorRoots.push(...vendorRoots);
},
};

return vitePlugin;
}

function getCurrentProjectName(
projectsConfigurations: ProjectsConfigurations,
projectRootDir: string
): string {
const projectRootMappings =
createProjectRootMappingsFromProjectConfigurations(
projectsConfigurations.projects
);
const relativeDirname = relative(workspaceRoot, projectRootDir);
const currentProjectName = findProjectForPath(
relativeDirname,
projectRootMappings
);

if (!currentProjectName) {
throw new Error(
`Could not resolve the project name for path "${projectRootDir}"`
);
}
return currentProjectName;
}

/** Retrieves vendor roots and applies necessary filtering */
function getVendorRoots(options?: QwikNxVitePluginOptions): string[] {
function getVendorRoots(
options: QwikNxVitePluginOptions | undefined,
qwikOptions: QwikVitePluginOptionsStub
): string[] {
const log = (...str: unknown[]) => {
(options?.debug || qwikOptions.debug) &&
console.debug(`[QWIK-NX-VITE PLUGIN:]`, ...str);
};

const workspaceConfig = readWorkspaceConfig({ format: 'nx' });

const baseTsConfig = JSON.parse(
Expand All @@ -67,13 +114,19 @@ function getVendorRoots(options?: QwikNxVitePluginOptions): string[] {

let projects = Object.values(workspaceConfig.projects);

const currentProjectName =
options?.currentProjectName ??
getCurrentProjectName(workspaceConfig, qwikOptions.rootDir);

projects = projects.filter((p) => p.name !== currentProjectName);

projects.forEach((p) => (p.sourceRoot ??= p.root));

projects = filterProjects(projects, options?.excludeProjects, true);
projects = filterProjects(projects, options?.includeProjects, false);

if (options?.debug) {
console.log(
log(
'Projects after applying include\\exclude filters:',
projects.map((p) => p.name)
);
Expand All @@ -84,7 +137,7 @@ function getVendorRoots(options?: QwikNxVitePluginOptions): string[] {
);

if (options?.debug) {
console.log(
log(
'Projects after excluding those not in tsconfig.base.json:',
projects.map((p) => p.name)
);
Expand Down

0 comments on commit 9456a9d

Please sign in to comment.