Skip to content

Commit 32aa8a1

Browse files
jaysooFrozenPandaz
authored andcommitted
fix(bundling): set project type correct for buildable vite projects (#26420)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> This PR: 1. fixes `@nx/vite/plugin` so that it infers the correct `projectType` 2. changes the default `package-json-workspaces` plugin such that `projectType` is not inferred is `appsDir` and `libsDir` are not set 3. Update PDV to look for the normalized `type` property on projects if `projectType` is missing (due to no longer being inferred by any plugin). ## Current Behavior <!-- This is the behavior we have today --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes # (cherry picked from commit fbd7f80)
1 parent b0c82e6 commit 32aa8a1

File tree

10 files changed

+300
-69
lines changed

10 files changed

+300
-69
lines changed

graph/ui-project-details/src/lib/project-details/project-details.stories.tsx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,3 +835,64 @@ export const Cart = {
835835
},
836836
},
837837
};
838+
839+
// We're normalizing `type` from `projectType`, so if projectType is missing we'll fallback to `type`.
840+
// See: packages/nx/src/project-graph/utils/normalize-project-nodes.ts
841+
export const FallbackType = {
842+
args: {
843+
project: {
844+
name: 'mypkg',
845+
type: 'lib',
846+
data: {
847+
root: '.',
848+
name: 'mypkg',
849+
targets: {
850+
echo: {
851+
executor: 'nx:run-script',
852+
metadata: {
853+
scriptContent: 'echo 1',
854+
runCommand: 'npm run echo',
855+
},
856+
options: {
857+
script: 'echo',
858+
},
859+
configurations: {},
860+
},
861+
},
862+
sourceRoot: '.',
863+
implicitDependencies: [],
864+
tags: [],
865+
},
866+
},
867+
sourceMap: {
868+
root: ['nx/core/project-json', 'project.json'],
869+
name: ['nx/core/project-json', 'project.json'],
870+
targets: ['nx/core/package-json', 'project.json'],
871+
'targets.echo': ['nx/core/package-json-workspaces', 'package.json'],
872+
'targets.echo.executor': [
873+
'nx/core/package-json-workspaces',
874+
'package.json',
875+
],
876+
'targets.echo.options': [
877+
'nx/core/package-json-workspaces',
878+
'package.json',
879+
],
880+
'targets.echo.metadata': [
881+
'nx/core/package-json-workspaces',
882+
'package.json',
883+
],
884+
'targets.echo.options.script': [
885+
'nx/core/package-json-workspaces',
886+
'package.json',
887+
],
888+
'targets.echo.metadata.scriptContent': [
889+
'nx/core/package-json-workspaces',
890+
'package.json',
891+
],
892+
'targets.echo.metadata.runCommand': [
893+
'nx/core/package-json-workspaces',
894+
'package.json',
895+
],
896+
},
897+
},
898+
};

graph/ui-project-details/src/lib/project-details/project-details.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { ProjectGraphProjectNode } from '@nx/devkit';
66
// nx-ignore-next-line
77
import { GraphError } from 'nx/src/command-line/graph/graph';
88
/* eslint-enable @nx/enforce-module-boundaries */
9-
109
import { EyeIcon } from '@heroicons/react/24/outline';
1110
import { PropertyInfoTooltip, Tooltip } from '@nx/graph/ui-tooltips';
1211
import { TooltipTriggerText } from '../target-configuration-details/tooltip-trigger-text';
@@ -29,6 +28,24 @@ export interface ProjectDetailsProps {
2928
viewInProjectGraphPosition?: 'top' | 'bottom';
3029
}
3130

