Skip to content

Commit

Permalink
feat(vite): add nodes - fixing vite build issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Nov 7, 2023
1 parent 666782d commit b8f5f81
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 145 deletions.
2 changes: 1 addition & 1 deletion docs/generated/packages/vite/generators/vitest.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"coverageProvider": {
"type": "string",
"enum": ["v8", "c8", "istanbul"],
"enum": ["v8", "c8", "custom"],
"default": "v8",
"description": "Coverage provider to use."
},
Expand Down
11 changes: 8 additions & 3 deletions packages/vite/src/executors/test/vitest.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,18 @@ async function getSettings(
const packageJson = existsSync(packageJsonPath)
? readJsonFile(packageJsonPath)
: undefined;
let provider: 'v8' | 'c8' = 'v8';

let provider: 'c8' | 'istanbul' | 'custom';

if (
packageJson?.dependencies?.['@vitest/coverage-c8'] ||
packageJson?.devDependencies?.['@vitest/coverage-c8']
packageJson?.dependencies?.['@vitest/coverage-istanbul'] ||
packageJson?.devDependencies?.['@vitest/coverage-istanbul']
) {
provider = 'istanbul';
} else {
provider = 'c8';
}

const offset = relative(workspaceRoot, context.cwd);
// if reportsDirectory is not provided vitest will remove all files in the project root
// when coverage is enabled in the vite.config.ts
Expand Down
27 changes: 18 additions & 9 deletions packages/vite/src/generators/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
formatFiles,
GeneratorCallback,
joinPathFragments,
readNxJson,
readProjectConfiguration,
runTasksInSerial,
Tree,
Expand Down Expand Up @@ -163,19 +164,27 @@ export async function viteConfigurationGenerator(
});
tasks.push(initTask);

if (!projectAlreadyHasViteTargets.build) {
addOrChangeBuildTarget(tree, schema, buildTargetName);
}
const nxJson = readNxJson(tree);
const hasPlugin = nxJson.plugins?.some((p) =>
typeof p === 'string'
? p === '@nx/vite/plugin'
: p.plugin === '@nx/vite/plugin'
);

