Skip to content

Commit

Permalink
remove storiesOf api
Browse files Browse the repository at this point in the history
  • Loading branch information
ndelangen committed Nov 1, 2023
1 parent 3f15a08 commit acdec9c
Show file tree
Hide file tree
Showing 17 changed files with 31 additions and 1,314 deletions.
28 changes: 0 additions & 28 deletions code/frameworks/angular/src/client/public-api.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1 @@
/* eslint-disable prefer-destructuring */
import { Addon_ClientStoryApi, Addon_Loadable } from '@storybook/types';
import { start } from '@storybook/preview-api';
import { renderToCanvas, render } from './render';
import decorateStory from './decorateStory';
import { AngularRenderer } from './types';

export * from './public-types';

const RENDERER = 'angular';

interface ClientApi extends Addon_ClientStoryApi<AngularRenderer['storyResult']> {
configure(loader: Addon_Loadable, module: NodeModule): void;
forceReRender(): void;
raw: () => any; // todo add type
load: (...args: any[]) => void;
}

const api = start<AngularRenderer>(renderToCanvas, { decorateStory, render });

export const storiesOf: ClientApi['storiesOf'] = (kind, m) => {
return (api.clientApi.storiesOf(kind, m) as ReturnType<ClientApi['storiesOf']>).addParameters({
renderer: RENDERER,
});
};

export const configure: ClientApi['configure'] = (...args) => api.configure(RENDERER, ...args);
export const forceReRender: ClientApi['forceReRender'] = api.forceReRender;
export const raw: ClientApi['raw'] = api.clientApi.raw;
15 changes: 0 additions & 15 deletions code/frameworks/ember/src/client/preview/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1 @@
import { start } from '@storybook/preview-api';

import './globals';
import type { EmberRenderer } from './types';
import { renderToCanvas } from './render';

const { configure: coreConfigure, clientApi, forceReRender } = start<EmberRenderer>(renderToCanvas);

export const { raw } = clientApi;

const RENDERER = 'ember';
export const storiesOf = (kind: string, m: any) =>
clientApi.storiesOf(kind, m).addParameters({ renderer: RENDERER });
export const configure = (...args: any[]) => coreConfigure(RENDERER, ...args);

export { forceReRender };
2 changes: 1 addition & 1 deletion code/frameworks/ember/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="webpack-env" />

export { storiesOf, configure, forceReRender, raw } from './client/preview';
import './client/preview';

// optimization: stop HMR propagation in webpack
if (typeof module !== 'undefined') module?.hot?.decline();
55 changes: 0 additions & 55 deletions code/lib/preview-api/src/modules/client-api/ClientApi.test.ts

This file was deleted.

151 changes: 0 additions & 151 deletions code/lib/preview-api/src/modules/client-api/ClientApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,157 +212,6 @@ export class ClientApi<TRenderer extends Renderer> {
);
}

