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

Core: Introduce setProjectAnnotations API to more renderers and frameworks #28907

Merged
merged 9 commits into from
Aug 19, 2024
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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ jobs:
parallelism:
type: integer
executor:
class: large
class: xlarge
name: sb_playwright
parallelism: << parameters.parallelism >>
steps:
Expand Down
1 change: 1 addition & 0 deletions code/frameworks/angular/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import './globals';

export * from './public-types';
export * from './portable-stories';

export type { StoryFnAngularReturnType as IStory } from './types';

Expand Down
42 changes: 42 additions & 0 deletions code/frameworks/angular/src/client/portable-stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
setProjectAnnotations as originalSetProjectAnnotations,
setDefaultProjectAnnotations,
} from 'storybook/internal/preview-api';
import {
NamedOrDefaultProjectAnnotations,
NormalizedProjectAnnotations,
} from 'storybook/internal/types';

import * as INTERNAL_DEFAULT_PROJECT_ANNOTATIONS from './render';
import { AngularRenderer } from './types';

/**
* Function that sets the globalConfig of your storybook. The global config is the preview module of
* your .storybook folder.
*
* It should be run a single time, so that your global config (e.g. decorators) is applied to your
* stories when using `composeStories` or `composeStory`.
*
* Example:
*
* ```jsx
* // setup-file.js
* import { setProjectAnnotations } from '@storybook/angular';
*
* import projectAnnotations from './.storybook/preview';
*
* setProjectAnnotations(projectAnnotations);
* ```
*
* @param projectAnnotations - E.g. (import projectAnnotations from '../.storybook/preview')
*/
export function setProjectAnnotations(
projectAnnotations:
| NamedOrDefaultProjectAnnotations<any>
| NamedOrDefaultProjectAnnotations<any>[]
Comment on lines +35 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using a more specific type than any for better type safety

): NormalizedProjectAnnotations<AngularRenderer> {
setDefaultProjectAnnotations(INTERNAL_DEFAULT_PROJECT_ANNOTATIONS);
return originalSetProjectAnnotations(
projectAnnotations
) as NormalizedProjectAnnotations<AngularRenderer>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import * as nextJsAnnotations from './preview';
* Example:
*
* ```jsx
* // setup.js (for jest)
* // setup-file.js
* import { setProjectAnnotations } from '@storybook/experimental-nextjs-vite';
* import projectAnnotations from './.storybook/preview';
*
Expand Down
2 changes: 1 addition & 1 deletion code/frameworks/nextjs/src/portable-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import * as nextJsAnnotations from './preview';
* Example:
*
* ```jsx
* // setup.js (for jest)
* // setup-file.js
* import { setProjectAnnotations } from '@storybook/nextjs';
* import projectAnnotations from './.storybook/preview';
*
Expand Down
1 change: 1 addition & 0 deletions code/frameworks/sveltekit/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './types';
export * from './portable-stories';
51 changes: 51 additions & 0 deletions code/frameworks/sveltekit/src/portable-stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
composeConfigs,
setProjectAnnotations as originalSetProjectAnnotations,
setDefaultProjectAnnotations,
} from 'storybook/internal/preview-api';
import type {
NamedOrDefaultProjectAnnotations,
NormalizedProjectAnnotations,
ProjectAnnotations,
} from 'storybook/internal/types';

import type { SvelteRenderer } from '@storybook/svelte';
import { INTERNAL_DEFAULT_PROJECT_ANNOTATIONS as svelteAnnotations } from '@storybook/svelte';

import * as svelteKitAnnotations from './preview';

/**
* Function that sets the globalConfig of your storybook. The global config is the preview module of
* your .storybook folder.
*
* It should be run a single time, so that your global config (e.g. decorators) is applied to your
* stories when using `composeStories` or `composeStory`.
*
* Example:
*
* ```jsx
* // setup-file.js
* import { setProjectAnnotations } from '@storybook/sveltekit';
* import projectAnnotations from './.storybook/preview';
*
* setProjectAnnotations(projectAnnotations);
* ```
*
* @param projectAnnotations - E.g. (import projectAnnotations from '../.storybook/preview')
*/
export function setProjectAnnotations(
projectAnnotations:
| NamedOrDefaultProjectAnnotations<any>
| NamedOrDefaultProjectAnnotations<any>[]
Comment on lines +38 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using a more specific type than any for better type safety

): NormalizedProjectAnnotations<SvelteRenderer> {
setDefaultProjectAnnotations(INTERNAL_DEFAULT_PROJECT_ANNOTATIONS);
return originalSetProjectAnnotations(
projectAnnotations
) as NormalizedProjectAnnotations<SvelteRenderer>;
}

