Skip to content

Commit

Permalink
Merge pull request #24623 from storybookjs/norbert/split-docs-entries
Browse files Browse the repository at this point in the history
Maintenance: Split renderers preview entrypoints
  • Loading branch information
ndelangen authored Nov 1, 2023
2 parents f176535 + 2f76b60 commit aadef51
Show file tree
Hide file tree
Showing 64 changed files with 311 additions and 246 deletions.
11 changes: 7 additions & 4 deletions code/lib/core-server/src/presets/common-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,13 @@ export const frameworkOptions = async (
export const docs = (
docsOptions: StorybookConfig['docs'],
{ docs: docsMode }: CLIOptions
): StorybookConfig['docs'] => ({
...docsOptions,
docsMode,
});
): StorybookConfig['docs'] =>
docsOptions && docsMode !== undefined
? {
...docsOptions,
docsMode,
}
: docsOptions;

export const managerHead = async (_: any, options: Options) => {
const location = join(options.configDir, 'manager-head.html');
Expand Down
2 changes: 1 addition & 1 deletion code/lib/preview-api/template/stories/args.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { global as globalThis } from '@storybook/global';
import type { PartialStoryFn, PlayFunctionContext, StoryContext } from '@storybook/types';
import { within } from '@storybook/testing-library';
import { expect } from '@storybook/jest';
import { pick } from 'lodash';
import pick from 'lodash/pick';
import { STORY_ARGS_UPDATED, UPDATE_STORY_ARGS, RESET_STORY_ARGS } from '@storybook/core-events';

export default {
Expand Down
12 changes: 6 additions & 6 deletions code/renderers/html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
"require": "./dist/index.js",
"import": "./dist/index.mjs"
},
"./preview": {
"types": "./dist/config.d.ts",
"require": "./dist/config.js",
"import": "./dist/config.mjs"
},
"./preset": "./preset.js",
"./dist/entry-preview.mjs": "./dist/entry-preview.mjs",
"./dist/entry-preview-docs.mjs": "./dist/entry-preview-docs.mjs",
"./package.json": "./package.json"
},
"main": "dist/index.js",
Expand Down Expand Up @@ -71,7 +69,9 @@
"bundler": {
"entries": [
"./src/index.ts",
"./src/config.ts"
"./src/preset.ts",
"./src/entry-preview.ts",
"./src/entry-preview-docs.ts"
],
"platform": "browser"
},
Expand Down
1 change: 1 addition & 0 deletions code/renderers/html/preset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist/preset');
1 change: 0 additions & 1 deletion code/renderers/html/preview.js

This file was deleted.

7 changes: 0 additions & 7 deletions code/renderers/html/src/config.ts

This file was deleted.

23 changes: 0 additions & 23 deletions code/renderers/html/src/docs/config.ts

This file was deleted.

10 changes: 5 additions & 5 deletions code/renderers/html/src/docs/sourceDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import { SNIPPET_RENDERED, SourceType } from '@storybook/docs-tools';
import { addons, useEffect } from '@storybook/preview-api';
import type { PartialStoryFn } from '@storybook/types';
import type { DecoratorFunction } from '@storybook/types';

import type { HtmlRenderer, StoryContext } from '../types';
import type { HtmlRenderer } from '../types';

import type { StoryFn } from '../public-types';

function skipSourceRender(context: StoryContext) {
function skipSourceRender(context: Parameters<DecoratorFunction<HtmlRenderer>>[1]) {
const sourceParams = context?.parameters.docs?.source;
const isArgsStory = context?.parameters.__isArgsStory;

Expand All @@ -22,7 +22,7 @@ function skipSourceRender(context: StoryContext) {
return !isArgsStory || sourceParams?.code || sourceParams?.type === SourceType.CODE;
}

export function sourceDecorator(storyFn: PartialStoryFn<HtmlRenderer>, context: StoryContext) {
export const sourceDecorator: DecoratorFunction<HtmlRenderer> = (storyFn, context) => {
const story = storyFn();
const renderedForSource = context?.parameters.docs?.source?.excludeDecorators
? (context.originalStoryFn as StoryFn)(context.args, context)
Expand All @@ -42,4 +42,4 @@ export function sourceDecorator(storyFn: PartialStoryFn<HtmlRenderer>, context:
});

return story;
}
};
21 changes: 21 additions & 0 deletions code/renderers/html/src/entry-preview-docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { ArgTypesEnhancer, DecoratorFunction } from '@storybook/types';
import { SourceType, enhanceArgTypes } from '@storybook/docs-tools';

import { sourceDecorator } from './docs/sourceDecorator';
import type { HtmlRenderer } from './types';

export const decorators: DecoratorFunction<HtmlRenderer>[] = [sourceDecorator];

export const parameters = {
docs: {
story: { inline: true },
source: {
type: SourceType.DYNAMIC,
language: 'html',
code: undefined,
excludeDecorators: undefined,
},
},
};

export const argTypesEnhancers: ArgTypesEnhancer[] = [enhanceArgTypes];
5 changes: 5 additions & 0 deletions code/renderers/html/src/entry-preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Parameters } from './types';

