Skip to content

Commit

Permalink
fix: storybook hmr (#26)
Browse files Browse the repository at this point in the history
* fix: storybook hmr

* fix: type error
  • Loading branch information
dannyhw authored Mar 9, 2024
1 parent 4dd4b4c commit 8a3d4b5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
47 changes: 37 additions & 10 deletions apps/example/.storybook/storybook.requires.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -34,14 +38,37 @@ const normalizedStories = [
},
];

// @ts-ignore
declare global {
var view: ReturnType<typeof start>;
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;
6 changes: 3 additions & 3 deletions apps/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Binary file modified bun.lockb
Binary file not shown.

0 comments on commit 8a3d4b5

Please sign in to comment.