Skip to content

Commit e2a6793

Browse files
feat(qwik-nx): qwikNxVite plugin to include only project's dependencies by default (#126)
* feat(qwik-nx): qwikNxVite plugin to include only project's dependencies by default * test: return new instance of a mock object for each test
1 parent ff1de6b commit e2a6793

File tree

8 files changed

+370
-287
lines changed

8 files changed

+370
-287
lines changed

packages/qwik-nx/src/plugins/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
export * from './qwik-nx-vite.plugin';
2+
export type {
3+
ProjectFilter,
4+
QwikNxVitePluginOptions,
5+
} from './models/qwik-nx-vite';
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { ProjectConfiguration } from '@nrwl/devkit';
2+
3+
export interface ProjectFilter {
4+
name?: string[] | RegExp;
5+
path?: RegExp;
6+
tags?: string[];
7+
customFilter?: (project: ProjectConfiguration) => boolean;
8+
}
9+
10+
export interface QwikNxVitePluginOptions {
11+
/**
12+
* Name of the project, with which plugin is used. By default it will be resolved using the path of the `rootDir` from the Vite environment.
13+
*/
14+
currentProjectName?: string;
15+
/**
16+
* Projects to be included as vendor roots for Qwik to be able to run optimizer over them.
17+
* If not specified, will include all projects that are recognized as dependencies of the given `currentProjectName`.
18+
*/
19+
includeProjects?: ProjectFilter;
20+
/**
21+
* Projects to be excluded from the list of resolved vendor roots.
22+
*/
23+
excludeProjects?: ProjectFilter;
24+
debug?: boolean;
25+
}
26+
27+
export interface QwikVitePluginStub {
28+
api: {
29+
getOptions: () => QwikVitePluginOptionsStub;
30+
};
31+
}
32+
33+
export interface QwikVitePluginOptionsStub {
34+
vendorRoots: string[];
35+
rootDir: string;
36+
debug: boolean;
37+
}

packages/qwik-nx/src/plugins/qwik-nx-vite.plugin.spec.ts

Lines changed: 86 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,103 @@
11
import { workspaceRoot } from '@nrwl/devkit';
22
import { join } from 'path';
3-
import { qwikNxVite, QwikNxVitePluginOptions } from './qwik-nx-vite.plugin';
3+
import { qwikNxVite } from './qwik-nx-vite.plugin';
4+
import { QwikNxVitePluginOptions } from './models/qwik-nx-vite';
45

56
// eslint-disable-next-line @typescript-eslint/no-var-requires
67
const fileUtils = require('nx/src/project-graph/file-utils');
78
// eslint-disable-next-line @typescript-eslint/no-var-requires
89
const fs = require('fs');
10+
// eslint-disable-next-line @typescript-eslint/no-var-requires
11+
const getProjectDependenciesModule = require('./utils/get-project-dependencies');
912

