Skip to content

Addon Test: Only install it in Vite-based projects or Next.js projects #30920

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

Open
wants to merge 2 commits into
base: next
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
5 changes: 4 additions & 1 deletion code/core/src/cli/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ export function detectFrameworkPreset(
*
* @returns CoreBuilder
*/
export async function detectBuilder(packageManager: JsPackageManager, projectType: ProjectType) {
export async function detectBuilder(
packageManager: JsPackageManager,
projectType: ProjectType
): Promise<CoreBuilder> {
const viteConfig = findUpSync(viteConfigFiles);
const webpackConfig = findUpSync(webpackConfigFiles);
const dependencies = await packageManager.getAllDependencies();
Expand Down
27 changes: 1 addition & 26 deletions code/lib/create-storybook/src/generators/baseGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import invariant from 'tiny-invariant';
import { dedent } from 'ts-dedent';

import type { NpmOptions } from '../../../../core/src/cli/NpmOptions';
import { detectBuilder } from '../../../../core/src/cli/detect';
import { configureEslintPlugin, extractEslintInfo } from '../../../../core/src/cli/eslintPlugin';
import { copyTemplateFiles } from '../../../../core/src/cli/helpers';
import {
Expand Down Expand Up @@ -202,38 +201,14 @@ const hasFrameworkTemplates = (framework?: SupportedFrameworks) => {
export async function baseGenerator(
packageManager: JsPackageManager,
npmOptions: NpmOptions,
{ language, builder, pnp, frameworkPreviewParts, projectType, features }: GeneratorOptions,
{ language, builder, pnp, frameworkPreviewParts, features }: GeneratorOptions,
renderer: SupportedRenderers,
options: FrameworkOptions = defaultOptions,
framework?: SupportedFrameworks
) {
const isStorybookInMonorepository = packageManager.isStorybookInMonorepo();
const shouldApplyRequireWrapperOnPackageNames = isStorybookInMonorepository || pnp;

if (!builder) {
builder = await detectBuilder(packageManager as any, projectType);
}

if (features.includes('test')) {
const supportedFrameworks: ProjectType[] = [
ProjectType.REACT,
ProjectType.VUE3,
ProjectType.NEXTJS,
ProjectType.NUXT,
ProjectType.PREACT,
ProjectType.SVELTE,
ProjectType.SVELTEKIT,
ProjectType.WEB_COMPONENTS,
ProjectType.REACT_NATIVE_WEB,
];
const supportsTestAddon =
projectType === ProjectType.NEXTJS ||
(builder !== 'webpack5' && supportedFrameworks.includes(projectType));
if (!supportsTestAddon) {
features.splice(features.indexOf('test'), 1);
}
}

const {
packages: frameworkPackages,
type,
Expand Down
36 changes: 29 additions & 7 deletions code/lib/create-storybook/src/initiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ import * as babel from '../../../core/src/babel';
import type { NpmOptions } from '../../../core/src/cli/NpmOptions';
import {
detect,
detectBuilder,
detectLanguage,
detectPnp,
isStorybookInstantiated,
} from '../../../core/src/cli/detect';
import type { Builder } from '../../../core/src/cli/project_types';
import { ProjectType, installableProjectTypes } from '../../../core/src/cli/project_types';
import {
CoreBuilder,
ProjectType,
installableProjectTypes,
} from '../../../core/src/cli/project_types';
import type { JsPackageManager } from '../../../core/src/common/js-package-manager/JsPackageManager';
import { JsPackageManagerFactory } from '../../../core/src/common/js-package-manager/JsPackageManagerFactory';
import { HandledError } from '../../../core/src/common/utils/HandledError';
Expand Down Expand Up @@ -403,6 +408,8 @@ export async function doInitiate(options: CommandOptions): Promise<

const storybookInstantiated = isStorybookInstantiated();

options.builder = options.builder ?? (await detectBuilder(packageManager as any, projectType));

if (options.force === false && storybookInstantiated && projectType !== ProjectType.ANGULAR) {
logger.log();
const { force } = await prompts([
Expand All @@ -422,7 +429,25 @@ export async function doInitiate(options: CommandOptions): Promise<
}
}

if (selectedFeatures.has('test')) {
const isTestFeatureEnabled = () => {
const supportedFrameworks: ProjectType[] = [
ProjectType.REACT,
ProjectType.VUE3,
ProjectType.NEXTJS,
ProjectType.NUXT,
ProjectType.PREACT,
ProjectType.SVELTE,
ProjectType.SVELTEKIT,
ProjectType.WEB_COMPONENTS,
ProjectType.REACT_NATIVE_WEB,
];
const supportsTestAddon =
projectType === ProjectType.NEXTJS ||
(options.builder !== 'webpack5' && supportedFrameworks.includes(projectType));
return selectedFeatures.has('test') && supportsTestAddon;
};

if (isTestFeatureEnabled()) {
const packageVersionsData = await packageVersions.condition({ packageManager }, {} as any);
if (packageVersionsData.type === 'incompatible') {
const { ignorePackageVersions } = isInteractive
Expand All @@ -445,7 +470,7 @@ export async function doInitiate(options: CommandOptions): Promise<
}
}

if (selectedFeatures.has('test')) {
if (isTestFeatureEnabled()) {
const vitestConfigFilesData = await vitestConfigFiles.condition(
{ babel, findUp, fs } as any,
{ directory: process.cwd() } as any
Expand Down Expand Up @@ -480,9 +505,6 @@ export async function doInitiate(options: CommandOptions): Promise<

const installResult = await installStorybook(projectType as ProjectType, packageManager, options);

// Sync features back because they may have been mutated by the generator (e.g. in case of undetected project type)
selectedFeatures = new Set(options.features);

if (!options.skipInstall) {
await packageManager.installDependencies();
}
Expand Down Expand Up @@ -532,7 +554,7 @@ export async function doInitiate(options: CommandOptions): Promise<
? `ng run ${installResult.projectName}:storybook`
: packageManager.getRunStorybookCommand();

if (selectedFeatures.has('test')) {
if (isTestFeatureEnabled()) {
logger.log(
`> npx storybook@${versions.storybook} add @storybook/addon-test@${versions['@storybook/addon-test']}`
);
Expand Down