if (!schema.includeLib) {
if (!projectAlreadyHasViteTargets.serve) {
addOrChangeServeTarget(tree, schema, serveTargetName);
if (!hasPlugin) {
if (!projectAlreadyHasViteTargets.build) {
addOrChangeBuildTarget(tree, schema, buildTargetName);
}
if (!projectAlreadyHasViteTargets.preview) {
addPreviewTarget(tree, schema, serveTargetName);

if (!schema.includeLib) {
if (!projectAlreadyHasViteTargets.serve) {
addOrChangeServeTarget(tree, schema, serveTargetName);
}
if (!projectAlreadyHasViteTargets.preview) {
addPreviewTarget(tree, schema, serveTargetName);
}
}
}

if (projectType === 'library') {
// update tsconfig.lib.json to include vite/client
updateJson(tree, joinPathFragments(root, 'tsconfig.lib.json'), (json) => {
Expand Down
106 changes: 6 additions & 100 deletions packages/vite/src/generators/init/init.ts
Original file line number Diff line number Diff line change
@@ -1,107 +1,13 @@
import {
addDependenciesToPackageJson,
logger,
readJson,
readNxJson,
runTasksInSerial,
Tree,
updateJson,
updateNxJson,
} from '@nx/devkit';
import { Tree, runTasksInSerial } from '@nx/devkit';

import { initGenerator as jsInitGenerator } from '@nx/js';

import {
edgeRuntimeVmVersion,
happyDomVersion,
jsdomVersion,
nxVersion,
vitePluginDtsVersion,
vitePluginReactSwcVersion,
vitePluginReactVersion,
vitestUiVersion,
vitestVersion,
viteVersion,
} from '../../utils/versions';
import { InitGeneratorSchema } from './schema';

function checkDependenciesInstalled(host: Tree, schema: InitGeneratorSchema) {
const packageJson = readJson(host, 'package.json');
const devDependencies = {};
const dependencies = {};
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};

// base deps
devDependencies['@nx/vite'] = nxVersion;
devDependencies['vite'] = viteVersion;
devDependencies['vitest'] = vitestVersion;
devDependencies['@vitest/ui'] = vitestUiVersion;

if (schema.testEnvironment === 'jsdom') {
devDependencies['jsdom'] = jsdomVersion;
} else if (schema.testEnvironment === 'happy-dom') {
devDependencies['happy-dom'] = happyDomVersion;
} else if (schema.testEnvironment === 'edge-runtime') {
devDependencies['@edge-runtime/vm'] = edgeRuntimeVmVersion;
} else if (schema.testEnvironment !== 'node' && schema.testEnvironment) {
logger.info(
`A custom environment was provided: ${schema.testEnvironment}. You need to install it manually.`
);
}

if (schema.uiFramework === 'react') {
if (schema.compiler === 'swc') {
devDependencies['@vitejs/plugin-react-swc'] = vitePluginReactSwcVersion;
} else {
devDependencies['@vitejs/plugin-react'] = vitePluginReactVersion;
}
}

if (schema.includeLib) {
devDependencies['vite-plugin-dts'] = vitePluginDtsVersion;
}

return addDependenciesToPackageJson(host, dependencies, devDependencies);
}

function moveToDevDependencies(tree: Tree) {
updateJson(tree, 'package.json', (packageJson) => {
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};

if (packageJson.dependencies['@nx/vite']) {
packageJson.devDependencies['@nx/vite'] =
packageJson.dependencies['@nx/vite'];
delete packageJson.dependencies['@nx/vite'];
}
return packageJson;
});
}

export function createVitestConfig(tree: Tree) {
const nxJson = readNxJson(tree);

const productionFileSet = nxJson.namedInputs?.production;
if (productionFileSet) {
productionFileSet.push(
'!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)',
'!{projectRoot}/tsconfig.spec.json'
);

nxJson.namedInputs.production = Array.from(new Set(productionFileSet));
}

nxJson.targetDefaults ??= {};
nxJson.targetDefaults['@nx/vite:test'] ??= {};
nxJson.targetDefaults['@nx/vite:test'].cache ??= true;
nxJson.targetDefaults['@nx/vite:test'].inputs ??= [
'default',
productionFileSet ? '^production' : '^default',
];

updateNxJson(tree, nxJson);
}
import {
checkDependenciesInstalled,
createVitestConfig,
moveToDevDependencies,
} from './lib/utils';

export async function initGenerator(tree: Tree, schema: InitGeneratorSchema) {
moveToDevDependencies(tree);
Expand Down
108 changes: 108 additions & 0 deletions packages/vite/src/generators/init/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import {
addDependenciesToPackageJson,
logger,
readJson,
readNxJson,
Tree,
updateJson,
updateNxJson,
} from '@nx/devkit';

import {
edgeRuntimeVmVersion,
happyDomVersion,
jsdomVersion,
nxVersion,
vitePluginDtsVersion,
vitePluginReactSwcVersion,
vitePluginReactVersion,
vitestUiVersion,
vitestVersion,
viteVersion,
} from '../../../utils/versions';
import { InitGeneratorSchema } from '../schema';

export function checkDependenciesInstalled(
host: Tree,
schema: InitGeneratorSchema
) {
const packageJson = readJson(host, 'package.json');
const devDependencies = {};
const dependencies = {};
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};

// base deps
devDependencies['@nx/vite'] = nxVersion;
devDependencies['vite'] = viteVersion;
devDependencies['vitest'] = vitestVersion;
devDependencies['@vitest/ui'] = vitestUiVersion;

if (process.env['NX_PCV3'] === 'true') {
devDependencies['@nx/vite/plugin'] = nxVersion;
}

if (schema.testEnvironment === 'jsdom') {
devDependencies['jsdom'] = jsdomVersion;
} else if (schema.testEnvironment === 'happy-dom') {
devDependencies['happy-dom'] = happyDomVersion;
} else if (schema.testEnvironment === 'edge-runtime') {
devDependencies['@edge-runtime/vm'] = edgeRuntimeVmVersion;
} else if (schema.testEnvironment !== 'node' && schema.testEnvironment) {
logger.info(
`A custom environment was provided: ${schema.testEnvironment}. You need to install it manually.`
);
}

if (schema.uiFramework === 'react') {
if (schema.compiler === 'swc') {
devDependencies['@vitejs/plugin-react-swc'] = vitePluginReactSwcVersion;
} else {
devDependencies['@vitejs/plugin-react'] = vitePluginReactVersion;
}
}

if (schema.includeLib) {
devDependencies['vite-plugin-dts'] = vitePluginDtsVersion;
}

return addDependenciesToPackageJson(host, dependencies, devDependencies);
}

export function moveToDevDependencies(tree: Tree) {
updateJson(tree, 'package.json', (packageJson) => {
packageJson.dependencies = packageJson.dependencies || {};
packageJson.devDependencies = packageJson.devDependencies || {};

if (packageJson.dependencies['@nx/vite']) {
packageJson.devDependencies['@nx/vite'] =
packageJson.dependencies['@nx/vite'];
delete packageJson.dependencies['@nx/vite'];
}
return packageJson;
});
}

export function createVitestConfig(tree: Tree) {
const nxJson = readNxJson(tree);

const productionFileSet = nxJson.namedInputs?.production;
if (productionFileSet) {
productionFileSet.push(
'!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)',
'!{projectRoot}/tsconfig.spec.json'
);

nxJson.namedInputs.production = Array.from(new Set(productionFileSet));
}

nxJson.targetDefaults ??= {};
nxJson.targetDefaults['@nx/vite:test'] ??= {};
nxJson.targetDefaults['@nx/vite:test'].cache ??= true;
nxJson.targetDefaults['@nx/vite:test'].inputs ??= [
'default',
productionFileSet ? '^production' : '^default',
];

updateNxJson(tree, nxJson);
}
2 changes: 1 addition & 1 deletion packages/vite/src/generators/vitest/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface VitestGeneratorSchema {
project: string;
uiFramework: 'react' | 'none';
coverageProvider: 'v8' | 'c8' | 'istanbul';
coverageProvider: 'c8' | 'istanbul' | 'custom';
inSourceTests?: boolean;
skipViteConfig?: boolean;
testTarget?: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/generators/vitest/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"coverageProvider": {
"type": "string",
"enum": ["v8", "c8", "istanbul"],
"enum": ["v8", "c8", "custom"],
"default": "v8",
"description": "Coverage provider to use."
},
Expand Down
23 changes: 15 additions & 8 deletions packages/vite/src/generators/vitest/vitest-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
GeneratorCallback,
joinPathFragments,
offsetFromRoot,
readNxJson,
readProjectConfiguration,
runTasksInSerial,
Tree,
Expand All @@ -21,7 +22,6 @@ import initGenerator from '../init/init';
import {
vitestCoverageC8Version,
vitestCoverageIstanbulVersion,
vitestCoverageV8Version,
} from '../../utils/versions';

import { addTsLibDependencies } from '@nx/js';
Expand All @@ -37,13 +37,20 @@ export async function vitestGenerator(
tree,
schema.project
);
let testTarget =
schema.testTarget ??
findExistingTargetsInProject(targets).validFoundTargetName.test ??
'test';

addOrChangeTestTarget(tree, schema, testTarget);

const nxJson = readNxJson(tree);
const hasPlugin = nxJson.plugins?.some((p) =>
typeof p === 'string'
? p === '@nx/vite/plugin'
: p.plugin === '@nx/vite/plugin'
);
if (!hasPlugin) {
const testTarget =
schema.testTarget ??
findExistingTargetsInProject(targets).validFoundTargetName.test ??
'test';
addOrChangeTestTarget(tree, schema, testTarget);
}
const initTask = await initGenerator(tree, {
uiFramework: schema.uiFramework,
testEnvironment: schema.testEnvironment,
Expand Down Expand Up @@ -212,7 +219,7 @@ function getCoverageProviderDependency(
};
default:
return {
'@vitest/coverage-v8': vitestCoverageV8Version,
'@vitest/coverage-c8': vitestCoverageC8Version,
};
}
}
Expand Down
Loading

0 comments on commit b8f5f81

Please sign in to comment.