Skip to content
Merged
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
6 changes: 2 additions & 4 deletions code/frameworks/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { fileURLToPath } from 'node:url';

import type { NextConfig } from 'next';
import semver from 'semver';
import type { Configuration as WebpackConfig } from 'webpack';

import { addScopedAlias, getNextjsVersion, resolveNextConfig } from '../utils';
import { addScopedAlias, isNextVersionGte, resolveNextConfig } from '../utils';

const nextjsVersion = getNextjsVersion();
const isNext16orNewer = semver.gte(nextjsVersion, '16.0.0');
const isNext16orNewer = isNextVersionGte('16.0.0');

const tryResolve = (path: string) => {
try {
Expand Down
5 changes: 2 additions & 3 deletions code/frameworks/nextjs/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import nextBabelPreset from './babel/preset';
import { configureConfig } from './config/webpack';
import TransformFontImports from './font/babel';
import type { FrameworkOptions, StorybookConfig } from './types';
import { getNextjsVersion } from './utils';
import { isNextVersionGte } from './utils';

export const addons: PresetProperty<'addons'> = [
fileURLToPath(import.meta.resolve('@storybook/preset-react-webpack')),
Expand Down Expand Up @@ -51,8 +51,7 @@ export const core: PresetProperty<'core'> = async (config, options) => {
export const previewAnnotations: PresetProperty<'previewAnnotations'> = (entry = []) => {
const annotations = [...entry, fileURLToPath(import.meta.resolve('@storybook/nextjs/preview'))];

const nextjsVersion = getNextjsVersion();
const isNext16orNewer = semver.gte(nextjsVersion, '16.0.0');
const isNext16orNewer = isNextVersionGte('16.0.0');

// TODO: Remove this once we only support Next.js v16 and above
if (!isNext16orNewer) {
Expand Down
7 changes: 7 additions & 0 deletions code/frameworks/nextjs/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { WebpackDefinePlugin } from '@storybook/builder-webpack5';
import type { NextConfig } from 'next';
import { PHASE_DEVELOPMENT_SERVER } from 'next/constants.js';
import nextJsLoadConfigModule from 'next/dist/server/config.js';
import semver from 'semver';
import type { Configuration as WebpackConfig } from 'webpack';

import { resolvePackageDir } from '../../../core/src/shared/utils/module';
Expand All @@ -24,6 +25,12 @@ export const configureRuntimeNextjsVersionResolution = (baseConfig: WebpackConfi
export const getNextjsVersion = (): string =>
JSON.parse(readFileSync(join(resolvePackageDir('next'), 'package.json'), 'utf8')).version;

export const isNextVersionGte = (version: string): boolean => {
const currentVersion = getNextjsVersion();
const coercedVersion = semver.coerce(currentVersion);
return coercedVersion ? semver.gte(coercedVersion, version) : false;
};

export const resolveNextConfig = async ({
nextConfigPath,
}: {
Expand Down
Loading