// This will not be necessary once we have auto preset loading
const INTERNAL_DEFAULT_PROJECT_ANNOTATIONS: ProjectAnnotations<SvelteRenderer> = composeConfigs([
svelteAnnotations,
svelteKitAnnotations,
]);
1 change: 1 addition & 0 deletions code/renderers/html/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import './globals';

export * from './public-types';
export * from './portable-stories';

// optimization: stop HMR propagation in webpack

Expand Down
41 changes: 41 additions & 0 deletions code/renderers/html/src/portable-stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
setProjectAnnotations as originalSetProjectAnnotations,
setDefaultProjectAnnotations,
} from 'storybook/internal/preview-api';
import type {
NamedOrDefaultProjectAnnotations,
NormalizedProjectAnnotations,
} from 'storybook/internal/types';

import * as INTERNAL_DEFAULT_PROJECT_ANNOTATIONS from './entry-preview';
import type { HtmlRenderer } from './types';

/**
* Function that sets the globalConfig of your storybook. The global config is the preview module of
* your .storybook folder.
*
* It should be run a single time, so that your global config (e.g. decorators) is applied to your
* stories when using `composeStories` or `composeStory`.
*
* Example:
*
* ```jsx
* // setup-file.js
* import { setProjectAnnotations } from '@storybook/preact';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: Update import statement to use '@storybook/html' instead of '@storybook/preact'

* import projectAnnotations from './.storybook/preview';
*
* setProjectAnnotations(projectAnnotations);
* ```
*
* @param projectAnnotations - E.g. (import projectAnnotations from '../.storybook/preview')
*/
export function setProjectAnnotations(
projectAnnotations:
| NamedOrDefaultProjectAnnotations<any>
| NamedOrDefaultProjectAnnotations<any>[]
): NormalizedProjectAnnotations<HtmlRenderer> {
setDefaultProjectAnnotations(INTERNAL_DEFAULT_PROJECT_ANNOTATIONS);
return originalSetProjectAnnotations(
projectAnnotations
) as NormalizedProjectAnnotations<HtmlRenderer>;
}
1 change: 1 addition & 0 deletions code/renderers/preact/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import './globals';

export * from './public-types';
export * from './portable-stories';

// optimization: stop HMR propagation in webpack

Expand Down
41 changes: 41 additions & 0 deletions code/renderers/preact/src/portable-stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
setProjectAnnotations as originalSetProjectAnnotations,
setDefaultProjectAnnotations,
} from 'storybook/internal/preview-api';
import type {
NamedOrDefaultProjectAnnotations,
NormalizedProjectAnnotations,
} from 'storybook/internal/types';

import * as INTERNAL_DEFAULT_PROJECT_ANNOTATIONS from './entry-preview';
import type { PreactRenderer } from './types';