// what are the occasions that "m" is a boolean vs an obj
storiesOf = (kind: string, m?: NodeModule): Addon_StoryApi<TRenderer['storyResult']> => {
if (!kind && typeof kind !== 'string') {
throw new Error('Invalid or missing kind provided for stories, should be a string');
}

if (!m) {
logger.warn(
`Missing 'module' parameter for story with a kind of '${kind}'. It will break your HMR`
);
}

if (m) {
const proto = Object.getPrototypeOf(m);
if (proto.exports && proto.exports.default) {
// FIXME: throw an error in SB6.0
logger.error(
`Illegal mix of CSF default export and storiesOf calls in a single file: ${proto.i}`
);
}
}

// eslint-disable-next-line no-plusplus
const baseFilename = m && m.id ? `${m.id}` : (this.lastFileName++).toString();
let fileName = baseFilename;
let i = 1;
// Deal with `storiesOf()` being called twice in the same file.
// On HMR, we clear _addedExports[fileName] below.

while (this._addedExports[fileName]) {
i += 1;
fileName = `${baseFilename}-${i}`;
}

if (m && m.hot && m.hot.accept) {
// This module used storiesOf(), so when it re-runs on HMR, it will reload
// itself automatically without us needing to look at our imports
m.hot.accept();
m.hot.dispose(() => {
this.facade.clearFilenameExports(fileName);

delete this._addedExports[fileName];

// We need to update the importFn as soon as the module re-evaluates
// (and calls storiesOf() again, etc). We could call `onImportFnChanged()`
// at the end of every setStories call (somehow), but then we'd need to
// debounce it somehow for initial startup. Instead, we'll take advantage of
// the fact that the evaluation of the module happens immediately in the same tick
setTimeout(() => {
this._loadAddedExports();
this.onImportFnChanged?.({ importFn: this.importFn.bind(this) });
}, 0);
});
}

let hasAdded = false;
const api: Addon_StoryApi<TRenderer['storyResult']> = {
kind: kind.toString(),
add: () => api,
addDecorator: () => api,
addLoader: () => api,
addParameters: () => api,
};

// apply addons
Object.keys(this.addons).forEach((name) => {
const addon = this.addons[name];
api[name] = (...args: any[]) => {
addon.apply(api, args);
return api;
};
});

const meta: NormalizedComponentAnnotations<TRenderer> = {
id: sanitize(kind),
title: kind,
decorators: [],
loaders: [],
parameters: {},
};
// We map these back to a simple default export, even though we have type guarantees at this point

this._addedExports[fileName] = { default: meta };

let counter = 0;
api.add = (storyName: string, storyFn: StoryFn<TRenderer>, parameters: Parameters = {}) => {
hasAdded = true;

if (typeof storyName !== 'string') {
throw new Error(`Invalid or missing storyName provided for a "${kind}" story.`);
}

if (!storyFn || Array.isArray(storyFn) || invalidStoryTypes.has(typeof storyFn)) {
throw new Error(
`Cannot load story "${storyName}" in "${kind}" due to invalid format. Storybook expected a function/object but received ${typeof storyFn} instead.`
);
}

const { decorators, loaders, component, args, argTypes, ...storyParameters } = parameters;

const storyId = parameters.__id || toId(kind, storyName);

const csfExports = this._addedExports[fileName];
// Whack a _ on the front incase it is "default"
csfExports[`story${counter}`] = {
name: storyName,
parameters: { fileName, __id: storyId, ...storyParameters },
decorators,
loaders,
args,
argTypes,
component,
render: storyFn,
};
counter += 1;

return api;
};

api.addDecorator = (decorator: DecoratorFunction<TRenderer>) => {
if (hasAdded)
throw new Error(`You cannot add a decorator after the first story for a kind.
Read more here: https://github.com/storybookjs/storybook/blob/master/MIGRATION.md#can-no-longer-add-decoratorsparameters-after-stories`);

meta.decorators?.push(decorator);
return api;
};

api.addLoader = (loader: LoaderFunction<TRenderer>) => {
if (hasAdded) throw new Error(`You cannot add a loader after the first story for a kind.`);

meta.loaders?.push(loader);
return api;
};

api.addParameters = ({ component, args, argTypes, tags, ...parameters }: Parameters) => {
if (hasAdded)
throw new Error(`You cannot add parameters after the first story for a kind.
Read more here: https://github.com/storybookjs/storybook/blob/master/MIGRATION.md#can-no-longer-add-decoratorsparameters-after-stories`);

meta.parameters = combineParameters(meta.parameters, parameters);
if (component) meta.component = component;
if (args) meta.args = { ...meta.args, ...args };
if (argTypes) meta.argTypes = { ...meta.argTypes, ...argTypes };
if (tags) meta.tags = tags;
return api;
};

return api;
};

// @deprecated
raw = () => {
return this.storyStore?.raw();
Expand Down
Loading

0 comments on commit acdec9c

Please sign in to comment.