Skip to content

Commit

Permalink
fix(bundling): set project type correct for buildable vite projects
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Jun 10, 2024
1 parent 94b1a21 commit 14e594b
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -835,3 +835,64 @@ export const Cart = {
},
},
};

// We're normalizing `type` from `projectType`, so if projectType is missing we'll fallback to `type`.
// See: packages/nx/src/project-graph/utils/normalize-project-nodes.ts
export const FallbackType = {
args: {
project: {
name: 'mypkg',
type: 'lib',
data: {
root: '.',
name: 'mypkg',
targets: {
echo: {
executor: 'nx:run-script',
metadata: {
scriptContent: 'echo 1',
runCommand: 'npm run echo',
},
options: {
script: 'echo',
},
configurations: {},
},
},
sourceRoot: '.',
implicitDependencies: [],
tags: [],
},
},
sourceMap: {
root: ['nx/core/project-json', 'project.json'],
name: ['nx/core/project-json', 'project.json'],
targets: ['nx/core/package-json', 'project.json'],
'targets.echo': ['nx/core/package-json-workspaces', 'package.json'],
'targets.echo.executor': [
'nx/core/package-json-workspaces',
'package.json',
],
'targets.echo.options': [
'nx/core/package-json-workspaces',
'package.json',
],
'targets.echo.metadata': [
'nx/core/package-json-workspaces',
'package.json',
],
'targets.echo.options.script': [
'nx/core/package-json-workspaces',
'package.json',
],
'targets.echo.metadata.scriptContent': [
'nx/core/package-json-workspaces',
'package.json',
],
'targets.echo.metadata.runCommand': [
'nx/core/package-json-workspaces',
'package.json',
],
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type { ProjectGraphProjectNode } from '@nx/devkit';
// nx-ignore-next-line
import { GraphError } from 'nx/src/command-line/graph/graph';
/* eslint-enable @nx/enforce-module-boundaries */

import { EyeIcon } from '@heroicons/react/24/outline';
import { PropertyInfoTooltip, Tooltip } from '@nx/graph/ui-tooltips';
import { TooltipTriggerText } from '../target-configuration-details/tooltip-trigger-text';
Expand All @@ -29,6 +28,24 @@ export interface ProjectDetailsProps {
viewInProjectGraphPosition?: 'top' | 'bottom';
}

const typeToProjectType = {
app: 'Application',
lib: 'Library',
e2e: 'E2E',
};

function getDisplayType(project: ProjectGraphProjectNode) {
if (project.data.projectType) {
return (
project.data.projectType &&
project.data.projectType?.charAt(0)?.toUpperCase() +
project.data.projectType?.slice(1)
);
} else {
return typeToProjectType[project.type] ?? 'Library';
}
}

export const ProjectDetails = ({
project,
sourceMap,
Expand All @@ -41,10 +58,7 @@ export const ProjectDetails = ({
const projectData = project.data;
const isCompact = variant === 'compact';

const displayType =
projectData.projectType &&
projectData.projectType?.charAt(0)?.toUpperCase() +
projectData.projectType?.slice(1);
const displayType = getDisplayType(project);

const technologies = [
...new Set(
Expand Down
1 change: 0 additions & 1 deletion packages/nx/src/config/to-project-name.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ describe('Workspaces', () => {
},
},
"name": "my-package",
"projectType": "library",
"root": "packages/my-package",
"sourceRoot": "packages/my-package",
"targets": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ describe('nx package.json workspaces plugin', () => {
},
},
"name": "root",
"projectType": "library",
"root": ".",
"sourceRoot": ".",
"targets": {
Expand Down Expand Up @@ -88,7 +87,6 @@ describe('nx package.json workspaces plugin', () => {
},
},
"name": "lib-a",
"projectType": "library",
"root": "packages/lib-a",
"sourceRoot": "packages/lib-a",
"targets": {
Expand Down Expand Up @@ -135,7 +133,6 @@ describe('nx package.json workspaces plugin', () => {
},
},
"name": "lib-b",
"projectType": "library",
"root": "packages/lib-b",
"sourceRoot": "packages/lib-b",
"targets": {
Expand Down Expand Up @@ -175,4 +172,91 @@ describe('nx package.json workspaces plugin', () => {
}
`);
});

it('should infer library and application project types from appsDir and libsDir', () => {
memfs.vol.fromJSON(
{
'nx.json': JSON.stringify({
workspaceLayout: {
appsDir: 'apps',
libsDir: 'packages',
},
}),
'apps/myapp/package.json': JSON.stringify({
name: 'myapp',
scripts: { test: 'jest' },
}),
'packages/mylib/package.json': JSON.stringify({
name: 'mylib',
scripts: { test: 'jest' },
}),
},
'/root'
);

expect(
createNodeFromPackageJson('apps/myapp/package.json', '/root').projects[
'apps/myapp'
].projectType
).toEqual('application');

expect(
createNodeFromPackageJson('packages/mylib/package.json', '/root')
.projects['packages/mylib'].projectType
).toEqual('library');
});

it('should infer library types for root library project if both appsDir and libsDir are set to empty string', () => {
memfs.vol.fromJSON(
{
'nx.json': JSON.stringify({
workspaceLayout: {
appsDir: '',
libsDir: '',
},
}),
'package.json': JSON.stringify({
name: 'mylib',
scripts: { test: 'jest' },
}),
},
'/root'
);

expect(
createNodeFromPackageJson('package.json', '/root').projects['.']
.projectType
).toEqual('library');
});

it('should infer library project type if only libsDir is set', () => {
memfs.vol.fromJSON(
{
'nx.json': JSON.stringify({
workspaceLayout: {
libsDir: 'packages',
},
}),
'example/package.json': JSON.stringify({
name: 'example',
scripts: { test: 'jest' },
}),
'packages/mylib/package.json': JSON.stringify({
name: 'mylib',
scripts: { test: 'jest' },
}),
},
'/root'
);

expect(
createNodeFromPackageJson('packages/mylib/package.json', '/root')
.projects['packages/mylib'].projectType
).toEqual('library');
expect(
createNodeFromPackageJson('example/package.json', '/root').projects[
'example'
].projectType
).toBeUndefined();
});
});
29 changes: 20 additions & 9 deletions packages/nx/src/plugins/package-json-workspaces/create-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { combineGlobPatterns } from '../../utils/globs';
import { NX_PREFIX } from '../../utils/logger';
import { output } from '../../utils/output';
import {
PackageJson,
getMetadataFromPackageJson,
PackageJson,
readTargetsFromPackageJson,
} from '../../utils/package-json';
import { joinPathFragments } from '../../utils/path';
Expand Down Expand Up @@ -88,22 +88,33 @@ export function buildProjectConfigurationFromPackageJson(
}

let name = packageJson.name ?? toProjectName(normalizedPath);
const projectType =
nxJson?.workspaceLayout?.appsDir != nxJson?.workspaceLayout?.libsDir &&
nxJson?.workspaceLayout?.appsDir &&
directory.startsWith(nxJson.workspaceLayout.appsDir)
? 'application'
: 'library';

return {
const projectConfiguration: ProjectConfiguration & { name: string } = {
root: directory,
sourceRoot: directory,
name,
projectType,
...packageJson.nx,
targets: readTargetsFromPackageJson(packageJson),
metadata: getMetadataFromPackageJson(packageJson),
};

if (
typeof nxJson?.workspaceLayout?.appsDir !== 'undefined' &&
typeof nxJson?.workspaceLayout?.libsDir !== 'undefined'
) {
projectConfiguration.projectType =
nxJson?.workspaceLayout?.appsDir != nxJson?.workspaceLayout?.libsDir &&
directory.startsWith(nxJson.workspaceLayout.appsDir)
? 'application'
: 'library';
} else if (
typeof nxJson?.workspaceLayout?.libsDir !== 'undefined' &&
directory.startsWith(nxJson.workspaceLayout.libsDir)
) {
projectConfiguration.projectType = 'library';
}

return projectConfiguration;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/plugins/__snapshots__/plugin.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`@nx/vite/plugin not root project should create nodes 1`] = `
{
"projects": {
"my-app": {
"projectType": "application",
"root": "my-app",
"targets": {
"build-something": {
Expand Down Expand Up @@ -57,6 +58,7 @@ exports[`@nx/vite/plugin root project should create nodes 1`] = `
{
"projects": {
".": {
"projectType": "application",
"root": ".",
"targets": {
"build": {
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/plugins/plugin-vitest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('@nx/vite/plugin', () => {
describe('root project', () => {
beforeEach(async () => {
context = {
configFiles: [],
nxJsonConfiguration: {
targetDefaults: {},
namedInputs: {
Expand Down
15 changes: 1 addition & 14 deletions packages/vite/src/plugins/plugin-with-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,12 @@ import { createNodes } from './plugin';

// This will only create test targets since no build targets are defined in vite.config.ts

jest.mock('vite', () => ({
resolveConfig: jest.fn().mockImplementation(() => {
return Promise.resolve({
path: 'vite.config.ts',
test: {
some: 'option',
},
dependencies: [],
});
}),
}));

jest.mock('../utils/executor-utils', () => ({
loadViteDynamicImport: jest.fn().mockResolvedValue({
resolveConfig: jest.fn().mockResolvedValue({
path: 'vite.config.ts',
test: {
some: 'option',
},
dependencies: [],
}),
}),
}));
Expand All @@ -33,6 +19,7 @@ describe('@nx/vite/plugin with test node', () => {
describe('root project', () => {
beforeEach(async () => {
context = {
configFiles: [],
nxJsonConfiguration: {
// These defaults should be overridden by plugin
targetDefaults: {
Expand Down
Loading

0 comments on commit 14e594b

Please sign in to comment.