10-
const workspaceConfig1 = {
11-
projects: {
12-
'tmp-test-app-a': {
13-
root: 'apps/test-app-a',
14-
name: 'tmp-test-app-a',
15-
projectType: 'application',
16-
sourceRoot: 'apps/test-app-a/src',
17-
prefix: 'tmp',
18-
tags: ['tag1', 'tag2'],
19-
},
20-
'tmp-test-lib-a': {
21-
root: 'libs/test-lib-a',
22-
name: 'tmp-test-lib-a',
23-
projectType: 'library',
24-
sourceRoot: 'libs/test-lib-a/src',
25-
prefix: 'tmp',
26-
tags: ['tag2'],
27-
},
28-
'tmp-test-lib-b': {
29-
root: 'libs/test-lib-b',
30-
name: 'tmp-test-lib-b',
31-
projectType: 'library',
32-
sourceRoot: 'libs/test-lib-b/src',
33-
prefix: 'tmp',
34-
tags: ['tag2', 'tag3'],
35-
},
36-
'tmp-test-lib-c-nested-1': {
37-
root: 'libs/test-lib-c/nested',
38-
name: 'tmp-test-lib-c-nested-1',
39-
projectType: 'library',
40-
sourceRoot: 'libs/test-lib-c/nested-1/src',
41-
prefix: 'tmp',
42-
tags: ['tag4'],
43-
},
44-
'tmp-test-lib-c-nested-2': {
45-
root: 'libs/test-lib-c/nested',
46-
name: 'tmp-test-lib-c-nested-2',
47-
projectType: 'library',
48-
sourceRoot: 'libs/test-lib-c/nested-2/src',
49-
prefix: 'tmp',
50-
tags: ['tag1', 'tag2', 'tag3'],
51-
},
52-
'tmp-other-test-lib-a': {
53-
root: 'libs/other/test-lib-a',
54-
name: 'tmp-other-test-lib-a',
55-
projectType: 'library',
56-
sourceRoot: 'libs/other/test-lib-a/src',
57-
prefix: 'tmp',
58-
tags: [],
59-
},
60-
// it will be excluded because it is not set in our mock tsconfig.json
61-
'tmp-always-excluded': {
62-
root: 'libs/always/excluded',
63-
name: 'tmp-always-excluded',
64-
projectType: 'library',
65-
sourceRoot: 'libs/always/excluded/src',
66-
prefix: 'tmp',
67-
tags: [],
13+
function getWorkspaceConfig() {
14+
return {
15+
projects: {
16+
'tmp-test-app-a': {
17+
root: 'apps/test-app-a',
18+
name: 'tmp-test-app-a',
19+
projectType: 'application',
20+
sourceRoot: 'apps/test-app-a/src',
21+
prefix: 'tmp',
22+
tags: ['tag1', 'tag2'],
23+
},
24+
'tmp-test-lib-a': {
25+
root: 'libs/test-lib-a',
26+
name: 'tmp-test-lib-a',
27+
projectType: 'library',
28+
sourceRoot: 'libs/test-lib-a/src',
29+
prefix: 'tmp',
30+
tags: ['tag2'],
31+
},
32+
'tmp-test-lib-b': {
33+
root: 'libs/test-lib-b',
34+
name: 'tmp-test-lib-b',
35+
projectType: 'library',
36+
sourceRoot: 'libs/test-lib-b/src',
37+
prefix: 'tmp',
38+
tags: ['tag2', 'tag3'],
39+
},
40+
'tmp-test-lib-c-nested-1': {
41+
root: 'libs/test-lib-c/nested',
42+
name: 'tmp-test-lib-c-nested-1',
43+
projectType: 'library',
44+
sourceRoot: 'libs/test-lib-c/nested-1/src',
45+
prefix: 'tmp',
46+
tags: ['tag4'],
47+
},
48+
'tmp-test-lib-c-nested-2': {
49+
root: 'libs/test-lib-c/nested',
50+
name: 'tmp-test-lib-c-nested-2',
51+
projectType: 'library',
52+
sourceRoot: 'libs/test-lib-c/nested-2/src',
53+
prefix: 'tmp',
54+
tags: ['tag1', 'tag2', 'tag3'],
55+
},
56+
'tmp-other-test-lib-a': {
57+
root: 'libs/other/test-lib-a',
58+
name: 'tmp-other-test-lib-a',
59+
projectType: 'library',
60+
sourceRoot: 'libs/other/test-lib-a/src',
61+
prefix: 'tmp',
62+
tags: [],
63+
},
64+
// it will be excluded because it is not set in our mock tsconfig.json
65+
'tmp-always-excluded': {
66+
root: 'libs/always/excluded',
67+
name: 'tmp-always-excluded',
68+
projectType: 'library',
69+
sourceRoot: 'libs/always/excluded/src',
70+
prefix: 'tmp',
71+
tags: [],
72+
},
6873
},
69-
},
70-
};
74+
};
75+
}
7176

72-
const tsConfigString1 = JSON.stringify({
73-
compilerOptions: {
74-
paths: {
75-
'@tmp/test-lib-a': 'libs/test-lib-a/src/index.ts',
76-
'@tmp/test-lib-b': 'libs/test-lib-b/src/index.ts',
77-
'@tmp/test-lib-c/nested-1': 'libs/test-lib-c/nested-1/src/index.ts',
78-
'@tmp/test-lib-c/nested-2': 'libs/test-lib-c/nested-2/src/index.ts',
79-
'@tmp/other/test-lib-a/nested-2': 'libs/other/test-lib-a/src/index.ts',
77+
function getTsConfigString() {
78+
return JSON.stringify({
79+
compilerOptions: {
80+
paths: {
81+
'@tmp/test-lib-a': 'libs/test-lib-a/src/index.ts',
82+
'@tmp/test-lib-b': 'libs/test-lib-b/src/index.ts',
83+
'@tmp/test-lib-c/nested-1': 'libs/test-lib-c/nested-1/src/index.ts',
84+
'@tmp/test-lib-c/nested-2': 'libs/test-lib-c/nested-2/src/index.ts',
85+
'@tmp/other/test-lib-a/nested-2': 'libs/other/test-lib-a/src/index.ts',
86+
},
8087
},
81-
},
82-
});
88+
});
89+
}
8390

8491
describe('qwik-nx-vite plugin', () => {
8592
jest
8693
.spyOn(fileUtils, 'readWorkspaceConfig')
87-
.mockReturnValue(workspaceConfig1);
88-
jest.spyOn(fs, 'readFileSync').mockReturnValue(tsConfigString1);
94+
.mockReturnValue(getWorkspaceConfig());
95+
jest.spyOn(fs, 'readFileSync').mockReturnValue(getTsConfigString());
96+
jest
97+
.spyOn(getProjectDependenciesModule, 'getProjectDependencies')
98+
.mockReturnValue(
99+
Promise.resolve(new Set(Object.keys(getWorkspaceConfig().projects)))
100+
);
89101

90102
/**
91103
* @param options options for the qwikNxVite plugin
@@ -132,7 +144,7 @@ describe('qwik-nx-vite plugin', () => {
132144
]);
133145
});
134146

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

0 commit comments

Comments
 (0)