feat(vite): add createNodesV2 function (#26484)
- chore(vite): move single file processing of plugin to own function - feat(vite): add CreateNodesV2 function <!-- 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` --> ## Current Behavior <!-- This is the behavior we have today --> There is no implementation for createNodesV2 ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> There should be an implementation for createNodesV2 ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
This commit is contained in:
parent
bcac55178f
commit
07fa8a6ffa
@ -1,5 +1,6 @@
|
||||
export {
|
||||
createNodes,
|
||||
createNodesV2,
|
||||
VitePluginOptions,
|
||||
createDependencies,
|
||||
} from './src/plugins/plugin';
|
||||
|
||||
@ -1,35 +1,41 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`@nx/vite/plugin root project should create nodes 1`] = `
|
||||
{
|
||||
"projects": {
|
||||
".": {
|
||||
"root": ".",
|
||||
"targets": {
|
||||
"test": {
|
||||
"cache": true,
|
||||
"command": "vitest",
|
||||
"inputs": [
|
||||
"default",
|
||||
"^production",
|
||||
{
|
||||
"externalDependencies": [
|
||||
"vitest",
|
||||
[
|
||||
[
|
||||
"vitest.config.ts",
|
||||
{
|
||||
"projects": {
|
||||
".": {
|
||||
"metadata": {},
|
||||
"root": ".",
|
||||
"targets": {
|
||||
"test": {
|
||||
"cache": true,
|
||||
"command": "vitest",
|
||||
"inputs": [
|
||||
"default",
|
||||
"^production",
|
||||
{
|
||||
"externalDependencies": [
|
||||
"vitest",
|
||||
],
|
||||
},
|
||||
{
|
||||
"env": "CI",
|
||||
},
|
||||
],
|
||||
"options": {
|
||||
"cwd": ".",
|
||||
},
|
||||
"outputs": [
|
||||
"{projectRoot}/coverage",
|
||||
],
|
||||
},
|
||||
{
|
||||
"env": "CI",
|
||||
},
|
||||
],
|
||||
"options": {
|
||||
"cwd": ".",
|
||||
},
|
||||
"outputs": [
|
||||
"{projectRoot}/coverage",
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
],
|
||||
]
|
||||
`;
|
||||
|
||||
@ -1,35 +1,41 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`@nx/vite/plugin with test node root project should create nodes - with test too 1`] = `
|
||||
{
|
||||
"projects": {
|
||||
".": {
|
||||
"root": ".",
|
||||
"targets": {
|
||||
"test": {
|
||||
"cache": true,
|
||||
"command": "vitest",
|
||||
"inputs": [
|
||||
"default",
|
||||
"^production",
|
||||
{
|
||||
"externalDependencies": [
|
||||
"vitest",
|
||||
[
|
||||
[
|
||||
"vite.config.ts",
|
||||
{
|
||||
"projects": {
|
||||
".": {
|
||||
"metadata": {},
|
||||
"root": ".",
|
||||
"targets": {
|
||||
"test": {
|
||||
"cache": true,
|
||||
"command": "vitest",
|
||||
"inputs": [
|
||||
"default",
|
||||
"^production",
|
||||
{
|
||||
"externalDependencies": [
|
||||
"vitest",
|
||||
],
|
||||
},
|
||||
{
|
||||
"env": "CI",
|
||||
},
|
||||
],
|
||||
"options": {
|
||||
"cwd": ".",
|
||||
},
|
||||
"outputs": [
|
||||
"{projectRoot}/coverage",
|
||||
],
|
||||
},
|
||||
{
|
||||
"env": "CI",
|
||||
},
|
||||
],
|
||||
"options": {
|
||||
"cwd": ".",
|
||||
},
|
||||
"outputs": [
|
||||
"{projectRoot}/coverage",
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
],
|
||||
]
|
||||
`;
|
||||
|
||||
@ -1,107 +1,119 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`@nx/vite/plugin not root project should create nodes 1`] = `
|
||||
{
|
||||
"projects": {
|
||||
"my-app": {
|
||||
"root": "my-app",
|
||||
"targets": {
|
||||
"build-something": {
|
||||
"cache": true,
|
||||
"command": "vite build",
|
||||
"dependsOn": [
|
||||
"^build-something",
|
||||
],
|
||||
"inputs": [
|
||||
"production",
|
||||
"^production",
|
||||
{
|
||||
"externalDependencies": [
|
||||
"vite",
|
||||
[
|
||||
[
|
||||
"my-app/vite.config.ts",
|
||||
{
|
||||
"projects": {
|
||||
"my-app": {
|
||||
"metadata": {},
|
||||
"root": "my-app",
|
||||
"targets": {
|
||||
"build-something": {
|
||||
"cache": true,
|
||||
"command": "vite build",
|
||||
"dependsOn": [
|
||||
"^build-something",
|
||||
],
|
||||
"inputs": [
|
||||
"production",
|
||||
"^production",
|
||||
{
|
||||
"externalDependencies": [
|
||||
"vite",
|
||||
],
|
||||
},
|
||||
],
|
||||
"options": {
|
||||
"cwd": "my-app",
|
||||
},
|
||||
"outputs": [
|
||||
"{workspaceRoot}/dist/{projectRoot}",
|
||||
],
|
||||
},
|
||||
],
|
||||
"options": {
|
||||
"cwd": "my-app",
|
||||
},
|
||||
"outputs": [
|
||||
"{workspaceRoot}/dist/{projectRoot}",
|
||||
],
|
||||
},
|
||||
"my-serve": {
|
||||
"command": "vite serve",
|
||||
"options": {
|
||||
"cwd": "my-app",
|
||||
},
|
||||
},
|
||||
"preview-site": {
|
||||
"command": "vite preview",
|
||||
"options": {
|
||||
"cwd": "my-app",
|
||||
},
|
||||
},
|
||||
"serve-static": {
|
||||
"executor": "@nx/web:file-server",
|
||||
"options": {
|
||||
"buildTarget": "build-something",
|
||||
"spa": true,
|
||||
"my-serve": {
|
||||
"command": "vite serve",
|
||||
"options": {
|
||||
"cwd": "my-app",
|
||||
},
|
||||
},
|
||||
"preview-site": {
|
||||
"command": "vite preview",
|
||||
"options": {
|
||||
"cwd": "my-app",
|
||||
},
|
||||
},
|
||||
"serve-static": {
|
||||
"executor": "@nx/web:file-server",
|
||||
"options": {
|
||||
"buildTarget": "build-something",
|
||||
"spa": true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
],
|
||||
]
|
||||
`;
|
||||
|
||||
exports[`@nx/vite/plugin root project should create nodes 1`] = `
|
||||
{
|
||||
"projects": {
|
||||
".": {
|
||||
"root": ".",
|
||||
"targets": {
|
||||
"build": {
|
||||
"cache": true,
|
||||
"command": "vite build",
|
||||
"dependsOn": [
|
||||
"^build",
|
||||
],
|
||||
"inputs": [
|
||||
"production",
|
||||
"^production",
|
||||
{
|
||||
"externalDependencies": [
|
||||
"vite",
|
||||
[
|
||||
[
|
||||
"vite.config.ts",
|
||||
{
|
||||
"projects": {
|
||||
".": {
|
||||
"metadata": {},
|
||||
"root": ".",
|
||||
"targets": {
|
||||
"build": {
|
||||
"cache": true,
|
||||
"command": "vite build",
|
||||
"dependsOn": [
|
||||
"^build",
|
||||
],
|
||||
"inputs": [
|
||||
"production",
|
||||
"^production",
|
||||
{
|
||||
"externalDependencies": [
|
||||
"vite",
|
||||
],
|
||||
},
|
||||
],
|
||||
"options": {
|
||||
"cwd": ".",
|
||||
},
|
||||
"outputs": [
|
||||
"{projectRoot}/dist",
|
||||
],
|
||||
},
|
||||
],
|
||||
"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",
|
||||
"spa": true,
|
||||
"preview": {
|
||||
"command": "vite preview",
|
||||
"options": {
|
||||
"cwd": ".",
|
||||
},
|
||||
},
|
||||
"serve": {
|
||||
"command": "vite serve",
|
||||
"options": {
|
||||
"cwd": ".",
|
||||
},
|
||||
},
|
||||
"serve-static": {
|
||||
"executor": "@nx/web:file-server",
|
||||
"options": {
|
||||
"buildTarget": "build",
|
||||
"spa": true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
],
|
||||
]
|
||||
`;
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { CreateNodesContext } from '@nx/devkit';
|
||||
import { createNodes } from './plugin';
|
||||
import { createNodesV2 } from './plugin';
|
||||
|
||||
jest.mock('vite', () => ({
|
||||
resolveConfig: jest.fn().mockImplementation(() => {
|
||||
@ -22,7 +22,7 @@ jest.mock('../utils/executor-utils', () => ({
|
||||
}));
|
||||
|
||||
describe('@nx/vite/plugin', () => {
|
||||
let createNodesFunction = createNodes[1];
|
||||
let createNodesFunction = createNodesV2[1];
|
||||
let context: CreateNodesContext;
|
||||
describe('root project', () => {
|
||||
beforeEach(async () => {
|
||||
@ -35,6 +35,7 @@ describe('@nx/vite/plugin', () => {
|
||||
},
|
||||
},
|
||||
workspaceRoot: '',
|
||||
configFiles: [],
|
||||
};
|
||||
});
|
||||
|
||||
@ -44,7 +45,7 @@ describe('@nx/vite/plugin', () => {
|
||||
|
||||
it('should create nodes', async () => {
|
||||
const nodes = await createNodesFunction(
|
||||
'vitest.config.ts',
|
||||
['vitest.config.ts'],
|
||||
{
|
||||
testTargetName: 'test',
|
||||
},
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { CreateNodesContext } from '@nx/devkit';
|
||||
import { createNodes } from './plugin';
|
||||
import { createNodes, createNodesV2 } from './plugin';
|
||||
|
||||
// This will only create test targets since no build targets are defined in vite.config.ts
|
||||
|
||||
@ -28,7 +28,7 @@ jest.mock('../utils/executor-utils', () => ({
|
||||
}));
|
||||
|
||||
describe('@nx/vite/plugin with test node', () => {
|
||||
let createNodesFunction = createNodes[1];
|
||||
let createNodesFunction = createNodesV2[1];
|
||||
let context: CreateNodesContext;
|
||||
describe('root project', () => {
|
||||
beforeEach(async () => {
|
||||
@ -47,6 +47,7 @@ describe('@nx/vite/plugin with test node', () => {
|
||||
},
|
||||
},
|
||||
workspaceRoot: '',
|
||||
configFiles: [],
|
||||
};
|
||||
});
|
||||
|
||||
@ -56,7 +57,7 @@ describe('@nx/vite/plugin with test node', () => {
|
||||
|
||||
it('should create nodes - with test too', async () => {
|
||||
const nodes = await createNodesFunction(
|
||||
'vite.config.ts',
|
||||
['vite.config.ts'],
|
||||
{
|
||||
buildTargetName: 'build',
|
||||
serveTargetName: 'serve',
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { CreateNodesContext } from '@nx/devkit';
|
||||
import { createNodes } from './plugin';
|
||||
import { createNodes, createNodesV2 } from './plugin';
|
||||
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
|
||||
|
||||
jest.mock('vite', () => ({
|
||||
@ -24,7 +24,7 @@ jest.mock('../utils/executor-utils', () => ({
|
||||
}));
|
||||
|
||||
describe('@nx/vite/plugin', () => {
|
||||
let createNodesFunction = createNodes[1];
|
||||
let createNodesFunction = createNodesV2[1];
|
||||
let context: CreateNodesContext;
|
||||
describe('root project', () => {
|
||||
let tempFs;
|
||||
@ -45,7 +45,9 @@ describe('@nx/vite/plugin', () => {
|
||||
},
|
||||
},
|
||||
workspaceRoot: tempFs.tempDir,
|
||||
configFiles: [],
|
||||
};
|
||||
tempFs.createFileSync('vite.config.ts', '');
|
||||
tempFs.createFileSync('index.html', '');
|
||||
tempFs.createFileSync('package.json', '');
|
||||
});
|
||||
@ -56,7 +58,7 @@ describe('@nx/vite/plugin', () => {
|
||||
|
||||
it('should create nodes', async () => {
|
||||
const nodes = await createNodesFunction(
|
||||
'vite.config.ts',
|
||||
['vite.config.ts'],
|
||||
{
|
||||
buildTargetName: 'build',
|
||||
serveTargetName: 'serve',
|
||||
@ -83,6 +85,7 @@ describe('@nx/vite/plugin', () => {
|
||||
},
|
||||
},
|
||||
workspaceRoot: tempFs.tempDir,
|
||||
configFiles: [],
|
||||
};
|
||||
|
||||
tempFs.createFileSync(
|
||||
@ -99,7 +102,7 @@ describe('@nx/vite/plugin', () => {
|
||||
|
||||
it('should create nodes', async () => {
|
||||
const nodes = await createNodesFunction(
|
||||
'my-app/vite.config.ts',
|
||||
['my-app/vite.config.ts'],
|
||||
{
|
||||
buildTargetName: 'build-something',
|
||||
serveTargetName: 'my-serve',
|
||||
|
||||
@ -2,8 +2,12 @@ import {
|
||||
CreateDependencies,
|
||||
CreateNodes,
|
||||
CreateNodesContext,
|
||||
createNodesFromFiles,
|
||||
CreateNodesV2,
|
||||
detectPackageManager,
|
||||
joinPathFragments,
|
||||
logger,
|
||||
ProjectConfiguration,
|
||||
readJsonFile,
|
||||
TargetConfiguration,
|
||||
writeJsonFile,
|
||||
@ -15,6 +19,7 @@ import { calculateHashForCreateNodes } from '@nx/devkit/src/utils/calculate-hash
|
||||
import { workspaceDataDirectory } from 'nx/src/utils/cache-directory';
|
||||
import { getLockFileName } from '@nx/js';
|
||||
import { loadViteDynamicImport } from '../utils/executor-utils';
|
||||
import { hashObject } from 'nx/src/hasher/file-hasher';
|
||||
|
||||
export interface VitePluginOptions {
|
||||
buildTargetName?: string;
|
||||
@ -23,76 +28,109 @@ export interface VitePluginOptions {
|
||||
previewTargetName?: string;
|
||||
serveStaticTargetName?: string;
|
||||
}
|
||||
type ViteTargets = Pick<ProjectConfiguration, 'targets' | 'metadata'>;
|
||||
|
||||
const cachePath = join(workspaceDataDirectory, 'vite.hash');
|
||||
const targetsCache = readTargetsCache();
|
||||
|
||||
function readTargetsCache(): Record<
|
||||
string,
|
||||
Record<string, TargetConfiguration>
|
||||
> {
|
||||
function readTargetsCache(cachePath: string): Record<string, ViteTargets> {
|
||||
return existsSync(cachePath) ? readJsonFile(cachePath) : {};
|
||||
}
|
||||
|
||||
function writeTargetsToCache() {
|
||||
const oldCache = readTargetsCache();
|
||||
writeJsonFile(cachePath, {
|
||||
...oldCache,
|
||||
...targetsCache,
|
||||
});
|
||||
function writeTargetsToCache(cachePath, results?: Record<string, ViteTargets>) {
|
||||
writeJsonFile(cachePath, results);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated The 'createDependencies' function is now a no-op. This functionality is included in 'createNodesV2'.
|
||||
*/
|
||||
export const createDependencies: CreateDependencies = () => {
|
||||
writeTargetsToCache();
|
||||
return [];
|
||||
};
|
||||
|
||||
export const createNodes: CreateNodes<VitePluginOptions> = [
|
||||
'**/{vite,vitest}.config.{js,ts,mjs,mts,cjs,cts}',
|
||||
async (configFilePath, options, context) => {
|
||||
const projectRoot = dirname(configFilePath);
|
||||
// Do not create a project if package.json and project.json isn't there.
|
||||
const siblingFiles = readdirSync(join(context.workspaceRoot, projectRoot));
|
||||
if (
|
||||
!siblingFiles.includes('package.json') &&
|
||||
!siblingFiles.includes('project.json')
|
||||
) {
|
||||
return {};
|
||||
const viteVitestConfigGlob = '**/{vite,vitest}.config.{js,ts,mjs,mts,cjs,cts}';
|
||||
|
||||
export const createNodesV2: CreateNodesV2<VitePluginOptions> = [
|
||||
viteVitestConfigGlob,
|
||||
async (configFilePaths, options, context) => {
|
||||
const optionsHash = hashObject(options);
|
||||
const cachePath = join(workspaceDataDirectory, `vite-${optionsHash}.hash`);
|
||||
const targetsCache = readTargetsCache(cachePath);
|
||||
try {
|
||||
return await createNodesFromFiles(
|
||||
(configFile, options, context) =>
|
||||
createNodesInternal(configFile, options, context, targetsCache),
|
||||
configFilePaths,
|
||||
options,
|
||||
context
|
||||
);
|
||||
} finally {
|
||||
writeTargetsToCache(cachePath, targetsCache);
|
||||
}
|
||||
|
||||
options = normalizeOptions(options);
|
||||
|
||||
// We do not want to alter how the hash is calculated, so appending the config file path to the hash
|
||||
// to prevent vite/vitest files overwriting the target cache created by the other
|
||||
const hash =
|
||||
(await calculateHashForCreateNodes(projectRoot, options, context, [
|
||||
getLockFileName(detectPackageManager(context.workspaceRoot)),
|
||||
])) + configFilePath;
|
||||
|
||||
targetsCache[hash] ??= await buildViteTargets(
|
||||
configFilePath,
|
||||
projectRoot,
|
||||
options,
|
||||
context
|
||||
);
|
||||
|
||||
return {
|
||||
projects: {
|
||||
[projectRoot]: {
|
||||
root: projectRoot,
|
||||
targets: targetsCache[hash],
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
];
|
||||
|
||||
export const createNodes: CreateNodes<VitePluginOptions> = [
|
||||
viteVitestConfigGlob,
|
||||
async (configFilePath, options, context) => {
|
||||
logger.warn(
|
||||
'`createNodes` is deprecated. Update your plugin to utilize createNodesV2 instead. In Nx 20, this will change to the createNodesV2 API.'
|
||||
);
|
||||
return createNodesInternal(configFilePath, options, context, {});
|
||||
},
|
||||
];
|
||||
|
||||
async function createNodesInternal(
|
||||
configFilePath: string,
|
||||
options: VitePluginOptions,
|
||||
context: CreateNodesContext,
|
||||
targetsCache: Record<string, ViteTargets>
|
||||
) {
|
||||
const projectRoot = dirname(configFilePath);
|
||||
// Do not create a project if package.json and project.json isn't there.
|
||||
const siblingFiles = readdirSync(join(context.workspaceRoot, projectRoot));
|
||||
if (
|
||||
!siblingFiles.includes('package.json') &&
|
||||
!siblingFiles.includes('project.json')
|
||||
) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const normalizedOptions = normalizeOptions(options);
|
||||
|
||||
// We do not want to alter how the hash is calculated, so appending the config file path to the hash
|
||||
// to prevent vite/vitest files overwriting the target cache created by the other
|
||||
const hash =
|
||||
(await calculateHashForCreateNodes(
|
||||
projectRoot,
|
||||
normalizedOptions,
|
||||
context,
|
||||
[getLockFileName(detectPackageManager(context.workspaceRoot))]
|
||||
)) + configFilePath;
|
||||
|
||||
targetsCache[hash] ??= await buildViteTargets(
|
||||
configFilePath,
|
||||
projectRoot,
|
||||
normalizedOptions,
|
||||
context
|
||||
);
|
||||
|
||||
const { targets, metadata } = targetsCache[hash];
|
||||
|
||||
return {
|
||||
projects: {
|
||||
[projectRoot]: {
|
||||
root: projectRoot,
|
||||
targets,
|
||||
metadata,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function buildViteTargets(
|
||||
configFilePath: string,
|
||||
projectRoot: string,
|
||||
options: VitePluginOptions,
|
||||
context: CreateNodesContext
|
||||
) {
|
||||
): Promise<ViteTargets> {
|
||||
const absoluteConfigFilePath = joinPathFragments(
|
||||
context.workspaceRoot,
|
||||
configFilePath
|
||||
@ -156,7 +194,8 @@ async function buildViteTargets(
|
||||
);
|
||||
}
|
||||
|
||||
return targets;
|
||||
const metadata = {};
|
||||
return { targets, metadata };
|
||||
}
|
||||
|
||||
async function buildTarget(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user