Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vite): PCV3 Plugin update to use resolveConfig #21287

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,6 @@ exports[`@nx/vite/plugin with test node root project should create nodes - with
".": {
"root": ".",
"targets": {
"build": {
"cache": true,
"command": "vite build",
"dependsOn": [
"^build",
],
"inputs": [
"production",
"^production",
{
"externalDependencies": [
"vite",
],
},
],
"options": {
"cwd": ".",
},
"outputs": [
"{projectRoot}/dist",
],
},
"preview": {
"command": "vite preview",
"options": {
"cwd": ".",
},
},
"serve": {
"command": "vite serve",
"options": {
"cwd": ".",
},
},
"serve-static": {
"executor": "@nx/web:file-server",
"options": {
"buildTarget": "build",
},
},
"test": {
"cache": true,
"command": "vitest run",
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/plugins/plugin-vitest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CreateNodesContext } from '@nx/devkit';
import { createNodes } from './plugin';

jest.mock('vite', () => ({
loadConfigFromFile: jest.fn().mockImplementation(() => {
resolveConfig: jest.fn().mockImplementation(() => {
return Promise.resolve({
path: 'vitest.config.ts',
config: {},
Expand All @@ -13,7 +13,7 @@ jest.mock('vite', () => ({

jest.mock('../utils/executor-utils', () => ({
loadViteDynamicImport: jest.fn().mockResolvedValue({
loadConfigFromFile: jest.fn().mockResolvedValue({
resolveConfig: jest.fn().mockResolvedValue({
path: 'vitest.config.ts',
config: {},
dependencies: [],
Expand Down
18 changes: 8 additions & 10 deletions packages/vite/src/plugins/plugin-with-test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { CreateNodesContext } from '@nx/devkit';
import { createNodes } from './plugin';

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

jest.mock('vite', () => ({
loadConfigFromFile: jest.fn().mockImplementation(() => {
resolveConfig: jest.fn().mockImplementation(() => {
return Promise.resolve({
path: 'vite.config.ts',
config: {
test: {
some: 'option',
},
test: {
some: 'option',
},
dependencies: [],
});
Expand All @@ -17,12 +17,10 @@ jest.mock('vite', () => ({

jest.mock('../utils/executor-utils', () => ({
loadViteDynamicImport: jest.fn().mockResolvedValue({
loadConfigFromFile: jest.fn().mockResolvedValue({
resolveConfig: jest.fn().mockResolvedValue({
path: 'vite.config.ts',
config: {
test: {
some: 'option',
},
test: {
some: 'option',
},
dependencies: [],
}),
Expand Down
12 changes: 9 additions & 3 deletions packages/vite/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import { createNodes } from './plugin';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';

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

jest.mock('../utils/executor-utils', () => ({
loadViteDynamicImport: jest.fn().mockResolvedValue({
loadConfigFromFile: jest.fn().mockResolvedValue({
resolveConfig: jest.fn().mockResolvedValue({
path: 'vite.config.ts',
config: {},
dependencies: [],
Expand All @@ -26,7 +27,9 @@ describe('@nx/vite/plugin', () => {
let createNodesFunction = createNodes[1];
let context: CreateNodesContext;
describe('root project', () => {
let tempFs;
beforeEach(async () => {
tempFs = new TempFs('');
context = {
nxJsonConfiguration: {
// These defaults should be overridden by plugin
Expand All @@ -43,6 +46,7 @@ describe('@nx/vite/plugin', () => {
},
workspaceRoot: '',
};
tempFs.createFileSync('index.html', '');
});

afterEach(() => {
Expand All @@ -67,8 +71,9 @@ describe('@nx/vite/plugin', () => {
});

describe('not root project', () => {
const tempFs = new TempFs('test');
let tempFs;
beforeEach(() => {
tempFs = new TempFs('test');
context = {
nxJsonConfiguration: {
namedInputs: {
Expand All @@ -84,6 +89,7 @@ describe('@nx/vite/plugin', () => {
JSON.stringify({ name: 'my-app' })
);
tempFs.createFileSync('my-app/vite.config.ts', '');
tempFs.createFileSync('my-app/index.html', '');
});

afterEach(() => {
Expand Down
29 changes: 20 additions & 9 deletions packages/vite/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,30 @@ async function buildViteTargets(
options: VitePluginOptions,
context: CreateNodesContext
) {
const { loadConfigFromFile } = await loadViteDynamicImport();
const viteConfig = await loadConfigFromFile(
const absoluteConfigFilePath = joinPathFragments(
context.workspaceRoot,
configFilePath
);
const { resolveConfig } = await loadViteDynamicImport();
const viteConfig = await resolveConfig(
{
command: 'build',
mode: 'production',
configFile: absoluteConfigFilePath,
mode: 'development',
},
configFilePath
'build'
);

const { buildOutputs, testOutputs, hasTest } = getOutputs(
viteConfig?.config,
const { buildOutputs, testOutputs, hasTest, isBuildable } = getOutputs(
viteConfig,
projectRoot
);

const namedInputs = getNamedInputs(projectRoot, context);

const targets: Record<string, TargetConfiguration> = {};

// If file is not vitest.config, create targets for build, serve, preview and serve-static
if (!configFilePath.includes('vitest.config')) {
// If file is not vitest.config and buildable, create targets for build, serve, preview and serve-static
if (!configFilePath.includes('vitest.config') && isBuildable) {
targets[options.buildTargetName] = await buildTarget(
options.buildTargetName,
namedInputs,
Expand Down Expand Up @@ -226,6 +230,7 @@ function getOutputs(
buildOutputs: string[];
testOutputs: string[];
hasTest: boolean;
isBuildable: boolean;
} {
const { build, test } = viteConfig;

Expand All @@ -235,6 +240,11 @@ function getOutputs(
'dist'
);

const isBuildable =
build?.lib ||
build?.rollupOptions?.inputs ||
existsSync(join(workspaceRoot, projectRoot, 'index.html'));

const reportsDirectoryPath = normalizeOutputPath(
test?.coverage?.reportsDirectory,
projectRoot,
Expand All @@ -245,6 +255,7 @@ function getOutputs(
buildOutputs: [buildOutputPath],
testOutputs: [reportsDirectoryPath],
hasTest: !!test,
isBuildable,
};
}

Expand Down