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(nextjs): ensure eslint-config-next matches Next.js 14 and 15 versions #30259

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions packages/next/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,47 @@ describe('app', () => {
`);
});

it('should install eslint-config-next@15 when generating a new Next.js application in an empty Nx workspace', async () => {
const name = uniq();
await applicationGenerator(tree, {
directory: name,
style: 'css',
});

const packageJson = readJson(tree, '/package.json');
expect(packageJson).toMatchObject({
devDependencies: {
'eslint-config-next': '~15.1.4',
},
});
});

it('should install eslint-config-next@14 when an existing Next.js 14 project is detected', async () => {
tree.write(
'/package.json',
JSON.stringify({
name: '@proj/source',
dependencies: {
next: '~14.2.16',
},
devDependencies: {},
})
);

const name = uniq();
await applicationGenerator(tree, {
directory: name,
style: 'css',
});

const packageJson = readJson(tree, '/package.json');
expect(packageJson).toMatchObject({
devDependencies: {
'eslint-config-next': '~14.2.16',
},
});
});

it('should add .eslintrc.json and dependencies', async () => {
await applicationGenerator(tree, {
directory: 'myapp',
Expand Down
7 changes: 5 additions & 2 deletions packages/next/src/generators/application/lib/add-linting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
isEslintConfigSupported,
updateOverrideInLintConfig,
} from '@nx/eslint/src/generators/utils/eslint-file';
import { eslintConfigNextVersion } from '../../../utils/versions';
import { getEslintConfigNextDependenciesVersionsToInstall } from '../../../utils/version-utils';
import { useFlatConfig } from '@nx/eslint/src/utils/flat-config';

export async function addLinting(
Expand Down Expand Up @@ -95,10 +95,13 @@ export async function addLinting(
}

if (!options.skipPackageJson) {
const eslintConfigNextVersions =
await getEslintConfigNextDependenciesVersionsToInstall(host);

tasks.push(
addDependenciesToPackageJson(host, extraEslintDependencies.dependencies, {
...extraEslintDependencies.devDependencies,
'eslint-config-next': eslintConfigNextVersion,
'eslint-config-next': eslintConfigNextVersions.eslintConfigNextVersion,
})
);
}
Expand Down
25 changes: 24 additions & 1 deletion packages/next/src/utils/version-utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { type Tree, readJson, createProjectGraphAsync } from '@nx/devkit';
import { clean, coerce, major } from 'semver';
import { nextVersion, next14Version } from './versions';
import {
nextVersion,
next14Version,
eslintConfigNext14Version,
eslintConfigNextVersion,
} from './versions';

type NextDependenciesVersions = {
next: string;
};

type EslintConfigNextVersions = {
eslintConfigNextVersion: string;
};

export async function getNextDependenciesVersionsToInstall(
tree: Tree,
isReact18 = false
Expand All @@ -21,6 +30,20 @@ export async function getNextDependenciesVersionsToInstall(
}
}

export async function getEslintConfigNextDependenciesVersionsToInstall(
tree: Tree
): Promise<EslintConfigNextVersions> {
if (await isNext14(tree)) {
return {
eslintConfigNextVersion: eslintConfigNext14Version,
};
} else {
return {
eslintConfigNextVersion: eslintConfigNextVersion,
};
}
}

export async function isNext14(tree: Tree) {
let installedNextVersion = await getInstalledNextVersionFromGraph();
if (!installedNextVersion) {
Expand Down
3 changes: 2 additions & 1 deletion packages/next/src/utils/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export const nxVersion = require('../../package.json').version;

export const nextVersion = '~15.1.4';
export const next14Version = '~14.2.16';
export const eslintConfigNextVersion = '14.2.16';
export const eslintConfigNextVersion = '~15.1.4';
export const eslintConfigNext14Version = '~14.2.16';
export const sassVersion = '1.62.1';
export const lessLoader = '11.1.0';
export const emotionServerVersion = '11.11.0';
Expand Down