From 99aad422b8a2ee6d46a2e214c9b89251ac97eeb0 Mon Sep 17 00:00:00 2001 From: Sergei Osipov Date: Tue, 8 Oct 2019 03:01:54 +0300 Subject: [PATCH 1/2] Migrate @storybook/mithril to TypeScript --- app/mithril/package.json | 4 +++ app/mithril/src/client/{index.js => index.ts} | 0 .../client/preview/{globals.js => globals.ts} | 0 app/mithril/src/client/preview/index.js | 21 ------------ app/mithril/src/client/preview/index.ts | 34 +++++++++++++++++++ .../client/preview/{render.js => render.ts} | 5 +-- app/mithril/src/client/preview/types.ts | 28 +++++++++++++++ app/mithril/src/server/{build.js => build.ts} | 0 ...mithril.js => framework-preset-mithril.ts} | 4 ++- app/mithril/src/server/{index.js => index.ts} | 0 .../src/server/{options.js => options.ts} | 0 app/mithril/src/typings.d.ts | 2 ++ app/mithril/tsconfig.json | 15 ++++++++ yarn.lock | 7 +++- 14 files changed, 95 insertions(+), 25 deletions(-) rename app/mithril/src/client/{index.js => index.ts} (100%) rename app/mithril/src/client/preview/{globals.js => globals.ts} (100%) delete mode 100644 app/mithril/src/client/preview/index.js create mode 100644 app/mithril/src/client/preview/index.ts rename app/mithril/src/client/preview/{render.js => render.ts} (91%) create mode 100644 app/mithril/src/client/preview/types.ts rename app/mithril/src/server/{build.js => build.ts} (100%) rename app/mithril/src/server/{framework-preset-mithril.js => framework-preset-mithril.ts} (53%) rename app/mithril/src/server/{index.js => index.ts} (100%) rename app/mithril/src/server/{options.js => options.ts} (100%) create mode 100644 app/mithril/src/typings.d.ts create mode 100644 app/mithril/tsconfig.json diff --git a/app/mithril/package.json b/app/mithril/package.json index 38a03dda3dd5..f4b101a34b61 100644 --- a/app/mithril/package.json +++ b/app/mithril/package.json @@ -22,6 +22,7 @@ "standalone.js" ], "main": "dist/client/index.js", + "types": "dist/client/index.d.ts", "bin": { "build-storybook": "./bin/build.js", "start-storybook": "./bin/index.js", @@ -31,7 +32,9 @@ "prepare": "node ../../scripts/prepare.js" }, "dependencies": { + "@babel/core": "^7.6.2", "@babel/plugin-transform-react-jsx": "^7.3.0", + "@storybook/addons": "5.3.0-alpha.12", "@storybook/core": "5.3.0-alpha.12", "core-js": "^3.0.1", "global": "^4.3.2", @@ -39,6 +42,7 @@ "ts-dedent": "^1.1.0" }, "devDependencies": { + "@types/mithril": "^2.0.0", "mithril": "^1.1.6" }, "peerDependencies": { diff --git a/app/mithril/src/client/index.js b/app/mithril/src/client/index.ts similarity index 100% rename from app/mithril/src/client/index.js rename to app/mithril/src/client/index.ts diff --git a/app/mithril/src/client/preview/globals.js b/app/mithril/src/client/preview/globals.ts similarity index 100% rename from app/mithril/src/client/preview/globals.js rename to app/mithril/src/client/preview/globals.ts diff --git a/app/mithril/src/client/preview/index.js b/app/mithril/src/client/preview/index.js deleted file mode 100644 index cb23a2edc6c8..000000000000 --- a/app/mithril/src/client/preview/index.js +++ /dev/null @@ -1,21 +0,0 @@ -import { start } from '@storybook/core/client'; - -import './globals'; -import render from './render'; - -const { configure: coreConfigure, clientApi, forceReRender } = start(render); - -export const { - setAddon, - addDecorator, - addParameters, - clearDecorators, - getStorybook, - raw, -} = clientApi; - -const framework = 'mithril'; -export const storiesOf = (...args) => clientApi.storiesOf(...args).addParameters({ framework }); -export const configure = (...args) => coreConfigure(...args, framework); - -export { forceReRender }; diff --git a/app/mithril/src/client/preview/index.ts b/app/mithril/src/client/preview/index.ts new file mode 100644 index 000000000000..a68d76ad0b48 --- /dev/null +++ b/app/mithril/src/client/preview/index.ts @@ -0,0 +1,34 @@ +import { start } from '@storybook/core/client'; + +import './globals'; +import { ClientStoryApi, Loadable } from '@storybook/addons'; +import render from './render'; + +import { IStorybookSection, StoryFnMithrilReturnType } from './types'; + +const { configure: coreConfigure, clientApi, forceReRender } = start(render); + +const framework = 'mithril'; + +interface ClientApi extends ClientStoryApi { + setAddon(addon: any): void; + configure(loader: Loadable, module: NodeModule): void; + getStorybook(): IStorybookSection[]; + clearDecorators(): void; + forceReRender(): void; + raw: () => any; // todo add type +} + +export const storiesOf: ClientApi['storiesOf'] = (kind, m) => + (clientApi.storiesOf(kind, m) as ReturnType).addParameters({ framework }); + +export const configure: ClientApi['configure'] = (...args) => coreConfigure(...args, framework); + +export const { setAddon } = clientApi; +export const { addDecorator } = clientApi; +export const { addParameters } = clientApi; +export const { clearDecorators } = clientApi; +export const { getStorybook } = clientApi; +export const { raw } = clientApi; + +export { forceReRender }; diff --git a/app/mithril/src/client/preview/render.js b/app/mithril/src/client/preview/render.ts similarity index 91% rename from app/mithril/src/client/preview/render.js rename to app/mithril/src/client/preview/render.ts index 209cf9449299..a368489d5055 100644 --- a/app/mithril/src/client/preview/render.js +++ b/app/mithril/src/client/preview/render.ts @@ -4,6 +4,8 @@ import { document } from 'global'; import m from 'mithril'; import dedent from 'ts-dedent'; +import { RenderMainArgs } from './types'; + const rootEl = document.getElementById('root'); export default function renderMain({ @@ -12,8 +14,7 @@ export default function renderMain({ selectedStory, showMain, showError, - // forceRender, -}) { +}: RenderMainArgs) { const element = storyFn(); if (!element) { diff --git a/app/mithril/src/client/preview/types.ts b/app/mithril/src/client/preview/types.ts new file mode 100644 index 000000000000..c9755ab82eb0 --- /dev/null +++ b/app/mithril/src/client/preview/types.ts @@ -0,0 +1,28 @@ +import m from 'mithril'; + +export interface IStorybookStory { + name: string; + render: () => any; +} + +export interface IStorybookSection { + kind: string; + stories: IStorybookStory[]; +} + +export type StoryFnMithrilReturnType = m.Component; + +export interface ShowErrorArgs { + title: string; + description: string; +} + +export interface RenderMainArgs { + storyFn: () => StoryFnMithrilReturnType; + selectedKind: string; + selectedStory: string; + showMain: () => void; + showError: (args: ShowErrorArgs) => void; + showException: (err: Error) => void; + forceRender: boolean; +} diff --git a/app/mithril/src/server/build.js b/app/mithril/src/server/build.ts similarity index 100% rename from app/mithril/src/server/build.js rename to app/mithril/src/server/build.ts diff --git a/app/mithril/src/server/framework-preset-mithril.js b/app/mithril/src/server/framework-preset-mithril.ts similarity index 53% rename from app/mithril/src/server/framework-preset-mithril.js rename to app/mithril/src/server/framework-preset-mithril.ts index ca0d622e4184..c54576fb127a 100644 --- a/app/mithril/src/server/framework-preset-mithril.js +++ b/app/mithril/src/server/framework-preset-mithril.ts @@ -1,4 +1,6 @@ -export function babelDefault(config) { +import { TransformOptions } from '@babel/core'; + +export function babelDefault(config: TransformOptions) { return { ...config, plugins: [...config.plugins, require.resolve('@babel/plugin-transform-react-jsx')], diff --git a/app/mithril/src/server/index.js b/app/mithril/src/server/index.ts similarity index 100% rename from app/mithril/src/server/index.js rename to app/mithril/src/server/index.ts diff --git a/app/mithril/src/server/options.js b/app/mithril/src/server/options.ts similarity index 100% rename from app/mithril/src/server/options.js rename to app/mithril/src/server/options.ts diff --git a/app/mithril/src/typings.d.ts b/app/mithril/src/typings.d.ts new file mode 100644 index 000000000000..6288cba4b09a --- /dev/null +++ b/app/mithril/src/typings.d.ts @@ -0,0 +1,2 @@ +declare module '@storybook/core/*'; +declare module 'global'; diff --git a/app/mithril/tsconfig.json b/app/mithril/tsconfig.json new file mode 100644 index 000000000000..ee16ed33c0a4 --- /dev/null +++ b/app/mithril/tsconfig.json @@ -0,0 +1,15 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": ".", + "types": ["webpack-env"], + "resolveJsonModule": true + }, + "include": [ + "src/**/*", + "package.json" + ], + "exclude": [ + "src/**/*.test.*" + ] +} diff --git a/yarn.lock b/yarn.lock index c991c52b7ad8..f49907b949dd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -286,7 +286,7 @@ semver "^5.4.1" source-map "^0.5.0" -"@babel/core@^7.0.0", "@babel/core@^7.1.0", "@babel/core@^7.1.6", "@babel/core@^7.2.2", "@babel/core@^7.3.3", "@babel/core@^7.3.4", "@babel/core@^7.4.3", "@babel/core@^7.4.5": +"@babel/core@^7.0.0", "@babel/core@^7.1.0", "@babel/core@^7.1.6", "@babel/core@^7.2.2", "@babel/core@^7.3.3", "@babel/core@^7.3.4", "@babel/core@^7.4.3", "@babel/core@^7.4.5", "@babel/core@^7.6.2": version "7.6.2" resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.6.2.tgz#069a776e8d5e9eefff76236bc8845566bd31dd91" integrity sha512-l8zto/fuoZIbncm+01p8zPSDZu/VuuJhAfA7d/AbzM09WR7iVhavvfNDYCNpo1VvLk6E6xgAoP9P+/EMJHuRkQ== @@ -3810,6 +3810,11 @@ resolved "https://registry.yarnpkg.com/@types/mithril/-/mithril-1.1.17.tgz#1a5dde2a653ef6011cd36ef70eefe6bac0cb92e4" integrity sha512-U/wwKZT8hjstY2Q470bLMGVh/fjT2+SgBMzIILn0Z4nmgzzG6j+n18UOAxQ63aI8vXIOkQsbkAdbESt8+jIQdQ== +"@types/mithril@^2.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@types/mithril/-/mithril-2.0.0.tgz#f9a9e523264af6dc8c2027029bcd400a459739bb" + integrity sha512-Bjjs5a7xc6eC207ZE1tR7ssnMoF8d3VVcupmzbhFaP2nM4G1Jpm1oqrvAiu+ys0OLKYUQjxNALUerHDPHbuwfA== + "@types/node@*", "@types/node@^12.7.9": version "12.7.10" resolved "https://registry.yarnpkg.com/@types/node/-/node-12.7.10.tgz#942c128c37b5d780aff5441fd457cd7ffdc18b36" From c925c8717704235c3e7d4865daeff564f9c1c4e4 Mon Sep 17 00:00:00 2001 From: Sergei Osipov Date: Thu, 17 Oct 2019 20:16:31 +0300 Subject: [PATCH 2/2] chore: move @types/mithril to deps --- app/mithril/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/mithril/package.json b/app/mithril/package.json index 455f248b1fd8..e7deb5c398e0 100644 --- a/app/mithril/package.json +++ b/app/mithril/package.json @@ -37,13 +37,13 @@ "@babel/plugin-transform-react-jsx": "^7.3.0", "@storybook/addons": "5.3.0-alpha.21", "@storybook/core": "5.3.0-alpha.21", + "@types/mithril": "^2.0.0", "core-js": "^3.0.1", "global": "^4.3.2", "regenerator-runtime": "^0.13.3", "ts-dedent": "^1.1.0" }, "devDependencies": { - "@types/mithril": "^2.0.0", "mithril": "^1.1.6" }, "peerDependencies": {