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

Migrate @storybook/html to Typescript #7338

Merged
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: 2 additions & 0 deletions app/html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"license": "MIT",
"main": "dist/client/index.js",
"types": "dist/client/index.d.ts",
"bin": {
"build-storybook": "./bin/build.js",
"start-storybook": "./bin/index.js",
Expand All @@ -25,6 +26,7 @@
"prepare": "node ../../scripts/prepare.js"
},
"dependencies": {
"@storybook/addons": "5.2.0-alpha.41",
"@storybook/core": "5.2.0-alpha.41",
"common-tags": "^1.8.0",
"core-js": "^3.0.1",
Expand Down
File renamed without changes.
22 changes: 0 additions & 22 deletions app/html/src/client/preview/index.js

This file was deleted.

37 changes: 37 additions & 0 deletions app/html/src/client/preview/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-disable prefer-destructuring */
import { start } from '@storybook/core/client';
import { ClientStoryApi } from '@storybook/addons';

import './globals';
import render from './render';
import { StoryFnHtmlReturnType, IStorybookSection } from './types';

const framework = 'html';

interface ClientApi extends ClientStoryApi<StoryFnHtmlReturnType> {
setAddon(addon: any): void;
configure(loaders: () => void, module: NodeModule): void;
getStorybook(): IStorybookSection[];
clearDecorators(): void;
forceReRender(): void;
raw: () => any; // todo add type
load: (req: any, m: NodeModule, framework: string) => void;
}

const api = start(render);

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

export const load: ClientApi['load'] = (...args) => api.load(...args, framework);
export const addDecorator: ClientApi['addDecorator'] = api.clientApi.addDecorator;
export const addParameters: ClientApi['addParameters'] = api.clientApi.addParameters;
export const clearDecorators: ClientApi['clearDecorators'] = api.clientApi.clearDecorators;
export const setAddon: ClientApi['setAddon'] = api.clientApi.setAddon;
export const configure: ClientApi['configure'] = api.configApi.configure;
export const forceReRender: ClientApi['forceReRender'] = api.forceReRender;
export const getStorybook: ClientApi['getStorybook'] = api.clientApi.getStorybook;
export const raw: ClientApi['raw'] = api.clientApi.raw;
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { document, Node } from 'global';
import { stripIndents } from 'common-tags';
import { RenderMainArgs } from './types';

const rootElement = document.getElementById('root');

export default function renderMain({
parameters = {},
storyFn,
selectedKind,
selectedStory,
showMain,
showError,
forceRender,
}) {
}: RenderMainArgs) {
const element = storyFn();

showMain();
Expand Down
27 changes: 27 additions & 0 deletions app/html/src/client/preview/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { StoryFn } from '@storybook/addons';

export type StoryFnHtmlReturnType = string | Node;

export interface IStorybookStory {
name: string;
render: () => any;
}

export interface IStorybookSection {
kind: string;
stories: IStorybookStory[];
}

export interface ShowErrorArgs {
title: string;
description: string;
}

export interface RenderMainArgs {
storyFn: () => StoryFn<StoryFnHtmlReturnType>;
selectedKind: string;
selectedStory: string;
showMain: () => void;
showError: (args: ShowErrorArgs) => void;
forceRender: boolean;
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export function webpack(config) {
// eslint-disable-next-line import/no-extraneous-dependencies
import { Configuration } from 'webpack';
Copy link
Member

Choose a reason for hiding this comment

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

I don't know well how this part of SB is working but maybe webpack should be a peerDependency?
@ndelangen can surely help us with this ;)

Copy link
Member

Choose a reason for hiding this comment

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

You can create a dependency on webpack for now, in monoconfig, I'll try an consolidate these types into a coherent whole.

When you add the dependency, be sure to add a wide version range.

Copy link
Member

@Hypnosphi Hypnosphi Jul 8, 2019

Choose a reason for hiding this comment

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

I think we should declare all the preset function types in @storybook/core

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to clarify, in this case Configuration is being picked up through @types/webpack which is a devDependency at the root of the repo. Technically, there's no runtime use or type definitions coming from webpack here.

It would make sense to add @types/webpack as a devDependency here—I could match the version at the root of the repo to avoid any conflicts. Given the above, do you guys still feel I should add webpack as a dependency? Let me know your thoughts 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gaetanmaisse @ndelangen let me know your thoughts on my last comment and I'll make the change :)


export function webpack(config: Configuration) {
return {
...config,
module: {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import packageJson from '../../package.json';
// tslint:disable-next-line: no-var-requires
const packageJson = require('../../package.json');

export default {
packageJson,
Expand Down
5 changes: 5 additions & 0 deletions app/html/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module '@storybook/core/*';
declare module 'global';

// will be provided by the webpack define plugin
declare var NODE_ENV: string | undefined;
9 changes: 9 additions & 0 deletions app/html/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"types": ["webpack-env"]
},
"include": ["src/**/*"],
"exclude": ["src/__tests__/**/*"]
}