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 6, 2024
1 parent 94b1a21 commit 7382792
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 40 deletions.
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
69 changes: 53 additions & 16 deletions packages/vite/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
import { CreateNodesContext } from '@nx/devkit';
import { createNodes } from './plugin';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';

jest.mock('vite', () => ({
resolveConfig: jest.fn().mockImplementation(() => {
return Promise.resolve({
path: 'vite.config.ts',
config: {},
build: {},
dependencies: [],
});
}),
}));
import { loadViteDynamicImport } from '../utils/executor-utils';

jest.mock('../utils/executor-utils', () => ({
loadViteDynamicImport: jest.fn().mockResolvedValue({
resolveConfig: jest.fn().mockResolvedValue({
path: 'vite.config.ts',
config: {},
dependencies: [],
}),
resolveConfig: jest.fn().mockResolvedValue({}),
}),
}));

describe('@nx/vite/plugin', () => {
let createNodesFunction = createNodes[1];
let context: CreateNodesContext;

describe('root project', () => {
let tempFs;

beforeEach(async () => {
tempFs = new TempFs('vite-plugin-tests');
context = {
configFiles: [],
nxJsonConfiguration: {
// These defaults should be overridden by plugin
targetDefaults: {
Expand Down Expand Up @@ -73,9 +62,11 @@ describe('@nx/vite/plugin', () => {

describe('not root project', () => {
let tempFs;

beforeEach(() => {
tempFs = new TempFs('test');
context = {
configFiles: [],
nxJsonConfiguration: {
namedInputs: {
default: ['{projectRoot}/**/*'],
Expand Down Expand Up @@ -113,4 +104,50 @@ describe('@nx/vite/plugin', () => {
expect(nodes).toMatchSnapshot();
});
});

describe('Library mode', () => {
it('should exclude serve and preview targets when vite.config.ts is in library mode', async () => {
const tempFs = new TempFs('test');
(loadViteDynamicImport as jest.Mock).mockResolvedValue({
resolveConfig: jest.fn().mockResolvedValue({
build: {
lib: {
entry: 'index.ts',
name: 'my-lib',
},
},
}),
}),
(context = {
configFiles: [],
nxJsonConfiguration: {
namedInputs: {
default: ['{projectRoot}/**/*'],
production: ['!{projectRoot}/**/*.spec.ts'],
},
},
workspaceRoot: tempFs.tempDir,
});
tempFs.createFileSync(
'my-lib/project.json',
JSON.stringify({ name: 'my-lib' })
);
tempFs.createFileSync('my-lib/vite.config.ts', '');

const nodes = await createNodesFunction(
'my-lib/vite.config.ts',
{
buildTargetName: 'build',
serveTargetName: 'serve',
},
context
);

expect(nodes.projects['my-lib'].projectType).toEqual('library');
expect(nodes.projects['my-lib'].targets).toHaveProperty('build');
expect(nodes.projects['my-lib'].targets).not.toHaveProperty('serve');

jest.resetModules();
});
});
});
29 changes: 19 additions & 10 deletions packages/vite/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CreateNodesContext,
detectPackageManager,
joinPathFragments,
ProjectConfiguration,
readJsonFile,
TargetConfiguration,
writeJsonFile,
Expand Down Expand Up @@ -75,13 +76,21 @@ export const createNodes: CreateNodes<VitePluginOptions> = [
options,
context
);
const project: ProjectConfiguration = {
root: projectRoot,
targets: targetsCache[hash],
};

// If project is buildable, then the project type.
// If it is not buildable, then leave it to other plugins/project.json to set the project type.
if (project.targets[options.buildTargetName]) {
project.projectType = project.targets[options.serveTargetName]
? 'application'
: 'library';
}
return {
projects: {
[projectRoot]: {
root: projectRoot,
targets: targetsCache[hash],
},
[projectRoot]: project,
},
};
},
Expand All @@ -97,7 +106,6 @@ async function buildViteTargets(
context.workspaceRoot,
configFilePath
);

// Workaround for the `build$3 is not a function` error that we sometimes see in agents.
// This should be removed later once we address the issue properly
try {
Expand Down Expand Up @@ -140,11 +148,12 @@ async function buildViteTargets(
projectRoot
);

targets[options.serveTargetName] = serveTarget(projectRoot);

targets[options.previewTargetName] = previewTarget(projectRoot);

targets[options.serveStaticTargetName] = serveStaticTarget(options) as {};
// If running in library mode, then there is nothing to serve.
if (!viteConfig.build?.lib) {
targets[options.serveTargetName] = serveTarget(projectRoot);
targets[options.previewTargetName] = previewTarget(projectRoot);
targets[options.serveStaticTargetName] = serveStaticTarget(options) as {};
}
}

// if file is vitest.config or vite.config has definition for test, create target for test
Expand Down

0 comments on commit 7382792

Please sign in to comment.