31+
const typeToProjectType = {
32+
app: 'Application',
33+
lib: 'Library',
34+
e2e: 'E2E',
35+
};
36+
37+
function getDisplayType(project: ProjectGraphProjectNode) {
38+
if (project.data.projectType) {
39+
return (
40+
project.data.projectType &&
41+
project.data.projectType?.charAt(0)?.toUpperCase() +
42+
project.data.projectType?.slice(1)
43+
);
44+
} else {
45+
return typeToProjectType[project.type] ?? 'Library';
46+
}
47+
}
48+
3249
export const ProjectDetails = ({
3350
project,
3451
sourceMap,
@@ -41,10 +58,7 @@ export const ProjectDetails = ({
4158
const projectData = project.data;
4259
const isCompact = variant === 'compact';
4360

44-
const displayType =
45-
projectData.projectType &&
46-
projectData.projectType?.charAt(0)?.toUpperCase() +
47-
projectData.projectType?.slice(1);
61+
const displayType = getDisplayType(project);
4862

4963
const technologies = [
5064
...new Set(

packages/nx/src/config/to-project-name.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ describe('Workspaces', () => {
5959
},
6060
},
6161
"name": "my-package",
62-
"projectType": "library",
6362
"root": "packages/my-package",
6463
"sourceRoot": "packages/my-package",
6564
"targets": {

packages/nx/src/plugins/package-json-workspaces/create-nodes.spec.ts

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ describe('nx package.json workspaces plugin', () => {
5959
},
6060
},
6161
"name": "root",
62-
"projectType": "library",
6362
"root": ".",
6463
"sourceRoot": ".",
6564
"targets": {
@@ -98,7 +97,6 @@ describe('nx package.json workspaces plugin', () => {
9897
},
9998
},
10099
"name": "lib-a",
101-
"projectType": "library",
102100
"root": "packages/lib-a",
103101
"sourceRoot": "packages/lib-a",
104102
"targets": {
@@ -145,7 +143,6 @@ describe('nx package.json workspaces plugin', () => {
145143
},
146144
},
147145
"name": "lib-b",
148-
"projectType": "library",
149146
"root": "packages/lib-b",
150147
"sourceRoot": "packages/lib-b",
151148
"targets": {
@@ -235,7 +232,6 @@ describe('nx package.json workspaces plugin', () => {
235232
},
236233
},
237234
"name": "vite",
238-
"projectType": "library",
239235
"root": "packages/vite",
240236
"sourceRoot": "packages/vite",
241237
"targets": {
@@ -322,7 +318,6 @@ describe('nx package.json workspaces plugin', () => {
322318
},
323319
},
324320
"name": "vite",
325-
"projectType": "library",
326321
"root": "packages/vite",
327322
"sourceRoot": "packages/vite",
328323
"targets": {
@@ -405,7 +400,6 @@ describe('nx package.json workspaces plugin', () => {
405400
},
406401
},
407402
"name": "vite",
408-
"projectType": "library",
409403
"root": "packages/vite",
410404
"sourceRoot": "packages/vite",
411405
"targets": {
@@ -478,7 +472,6 @@ describe('nx package.json workspaces plugin', () => {
478472
},
479473
},
480474
"name": "root",
481-
"projectType": "library",
482475
"root": "packages/a",
483476
"sourceRoot": "packages/a",
484477
"targets": {
@@ -543,7 +536,6 @@ describe('nx package.json workspaces plugin', () => {
543536
},
544537
},
545538
"name": "root",
546-
"projectType": "library",
547539
"root": "packages/a",
548540
"sourceRoot": "packages/a",
549541
"targets": {
@@ -606,7 +598,6 @@ describe('nx package.json workspaces plugin', () => {
606598
},
607599
},
608600
"name": "root",
609-
"projectType": "library",
610601
"root": "packages/a",
611602
"sourceRoot": "packages/a",
612603
"targets": {
@@ -624,4 +615,91 @@ describe('nx package.json workspaces plugin', () => {
624615
`);
625616
});
626617
});
618+
619+
it('should infer library and application project types from appsDir and libsDir', () => {
620+
vol.fromJSON(
621+
{
622+
'nx.json': JSON.stringify({
623+
workspaceLayout: {
624+
appsDir: 'apps',
625+
libsDir: 'packages',
626+
},
627+
}),
628+
'apps/myapp/package.json': JSON.stringify({
629+
name: 'myapp',
630+
scripts: { test: 'jest' },
631+
}),
632+
'packages/mylib/package.json': JSON.stringify({
633+
name: 'mylib',
634+
scripts: { test: 'jest' },
635+
}),
636+
},
637+
'/root'
638+
);
639+
640+
expect(
641+
createNodeFromPackageJson('apps/myapp/package.json', '/root').projects[
642+
'apps/myapp'
643+
].projectType
644+
).toEqual('application');
645+
646+
expect(
647+
createNodeFromPackageJson('packages/mylib/package.json', '/root')
648+
.projects['packages/mylib'].projectType
649+
).toEqual('library');
650+
});
651+
652+
it('should infer library types for root library project if both appsDir and libsDir are set to empty string', () => {
653+
vol.fromJSON(
654+
{
655+
'nx.json': JSON.stringify({
656+
workspaceLayout: {
657+
appsDir: '',
658+
libsDir: '',
659+
},
660+
}),
661+
'package.json': JSON.stringify({
662+
name: 'mylib',
663+
scripts: { test: 'jest' },
664+
}),
665+
},
666+
'/root'
667+
);
668+
669+
expect(
670+
createNodeFromPackageJson('package.json', '/root').projects['.']
671+
.projectType
672+
).toEqual('library');
673+
});
674+
675+
it('should infer library project type if only libsDir is set', () => {
676+
vol.fromJSON(
677+
{
678+
'nx.json': JSON.stringify({
679+
workspaceLayout: {
680+
libsDir: 'packages',
681+
},
682+
}),
683+
'example/package.json': JSON.stringify({
684+
name: 'example',
685+
scripts: { test: 'jest' },
686+
}),
687+
'packages/mylib/package.json': JSON.stringify({
688+
name: 'mylib',
689+
scripts: { test: 'jest' },
690+
}),
691+
},
692+
'/root'
693+
);
694+
695+
expect(
696+
createNodeFromPackageJson('packages/mylib/package.json', '/root')
697+
.projects['packages/mylib'].projectType
698+
).toEqual('library');
699+
expect(
700+
createNodeFromPackageJson('example/package.json', '/root').projects[
701+
'example'
702+
].projectType
703+
).toBeUndefined();
704+
});
627705
});

packages/nx/src/plugins/package-json-workspaces/create-nodes.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import { combineGlobPatterns } from '../../utils/globs';
1010
import { NX_PREFIX } from '../../utils/logger';
1111
import { output } from '../../utils/output';
1212
import {
13-
PackageJson,
1413
getMetadataFromPackageJson,
14+
PackageJson,
1515
readTargetsFromPackageJson,
1616
} from '../../utils/package-json';
1717
import { joinPathFragments } from '../../utils/path';
@@ -112,22 +112,30 @@ export function buildProjectConfigurationFromPackageJson(
112112
}
113113

114114
let name = packageJson.name ?? toProjectName(normalizedPath);
115-
const projectType =
116-
nxJson?.workspaceLayout?.appsDir != nxJson?.workspaceLayout?.libsDir &&
117-
nxJson?.workspaceLayout?.appsDir &&
118-
projectRoot.startsWith(nxJson.workspaceLayout.appsDir)
119-
? 'application'
120-
: 'library';
121115

122-
return {
116+
const projectConfiguration: ProjectConfiguration & { name: string } = {
123117
root: projectRoot,
124118
sourceRoot: projectRoot,
125119
name,
126-
projectType,
127120
...packageJson.nx,
128121
targets: readTargetsFromPackageJson(packageJson),
129122
metadata: getMetadataFromPackageJson(packageJson),
130123
};
124+
125+
if (
126+
nxJson?.workspaceLayout?.appsDir != nxJson?.workspaceLayout?.libsDir &&
127+
nxJson?.workspaceLayout?.appsDir &&
128+
projectRoot.startsWith(nxJson.workspaceLayout.appsDir)
129+
) {
130+
projectConfiguration.projectType = 'application';
131+
} else if (
132+
typeof nxJson?.workspaceLayout?.libsDir !== 'undefined' &&
133+
projectRoot.startsWith(nxJson.workspaceLayout.libsDir)
134+
) {
135+
projectConfiguration.projectType = 'library';
136+
}
137+
138+
return projectConfiguration;
131139
}
132140

133141
/**

packages/vite/src/plugins/__snapshots__/plugin.spec.ts.snap

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`@nx/vite/plugin Library mode should exclude serve and preview targets when vite.config.ts is in library mode 1`] = `
4+
[
5+
[
6+
"my-lib/vite.config.ts",
7+
{
8+
"projects": {
9+
"my-lib": {
10+
"metadata": {},
11+
"projectType": "library",
12+
"root": "my-lib",
13+
"targets": {
14+
"build": {
15+
"cache": true,
16+
"command": "vite build",
17+
"dependsOn": [
18+
"^build",
19+
],
20+
"inputs": [
21+
"production",
22+
"^production",
23+
{
24+
"externalDependencies": [
25+
"vite",
26+
],
27+
},
28+
],
29+
"options": {
30+
"cwd": "my-lib",
31+
},
32+
"outputs": [
33+
"{workspaceRoot}/dist/{projectRoot}",
34+
],
35+
},
36+
},
37+
},
38+
},
39+
},
40+
],
41+
]
42+
`;
43+
344
exports[`@nx/vite/plugin not root project should create nodes 1`] = `
445
[
546
[
@@ -8,6 +49,7 @@ exports[`@nx/vite/plugin not root project should create nodes 1`] = `
849
"projects": {
950
"my-app": {
1051
"metadata": {},
52+
"projectType": "application",
1153
"root": "my-app",
1254
"targets": {
1355
"build-something": {
@@ -67,6 +109,7 @@ exports[`@nx/vite/plugin root project should create nodes 1`] = `
67109
"projects": {
68110
".": {
69111
"metadata": {},
112+
"projectType": "application",
70113
"root": ".",
71114
"targets": {
72115
"build": {

0 commit comments

Comments
 (0)