export const parameters: Parameters = { renderer: 'html' };

export { renderToCanvas, render } from './render';
12 changes: 12 additions & 0 deletions code/renderers/html/src/preset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { StorybookConfig } from '@storybook/types';
import { join } from 'path';

export const previewAnnotations: StorybookConfig['previewAnnotations'] = async (input, options) => {
const docsEnabled = Object.keys(await options.presets.apply('docs', {}, options)).length > 0;
const result: string[] = [];

return result
.concat(input)
.concat([join(__dirname, 'entry-preview.mjs')])
.concat(docsEnabled ? [join(__dirname, 'entry-preview-docs.mjs')] : []);
};
12 changes: 6 additions & 6 deletions code/renderers/preact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
"require": "./dist/index.js",
"import": "./dist/index.mjs"
},
"./preview": {
"types": "./dist/config.d.ts",
"require": "./dist/config.js",
"import": "./dist/config.mjs"
},
"./preset": "./preset.js",
"./dist/entry-preview.mjs": "./dist/entry-preview.mjs",
"./dist/entry-preview-docs.mjs": "./dist/entry-preview-docs.mjs",
"./package.json": "./package.json"
},
"main": "dist/index.js",
Expand Down Expand Up @@ -71,7 +69,9 @@
"bundler": {
"entries": [
"./src/index.ts",
"./src/config.ts"
"./src/preset.ts",
"./src/entry-preview.ts",
"./src/entry-preview-docs.ts"
],
"platform": "browser"
},
Expand Down
1 change: 1 addition & 0 deletions code/renderers/preact/preset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist/preset');
1 change: 0 additions & 1 deletion code/renderers/preact/preview.js

This file was deleted.

5 changes: 0 additions & 5 deletions code/renderers/preact/src/config.ts

This file was deleted.

File renamed without changes.
3 changes: 3 additions & 0 deletions code/renderers/preact/src/entry-preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { renderToCanvas, render } from './render';

export const parameters = { renderer: 'preact' };
12 changes: 12 additions & 0 deletions code/renderers/preact/src/preset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { StorybookConfig } from '@storybook/types';
import { join } from 'path';

export const previewAnnotations: StorybookConfig['previewAnnotations'] = async (input, options) => {
const docsEnabled = Object.keys(await options.presets.apply('docs', {}, options)).length > 0;
const result: string[] = [];

return result
.concat(input)
.concat([join(__dirname, 'entry-preview.mjs')])
.concat(docsEnabled ? [join(__dirname, 'entry-preview-docs.mjs')] : []);
};
17 changes: 6 additions & 11 deletions code/renderers/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,9 @@
"require": "./dist/index.js",
"import": "./dist/index.mjs"
},
"./preview": {
"types": "./dist/config.d.ts",
"require": "./dist/config.js",
"import": "./dist/config.mjs"
},
"./dist/preset": {
"types": "./dist/preset.d.ts",
"require": "./dist/preset.js"
},
"./preset": "./preset.js",
"./dist/entry-preview.mjs": "./dist/entry-preview.mjs",
"./dist/entry-preview-docs.mjs": "./dist/entry-preview-docs.mjs",
"./package.json": "./package.json"
},
"main": "dist/index.js",
Expand Down Expand Up @@ -102,8 +96,9 @@
"bundler": {
"entries": [
"./src/index.ts",
"./src/config.ts",
"./src/preset.ts"
"./src/preset.ts",
"./src/entry-preview.ts",
"./src/entry-preview-docs.ts"
],
"platform": "browser"
},
Expand Down
1 change: 0 additions & 1 deletion code/renderers/react/preview.js

