Skip to content

Commit

Permalink
Merge pull request #25263 from storybookjs/shilman/nextjs-mock-server…
Browse files Browse the repository at this point in the history
…-only

NextJS: Mock out `server-only` package for RSC
  • Loading branch information
shilman authored Dec 19, 2023
2 parents 05e7aaa + ec52e79 commit 4169cd5
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 8 deletions.
5 changes: 3 additions & 2 deletions code/frameworks/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"import": "./dist/font/webpack/loader/storybook-nextjs-font-loader.mjs"
},
"./dist/preview.mjs": "./dist/preview.mjs",
"./dist/previewRSC.mjs": "./dist/previewRSC.mjs",
"./dist/rsc/preview.mjs": "./dist/rsc/preview.mjs",
"./next-image-loader-stub.js": {
"types": "./dist/next-image-loader-stub.d.ts",
"require": "./dist/next-image-loader-stub.js",
Expand Down Expand Up @@ -153,12 +153,13 @@
"./src/index.ts",
"./src/preset.ts",
"./src/preview.tsx",
"./src/previewRSC.tsx",
"./src/next-image-loader-stub.ts",
"./src/images/decorator.tsx",
"./src/images/next-legacy-image.tsx",
"./src/images/next-image.tsx",
"./src/font/webpack/loader/storybook-nextjs-font-loader.ts",
"./src/rsc/preview.tsx",
"./src/rsc/server-only.ts",
"./src/swc/next-swc-loader-patch.ts"
],
"externals": [
Expand Down
7 changes: 6 additions & 1 deletion code/frameworks/nextjs/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { configureCss } from './css/webpack';
import { configureImports } from './imports/webpack';
import { configureStyledJsx } from './styledJsx/webpack';
import { configureImages } from './images/webpack';
import { configureRSC } from './rsc/webpack';
import { configureRuntimeNextjsVersionResolution } from './utils';
import type { FrameworkOptions, StorybookConfig } from './types';
import TransformFontImports from './font/babel';
Expand Down Expand Up @@ -72,7 +73,7 @@ export const previewAnnotations: PresetProperty<'previewAnnotations'> = (
const nextDir = dirname(require.resolve('@storybook/nextjs/package.json'));
const result = [...entry, join(nextDir, 'dist/preview.mjs')];
if (features?.experimentalNextRSC) {
result.unshift(join(nextDir, 'dist/previewRSC.mjs'));
result.unshift(join(nextDir, 'dist/rsc/preview.mjs'));
}
return result;
};
Expand Down Expand Up @@ -155,6 +156,10 @@ export const webpackFinal: StorybookConfig['webpackFinal'] = async (baseConfig,
configureStyledJsx(baseConfig);
configureNodePolyfills(baseConfig);

if (options.features?.experimentalNextRSC) {
configureRSC(baseConfig);
}

// TODO: In Storybook 8.0, we have to check whether the babel-compiler addon is used. Otherwise, swc should be used.
if (builder?.useSWC) {
await configureSWCLoader(baseConfig, options, nextConfig);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Addon_DecoratorFunction } from '@storybook/types';
import { ServerComponentDecorator } from './rsc/decorator';
import { ServerComponentDecorator } from './decorator';

export const decorators: Addon_DecoratorFunction<any>[] = [ServerComponentDecorator];

Expand Down
2 changes: 2 additions & 0 deletions code/frameworks/nextjs/src/rsc/server-only.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// dummy export to make this a module
export default {};
9 changes: 9 additions & 0 deletions code/frameworks/nextjs/src/rsc/webpack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Configuration as WebpackConfig } from 'webpack';

export const configureRSC = (baseConfig: WebpackConfig): void => {
const resolve = baseConfig.resolve ?? {};
resolve.alias = {
...resolve.alias,
'server-only$': require.resolve('./rsc/server-only.js'),
};
};
1 change: 1 addition & 0 deletions code/frameworks/nextjs/template/stories/RSC.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import 'server-only';

export const RSC = async ({ label }) => <>RSC {label}</>;

Expand Down
5 changes: 5 additions & 0 deletions code/lib/cli/src/sandbox-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export type Template = {
mainConfig?: Partial<StorybookConfigRaw>;
testBuild?: boolean;
disableDocs?: boolean;
extraDependencies?: string[];
};
/**
* Flag to indicate that this template is a secondary template, which is used mainly to test rather specific features.
Expand Down Expand Up @@ -124,6 +125,7 @@ const baseTemplates = {
mainConfig: {
features: { experimentalNextRSC: true },
},
extraDependencies: ['server-only'],
},
skipTasks: ['e2e-tests-dev', 'bench'],
inDevelopment: true,
Expand All @@ -141,6 +143,7 @@ const baseTemplates = {
mainConfig: {
features: { experimentalNextRSC: true },
},
extraDependencies: ['server-only'],
},
skipTasks: ['e2e-tests-dev', 'bench'],
},
Expand All @@ -157,6 +160,7 @@ const baseTemplates = {
mainConfig: {
features: { experimentalNextRSC: true },
},
extraDependencies: ['server-only'],
},
skipTasks: ['e2e-tests-dev', 'bench'],
},
Expand All @@ -173,6 +177,7 @@ const baseTemplates = {
mainConfig: {
features: { experimentalNextRSC: true },
},
extraDependencies: ['server-only'],
},
skipTasks: ['e2e-tests-dev', 'bench'],
},
Expand Down
16 changes: 12 additions & 4 deletions scripts/tasks/sandbox-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { workspacePath } from '../utils/workspace';
import { babelParse } from '../../code/lib/csf-tools/src/babelParse';
import { CODE_DIRECTORY, REPROS_DIRECTORY } from '../utils/constants';
import type { TemplateKey } from '../../code/lib/cli/src/sandbox-templates';
import type { JsPackageManager } from '../../code/lib/cli/src/js-package-manager';

const logger = console;

Expand Down Expand Up @@ -380,21 +381,28 @@ export async function addExtraDependencies({
cwd,
dryRun,
debug,
extraDeps,
}: {
cwd: string;
dryRun: boolean;
debug: boolean;
extraDeps?: string[];
}) {
// web-components doesn't install '@storybook/testing-library' by default
const extraDeps = [
const extraDevDeps = [
'@storybook/jest@next',
'@storybook/testing-library@next',
'@storybook/test-runner@next',
];
if (debug) logger.log('🎁 Adding extra deps', extraDeps);
if (debug) logger.log('🎁 Adding extra dev deps', extraDevDeps);
let packageManager: JsPackageManager;
if (!dryRun) {
const packageManager = JsPackageManagerFactory.getPackageManager({}, cwd);
await packageManager.addDependencies({ installAsDevDependencies: true }, extraDeps);
packageManager = JsPackageManagerFactory.getPackageManager({}, cwd);
await packageManager.addDependencies({ installAsDevDependencies: true }, extraDevDeps);
}
if (extraDeps) {
if (debug) logger.log('🎁 Adding extra deps', extraDeps);
await packageManager.addDependencies({ installAsDevDependencies: false }, extraDeps);
}
}

Expand Down
1 change: 1 addition & 0 deletions scripts/tasks/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const sandbox: Task = {
cwd: details.sandboxDir,
debug: options.debug,
dryRun: options.dryRun,
extraDeps: details.template.modifications?.extraDependencies,
});

await extendMain(details, options);
Expand Down

0 comments on commit 4169cd5

Please sign in to comment.