Skip to content

Commit

Permalink
feat(nextjs): add support for Next 15
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Jan 8, 2025
1 parent 7098290 commit 2eaf48b
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 18 deletions.
4 changes: 2 additions & 2 deletions e2e/next/src/__snapshots__/next.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`Next.js Applications next-env.d.ts should remain the same after a build
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
"
`;

Expand All @@ -14,6 +14,6 @@ exports[`Next.js Applications next-env.d.ts should remain the same after a build
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.
// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.
"
`;
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"dependencies": {
"@nx/devkit": "file:../devkit",
"@babel/plugin-proposal-decorators": "^7.22.7",
"@svgr/webpack": "^8.0.1",
"@svgr/webpack": "^8.1.0",
"copy-webpack-plugin": "^10.2.4",
"file-loader": "^6.2.0",
"ignore": "^5.0.4",
Expand Down
12 changes: 5 additions & 7 deletions packages/next/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import {
} from '@nx/devkit';
import { initGenerator as jsInitGenerator } from '@nx/js';
import { setupTailwindGenerator } from '@nx/react';
import {
testingLibraryReactVersion,
typesReactDomVersion,
typesReactVersion,
} from '@nx/react/src/utils/versions';
import { testingLibraryReactVersion } from '@nx/react/src/utils/versions';
import { getReactDependenciesVersionsToInstall } from '@nx/react/src/utils/version-utils';

import { normalizeOptions } from './lib/normalize-options';
import { Schema } from './schema';
Expand Down Expand Up @@ -102,9 +99,10 @@ export async function applicationGeneratorInternal(host: Tree, schema: Schema) {
}

if (!options.skipPackageJson) {
const reactVersions = await getReactDependenciesVersionsToInstall(host);
const devDependencies: Record<string, string> = {
'@types/react': typesReactVersion,
'@types/react-dom': typesReactDomVersion,
'@types/react': reactVersions['@types/react'],
'@types/react-dom': reactVersions['@types/react-dom'],
};

if (options.unitTestRunner && options.unitTestRunner !== 'none') {
Expand Down
18 changes: 11 additions & 7 deletions packages/next/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@ import {
createProjectGraphAsync,
} from '@nx/devkit';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { reactDomVersion, reactVersion } from '@nx/react/src/utils/versions';
import { getReactDependenciesVersionsToInstall } from '@nx/react/src/utils/version-utils';
import { addGitIgnoreEntry } from '../../utils/add-gitignore-entry';
import { nextVersion, nxVersion } from '../../utils/versions';
import { nxVersion } from '../../utils/versions';
import { getNextDependenciesVersionsToInstall } from '../../utils/version-utils';
import type { InitSchema } from './schema';

function updateDependencies(host: Tree, schema: InitSchema) {
async function updateDependencies(host: Tree, schema: InitSchema) {
const tasks: GeneratorCallback[] = [];

tasks.push(removeDependenciesFromPackageJson(host, ['@nx/next'], []));

const versions = await getNextDependenciesVersionsToInstall(host);
const reactVersions = await getReactDependenciesVersionsToInstall(host);

tasks.push(
addDependenciesToPackageJson(
host,
{
next: nextVersion,
react: reactVersion,
'react-dom': reactDomVersion,
next: versions.next,
react: reactVersions.react,
'react-dom': reactVersions['react-dom'],
},
{
'@nx/next': nxVersion,
Expand Down Expand Up @@ -76,7 +80,7 @@ export async function nextInitGeneratorInternal(

let installTask: GeneratorCallback = () => {};
if (!schema.skipPackageJson) {
installTask = updateDependencies(host, schema);
installTask = await updateDependencies(host, schema);
}

return installTask;
Expand Down
54 changes: 54 additions & 0 deletions packages/next/src/utils/version-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { type Tree, readJson, createProjectGraphAsync } from '@nx/devkit';
import { clean, coerce, major } from 'semver';
import { nextVersion, next14Version } from './versions';

type NextDependenciesVersions = {
next: string;
};

export async function getNextDependenciesVersionsToInstall(
tree: Tree
): Promise<NextDependenciesVersions> {
if (await isNext14(tree)) {
return {
next: next14Version,
};
} else {
return {
next: nextVersion,
};
}
}

export async function isNext14(tree: Tree) {
let installedNextVersion = await getInstalledNextVersionFromGraph();
if (!installedNextVersion) {
installedNextVersion = getInstalledNextVersion(tree);
}
return major(installedNextVersion) === 14;
}

export function getInstalledNextVersion(tree: Tree): string {
const pkgJson = readJson(tree, 'package.json');
const installedNextVersion =
pkgJson.dependencies && pkgJson.dependencies['next'];

if (
!installedNextVersion ||
installedNextVersion === 'latest' ||
installedNextVersion === 'next'
) {
return clean(nextVersion) ?? coerce(nextVersion).version;
}

return clean(installedNextVersion) ?? coerce(installedNextVersion).version;
}

export async function getInstalledNextVersionFromGraph() {
const graph = await createProjectGraphAsync();
const nextDep = graph.externalNodes?.['npm:next'];
if (!nextDep) {
return undefined;
}
return clean(nextDep.data.version) ?? coerce(nextDep.data.version).version;
}
3 changes: 2 additions & 1 deletion packages/next/src/utils/versions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const nxVersion = require('../../package.json').version;

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

0 comments on commit 2eaf48b

Please sign in to comment.