From 8a3d4b519462bef471c9ca1586ec79e4604fd962 Mon Sep 17 00:00:00 2001 From: Daniel Williams Date: Sat, 9 Mar 2024 14:56:10 +0000 Subject: [PATCH] fix: storybook hmr (#26) * fix: storybook hmr * fix: type error --- apps/example/.storybook/storybook.requires.ts | 47 ++++++++++++++---- apps/example/package.json | 6 +-- bun.lockb | Bin 887904 -> 887904 bytes 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/apps/example/.storybook/storybook.requires.ts b/apps/example/.storybook/storybook.requires.ts index 2a90ed6..5e57996 100644 --- a/apps/example/.storybook/storybook.requires.ts +++ b/apps/example/.storybook/storybook.requires.ts @@ -1,6 +1,10 @@ /* do not change this file, it is auto generated by storybook. */ -import { start } from "@storybook/react-native"; +import { + start, + prepareStories, + getProjectAnnotations, +} from "@storybook/react-native"; import "@storybook/addon-ondevice-controls/register"; import "@storybook/addon-ondevice-actions/register"; @@ -34,14 +38,37 @@ const normalizedStories = [ }, ]; -// @ts-ignore +declare global { + var view: ReturnType; + var STORIES: typeof normalizedStories; +} + +const annotations = [ + require("./preview"), + require("@storybook/react-native/dist/preview"), + require("@storybook/addon-actions/preview"), +]; + global.STORIES = normalizedStories; -export const view = start({ - annotations: [ - require("./preview"), - require("@storybook/react-native/dist/preview"), - require("@storybook/addon-actions/preview"), - ], - storyEntries: normalizedStories, -}); +// @ts-ignore +module?.hot?.accept?.(); + +if (!global.view) { + global.view = start({ + annotations, + storyEntries: normalizedStories, + }); +} else { + const { importMap } = prepareStories({ storyEntries: normalizedStories }); + + global.view._preview.onStoriesChanged({ + importFn: async (importPath: string) => importMap[importPath], + }); + + global.view._preview.onGetProjectAnnotationsChanged({ + getProjectAnnotations: getProjectAnnotations(global.view, annotations), + }); +} + +export const view = global.view; diff --git a/apps/example/package.json b/apps/example/package.json index a6ba395..ba8cb5f 100644 --- a/apps/example/package.json +++ b/apps/example/package.json @@ -31,9 +31,9 @@ "@react-native-async-storage/async-storage": "^1.21.0", "@react-native-community/datetimepicker": "^7.6.2", "@react-native-community/slider": "^4.5.0", - "@storybook/addon-ondevice-actions": "^7.6.15", - "@storybook/addon-ondevice-controls": "^7.6.15", - "@storybook/react-native": "^7.6.15", + "@storybook/addon-ondevice-actions": "^7.6.17", + "@storybook/addon-ondevice-controls": "^7.6.17", + "@storybook/react-native": "^7.6.17", "@types/react": "~18.2.45", "babel-loader": "^8.3.0", "eslint": "^8.56.0", diff --git a/bun.lockb b/bun.lockb index 0c7011efeea456e1f1bb1ad3c231740b2f383e9c..c130e685d787e3442a9331e4b500a043de100f81 100755 GIT binary patch delta 520 zcmaDb!|cHfvk7_%ap{kwthYW}^=R$lwNJJGbH>HAosJT!v}nHky85kSTt}l{s~_W5 zKc-j7(|@Eg8Enr;VG3Y`FkhrHO%R4KS5z_u@PV1_8XZhP%nZaV+ci2^MWq~ey*xd2 zSN;BDws#tROy%-s-mv@Hcg8R#^yv<+e`$N~64n<|4zH^8K4#wX*t{g6 zBA;ugd2;RAgB|lPzuG;&AZ*=&s{503TipWXxjtxItL2Usm6+-EViWuHDN~qM$}ZCB zuhY0;x_#?@)(yfACq6VkDw!8^C}v7U(&FviJ^k8p>ykx#ek+B9F8_7lm>#nY?@Gbj zw{EPHVcs}h`>J2NUzqiVwh8|ZsqJ{ynz;X0`_jv-K+Fcj>_E)1ed%S+*u&E14D%b9 z7?cbN7 znKyC)G52=njXWEc02OTs<^91lJ-vWed;9eW-cRDw^NM)|rrWjivTeU#!0RstRHEO` HYsd-!6i?VsG?o^<&)X z$Mh7zUz>Nt%v*L z%{A`>9!!}&k<+$p8|#&?Yje-GA6Gf4e(gx2$xG&RyWJD-g2*F*^`*Y+rhrGxo5wDZ~5* zCI%%2hK5MhSbOzIb2|>kICDKSJwwy!veUUF8BMpFPv_!iVKklI-O44sJ!v-A0#QcO zcIJ&-K+L_Jc_Yt;B|t@6LV15MO=slgR+wH8!7H%+P6Y2;aiAvsc3#o#_X~La#ej-{ H979$Blri5r