/**
* Function that sets the globalConfig of your storybook. The global config is the preview module of
* your .storybook folder.
*
* It should be run a single time, so that your global config (e.g. decorators) is applied to your
* stories when using `composeStories` or `composeStory`.
*
* Example:
*
* ```jsx
* // setup-file.js
* import { setProjectAnnotations } from '@storybook/preact';
* import projectAnnotations from './.storybook/preview';
*
* setProjectAnnotations(projectAnnotations);
* ```
*
* @param projectAnnotations - E.g. (import projectAnnotations from '../.storybook/preview')
*/
export function setProjectAnnotations(
projectAnnotations:
| NamedOrDefaultProjectAnnotations<any>
| NamedOrDefaultProjectAnnotations<any>[]
Comment on lines +34 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using a more specific type than any for better type safety

): NormalizedProjectAnnotations<PreactRenderer> {
setDefaultProjectAnnotations(INTERNAL_DEFAULT_PROJECT_ANNOTATIONS);
return originalSetProjectAnnotations(
projectAnnotations
) as NormalizedProjectAnnotations<PreactRenderer>;
}
2 changes: 1 addition & 1 deletion code/renderers/react/src/portable-stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import type { ReactRenderer } from './types';
* Example:
*
* ```jsx
* // setup.js (for jest)
* // setup-file.js
* import { setProjectAnnotations } from '@storybook/react';
* import projectAnnotations from './.storybook/preview';
*
Expand Down
2 changes: 1 addition & 1 deletion code/renderers/svelte/src/portable-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type MapToComposed<TModule> = {
* Example:
*
* ```jsx
* // setup.js (for jest)
* // setup-file.js
* import { setProjectAnnotations } from '@storybook/svelte';
* import projectAnnotations from './.storybook/preview';
*
Expand Down
2 changes: 1 addition & 1 deletion code/renderers/vue3/src/portable-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type MapToJSXAble<T> = {
* Example:
*
* ```jsx
* // setup.js (for jest)
* // setup-file.js
* import { setProjectAnnotations } from '@storybook/vue3';
* import projectAnnotations from './.storybook/preview';
*
Expand Down
1 change: 1 addition & 0 deletions code/renderers/web-components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { window, EventSource } = global;

export * from './public-types';
export * from './framework-api';
export * from './portable-stories';

// TODO: disable HMR and do full page loads because of customElements.define
if (typeof module !== 'undefined' && module?.hot?.decline) {
Expand Down
41 changes: 41 additions & 0 deletions code/renderers/web-components/src/portable-stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
setProjectAnnotations as originalSetProjectAnnotations,
setDefaultProjectAnnotations,
} from 'storybook/internal/preview-api';
import type {
NamedOrDefaultProjectAnnotations,
NormalizedProjectAnnotations,
} from 'storybook/internal/types';

import * as webComponentsAnnotations from './entry-preview';
import type { WebComponentsRenderer } from './types';

/**
* Function that sets the globalConfig of your storybook. The global config is the preview module of
* your .storybook folder.
*
* It should be run a single time, so that your global config (e.g. decorators) is applied to your
* stories when using `composeStories` or `composeStory`.
*
* Example:
*
* ```jsx
* // setup-file.js
* import { setProjectAnnotations } from '@storybook/web-components';
* import projectAnnotations from './.storybook/preview';
*
* setProjectAnnotations(projectAnnotations);
* ```
*
* @param projectAnnotations - E.g. (import projectAnnotations from '../.storybook/preview')
*/
export function setProjectAnnotations(
projectAnnotations:
| NamedOrDefaultProjectAnnotations<any>
| NamedOrDefaultProjectAnnotations<any>[]
Comment on lines +34 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using a more specific type than any for better type safety

): NormalizedProjectAnnotations<WebComponentsRenderer> {
setDefaultProjectAnnotations(webComponentsAnnotations);
return originalSetProjectAnnotations(
projectAnnotations
) as NormalizedProjectAnnotations<WebComponentsRenderer>;
}
13 changes: 11 additions & 2 deletions scripts/tasks/sandbox-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,15 @@ export async function setupVitest(details: TemplateDetails, options: PassedOptio
const isVue = template.expected.renderer === '@storybook/vue3';
const isNextjs = template.expected.framework.includes('nextjs');
// const isAngular = template.expected.framework === '@storybook/angular';
const storybookPackage = isNextjs ? template.expected.framework : template.expected.renderer;

const portableStoriesFrameworks = [
'@storybook/nextjs',
'@storybook/experimental-nextjs-vite',
// TODO: add sveltekit and angular once we enable their sandboxes
];
const storybookPackage = portableStoriesFrameworks.includes(template.expected.framework)
? template.expected.framework
: template.expected.renderer;
const viteConfigPath = template.name.includes('JavaScript') ? 'vite.config.js' : 'vite.config.ts';

await writeFile(
Expand Down Expand Up @@ -480,7 +488,8 @@ export async function setupVitest(details: TemplateDetails, options: PassedOptio

packageJson.scripts = {
...packageJson.scripts,
vitest: 'vitest --pass-with-no-tests --reporter=default --reporter=hanging-process',
vitest:
'vitest --pass-with-no-tests --reporter=default --reporter=hanging-process --test-timeout=5000',
};

// This workaround is needed because Vitest seems to have issues in link mode
Expand Down