Skip to content

Commit

Permalink
feat(nuxt): fies
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Nov 30, 2023
1 parent 69859fb commit 2c07457
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,6 @@ exports[`app generated files content - as-provided general application should co
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"]
},
"test": {
"executor": "@nx/vite:test",
"outputs": ["{options.reportsDirectory}"],
"options": {
"reportsDirectory": "../coverage/my-app"
}
}
}
}
Expand Down Expand Up @@ -125,7 +118,7 @@ export default defineConfig({
cache: {
dir: '../node_modules/.vitest',
},
environment: 'nuxt',
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
coverage: {
Expand Down
30 changes: 13 additions & 17 deletions packages/nuxt/src/generators/application/lib/add-vitest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,31 @@ export async function addVitest(tree: Tree, options: NormalizedSchema) {
? p === '@nx/nuxt/plugin'
: p.plugin === '@nx/nuxt/plugin'
);
const projectConfig = readProjectConfiguration(tree, options.name);
if (!hasPlugin) {
projectConfig.targets['test'] = {
executor: '@nx/vite:test',
outputs: ['{options.reportsDirectory}'],
};
updateProjectConfiguration(tree, options.name, projectConfig);
}

const { createOrEditViteConfig, vitestGenerator } = ensurePackage<
typeof import('@nx/vite')
>('@nx/vite', nxVersion);

const vitestTask = await vitestGenerator(tree, {
project: options.name,
uiFramework: 'none',
coverageProvider: 'v8',
skipFormat: true,
testEnvironment: 'nuxt',
skipViteConfig: true,
});
const vitestTask = await vitestGenerator(
tree,
{
project: options.name,
uiFramework: 'none',
coverageProvider: 'v8',
skipFormat: true,
testEnvironment: 'jsdom',
skipViteConfig: true,
},
hasPlugin
);

createOrEditViteConfig(
tree,
{
project: options.name,
includeLib: false,
includeVitest: true,
testEnvironment: 'nuxt',
testEnvironment: 'jsdom',
imports: [`import vue from '@vitejs/plugin-vue'`],
plugins: ['vue()'],
},
Expand Down
11 changes: 0 additions & 11 deletions packages/nuxt/src/generators/init/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,6 @@ export function addVitestTargetDefaults(tree: Tree) {
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',
];
nxJson.targetDefaults['@nx/vite:test'].options ??= {
passWithNoTests: true,
};

updateNxJson(tree, nxJson);
}

Expand Down
54 changes: 42 additions & 12 deletions packages/nuxt/src/plugins/__snapshots__/plugin.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,40 @@ exports[`@nx/nuxt/plugin not root project should create nodes 1`] = `
"targets": {
"build-something": {
"cache": true,
"command": "pnpm exec nuxi build my-app",
"command": "nuxi build",
"inputs": [
"production",
"default",
"^production",
{
"externalDependencies": [
"nuxi",
],
},
],
"options": {
"cwd": "my-app",
},
"outputs": [
"{options.outputPath}",
],
},
"my-serve": {
"defaultConfiguration": "development",
"executor": "@nx/nuxt:serve",
"command": "nuxi dev",
"options": {
"cwd": "my-app",
},
},
"test": {
"cache": true,
"command": "pnpm exec vitest run",
"command": "vitest run",
"inputs": [
"production",
"default",
"^production",
{
"externalDependencies": [
"vitest",
],
},
],
"options": {
"cwd": "my-app",
Expand All @@ -50,25 +65,40 @@ exports[`@nx/nuxt/plugin root project should create nodes 1`] = `
"targets": {
"build": {
"cache": true,
"command": "pnpm exec nuxi build .",
"command": "nuxi build",
"inputs": [
"production",
"default",
"^production",
{
"externalDependencies": [
"nuxi",
],
},
],
"options": {
"cwd": ".",
},
"outputs": [
"{options.outputPath}",
],
},
"serve": {
"defaultConfiguration": "development",
"executor": "@nx/nuxt:serve",
"command": "nuxi dev",
"options": {
"cwd": ".",
},
},
"test": {
"cache": true,
"command": "pnpm exec vitest run",
"command": "vitest run",
"inputs": [
"production",
"default",
"^production",
{
"externalDependencies": [
"vitest",
],
},
],
"options": {
"cwd": ".",
Expand Down
74 changes: 41 additions & 33 deletions packages/nuxt/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
CreateNodesContext,
TargetConfiguration,
detectPackageManager,
getPackageManagerCommand,
joinPathFragments,
offsetFromRoot,
readJsonFile,
Expand All @@ -17,6 +16,7 @@ import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs';
import { existsSync, readdirSync } from 'fs';
import { loadNuxtKitDynamicImport } from '../utils/executor-utils';
import { calculateHashForCreateNodes } from '@nx/devkit/src/utils/calculate-hash-for-create-nodes';
import { getLockFileName } from '@nx/js';

const cachePath = join(projectGraphCacheDirectory, 'nuxt.hash');
const targetsCache = existsSync(cachePath) ? readTargetsCache() : {};
Expand Down Expand Up @@ -65,7 +65,9 @@ export const createNodes: CreateNodes<NuxtPluginOptions> = [

options = normalizeOptions(options);

const hash = calculateHashForCreateNodes(projectRoot, options, context);
const hash = calculateHashForCreateNodes(projectRoot, options, context, [
getLockFileName(detectPackageManager(context.workspaceRoot)),
]);
const targets = targetsCache[hash]
? targetsCache[hash]
: await buildNuxtTargets(configFilePath, projectRoot, options, context);
Expand Down Expand Up @@ -103,10 +105,11 @@ async function buildNuxtTargets(
context,
namedInputs,
buildOutputs,
projectRoot
projectRoot,
options
);

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

targets[options.testTargetName] = testTarget(
context,
Expand All @@ -125,22 +128,19 @@ function buildTarget(
[inputName: string]: any[];
},
buildOutputs: string[],
projectRoot: string
projectRoot: string,
options: NuxtPluginOptions
) {
const targetDefaults = readTargetDefaultsForTarget(
'build',
context.nxJsonConfiguration.targetDefaults,
'@nx/nuxt:build'
options.buildTargetName,
context.nxJsonConfiguration.targetDefaults
);

const targetConfig: TargetConfiguration = {
command: `${
getPackageManagerCommand(detectPackageManager()).exec
} nuxi build ${projectRoot}`,
// this seems to not have any effect
// options: {
// cwd: joinPathFragments(projectRoot),
// },
command: `nuxi build`,
options: {
cwd: projectRoot,
},
};

if (targetDefaults?.outputs === undefined) {
Expand All @@ -152,19 +152,25 @@ function buildTarget(
}

if (targetDefaults?.inputs === undefined) {
targetConfig.inputs =
'production' in namedInputs
? ['production', '^production']
: ['default', '^default'];
}
targetConfig.inputs = [
...('production' in namedInputs
? ['default', '^production']
: ['default', '^default']),

{
externalDependencies: ['nuxi'],
},
];
}
return targetConfig;
}

function serveTarget() {
function serveTarget(projectRoot: string) {
const targetConfig: TargetConfiguration = {
executor: '@nx/nuxt:serve',
defaultConfiguration: 'development',
command: `nuxi dev`,
options: {
cwd: projectRoot,
},
};

return targetConfig;
Expand All @@ -181,16 +187,13 @@ function testTarget(
) {
const targetDefaults = readTargetDefaultsForTarget(
options.testTargetName,
context.nxJsonConfiguration.targetDefaults,
'@nx/vite:test'
context.nxJsonConfiguration.targetDefaults
);

const targetConfig: TargetConfiguration = {
command: `${
getPackageManagerCommand(detectPackageManager()).exec
} vitest run`,
command: `vitest run`,
options: {
cwd: joinPathFragments(projectRoot),
cwd: projectRoot,
},
};

Expand All @@ -203,10 +206,15 @@ function testTarget(
}

if (targetDefaults?.inputs === undefined) {
targetConfig.inputs =
'production' in namedInputs
? ['production', '^production']
: ['default', '^default'];
targetConfig.inputs = [
...('production' in namedInputs
? ['default', '^production']
: ['default', '^default']),

{
externalDependencies: ['vitest'],
},
];
}

return targetConfig;
Expand Down
7 changes: 5 additions & 2 deletions packages/vite/src/generators/vitest/vitest-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import { join } from 'path';

export async function vitestGenerator(
tree: Tree,
schema: VitestGeneratorSchema
schema: VitestGeneratorSchema,
hasPlugin = false
) {
const tasks: GeneratorCallback[] = [];

Expand All @@ -41,7 +42,9 @@ export async function vitestGenerator(
findExistingTargetsInProject(targets).validFoundTargetName.test ??
'test';

addOrChangeTestTarget(tree, schema, testTarget);
if (!hasPlugin) {
addOrChangeTestTarget(tree, schema, testTarget);
}

const initTask = await initGenerator(tree, {
uiFramework: schema.uiFramework,
Expand Down

0 comments on commit 2c07457

Please sign in to comment.