This file was deleted.

9 changes: 0 additions & 9 deletions code/renderers/react/src/config.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defaultDecorateStory } from '@storybook/preview-api';
import type { LegacyStoryFn, DecoratorFunction } from '@storybook/types';

import type { ReactRenderer } from './types';
import { jsxDecorator } from './docs/jsxDecorator';
import type { ReactRenderer } from '../types';
import { jsxDecorator } from './jsxDecorator';

export const applyDecorators = (
storyFn: LegacyStoryFn<ReactRenderer>,
Expand Down
18 changes: 0 additions & 18 deletions code/renderers/react/src/docs/config.ts

This file was deleted.

20 changes: 20 additions & 0 deletions code/renderers/react/src/entry-preview-docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { ArgTypesEnhancer, DecoratorFunction } from '@storybook/types';
import { extractComponentDescription, enhanceArgTypes } from '@storybook/docs-tools';

import { extractArgTypes } from './docs/extractArgTypes';
import { jsxDecorator } from './docs/jsxDecorator';
import type { ReactRenderer } from './types';

export const parameters = {
docs: {
story: { inline: true },
extractArgTypes,
extractComponentDescription,
},
};

export const decorators: DecoratorFunction<ReactRenderer>[] = [jsxDecorator];

export const argTypesEnhancers: ArgTypesEnhancer<ReactRenderer>[] = [enhanceArgTypes];

export { applyDecorators } from './docs/applyDecorators';
2 changes: 2 additions & 0 deletions code/renderers/react/src/entry-preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const parameters: {} = { renderer: 'react' };
export { render, renderToCanvas } from './render';
13 changes: 13 additions & 0 deletions code/renderers/react/src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import type { StorybookConfig } from '@storybook/types';

import { join } from 'path';

export const addons: StorybookConfig['addons'] = [
require.resolve('@storybook/react-dom-shim/dist/preset'),
];

export const previewAnnotations: StorybookConfig['previewAnnotations'] = async (input, options) => {
const docsConfig = await options.presets.apply('docs', {}, options);
const docsEnabled = Object.keys(docsConfig).length > 0;
const result: string[] = [];

return result
.concat(input)
.concat([join(__dirname, 'entry-preview.mjs')])
.concat(docsEnabled ? [join(__dirname, 'entry-preview-docs.mjs')] : []);
};
16 changes: 4 additions & 12 deletions code/renderers/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,8 @@
"require": "./dist/index.js",
"import": "./dist/index.mjs"
},
"./preview": {
"types": "./dist/config.d.ts",
"require": "./dist/config.js",
"import": "./dist/config.mjs"
},
"./preset": {
"types": "./dist/preset.d.ts",
"require": "./dist/preset.js",
"import": "./dist/preset.mjs"
},
"./preset": "./preset.js",
"./dist/entry-preview.mjs": "./dist/entry-preview.mjs",
"./package.json": "./package.json"
},
"main": "dist/index.js",
Expand Down Expand Up @@ -77,8 +69,8 @@
"bundler": {
"entries": [
"./src/index.ts",
"./src/config.ts",
"./src/preset.ts"
"./src/preset.ts",
"./src/entry-preview.ts"
],
"platform": "browser"
},
Expand Down
1 change: 0 additions & 1 deletion code/renderers/server/preview.js

This file was deleted.

File renamed without changes.
12 changes: 12 additions & 0 deletions code/renderers/server/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import fs from 'fs-extra';
import yaml from 'yaml';
import type { StorybookConfig, Tag, StoryName, ComponentTitle } from '@storybook/types';

import { join } from 'path';

type FileContent = {
title: ComponentTitle;
tags?: Tag[];
Expand Down Expand Up @@ -34,3 +36,13 @@ export const experimental_indexers: StorybookConfig['experimental_indexers'] = (
},
...(existingIndexers || []),
];

export const previewAnnotations: StorybookConfig['previewAnnotations'] = async (input, options) => {
const { presetsList } = options;
if (!presetsList) {
return input;
}
const result: string[] = [];

return result.concat(input).concat([join(__dirname, 'entry-preview.mjs')]);
};
Loading

0 comments on commit aadef51

Please sign in to comment.