From c678ae25175fe10f420f69d7557904e7fd5a0ac3 Mon Sep 17 00:00:00 2001 From: Benoit Rajalu Date: Thu, 21 Mar 2024 14:35:12 +0100 Subject: [PATCH 1/2] Fix - Make loki work with Storybook8 --- packages/browser/src/get-stories.js | 39 +++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/packages/browser/src/get-stories.js b/packages/browser/src/get-stories.js index 23ba3d39..06e809c8 100644 --- a/packages/browser/src/get-stories.js +++ b/packages/browser/src/get-stories.js @@ -3,6 +3,7 @@ const getStories = async (window) => { const getStorybook = (window.__STORYBOOK_CLIENT_API__ && window.__STORYBOOK_CLIENT_API__.raw) || + (window.__STORYBOOK_PREVIEW__ && window.__STORYBOOK_PREVIEW__.extract && window.__STORYBOOK_PREVIEW__.storyStore.raw) || (window.loki && window.loki.getStorybook); if (!getStorybook) { throw new Error( @@ -19,13 +20,6 @@ const getStories = async (window) => { 'storySource', ]; - if ( - window.__STORYBOOK_CLIENT_API__.storyStore && - window.__STORYBOOK_CLIENT_API__.storyStore.cacheAllCSFFiles - ) { - await window.__STORYBOOK_CLIENT_API__.storyStore.cacheAllCSFFiles(); - } - const isSerializable = (value) => { try { JSON.stringify(value); @@ -35,6 +29,37 @@ const getStories = async (window) => { } }; + if (window.__STORYBOOK_PREVIEW__ && window.__STORYBOOK_PREVIEW__.extract) { + // New official API to extract stories from preview + await window.__STORYBOOK_PREVIEW__.extract(); + + // Deprecated, will be removed in V9 + const stories = window.__STORYBOOK_PREVIEW__.storyStore.raw(); + + return stories + .map((component) => ({ + id: component.id, + kind: component.kind, + story: component.story, + parameters: Object.fromEntries( + Object.entries(component.parameters || {}).filter( + ([key, value]) => + !key.startsWith('__') && + !blockedParams.includes(key) && + isSerializable(value) + ) + ), + })) + .filter(({ parameters }) => !parameters.loki || !parameters.loki.skip); + } + + if ( + window.__STORYBOOK_CLIENT_API__.storyStore && + window.__STORYBOOK_CLIENT_API__.storyStore.cacheAllCSFFiles + ) { + await window.__STORYBOOK_CLIENT_API__.storyStore.cacheAllCSFFiles(); + } + return getStorybook() .map((component) => ({ id: component.id, From 7d95bdceeee3ae70154c471982216738abda141c Mon Sep 17 00:00:00 2001 From: Joel Arvidsson Date: Fri, 29 Mar 2024 20:01:25 +0100 Subject: [PATCH 2/2] Fix formatting --- packages/browser/src/get-stories.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/browser/src/get-stories.js b/packages/browser/src/get-stories.js index 06e809c8..8d62d36f 100644 --- a/packages/browser/src/get-stories.js +++ b/packages/browser/src/get-stories.js @@ -3,7 +3,9 @@ const getStories = async (window) => { const getStorybook = (window.__STORYBOOK_CLIENT_API__ && window.__STORYBOOK_CLIENT_API__.raw) || - (window.__STORYBOOK_PREVIEW__ && window.__STORYBOOK_PREVIEW__.extract && window.__STORYBOOK_PREVIEW__.storyStore.raw) || + (window.__STORYBOOK_PREVIEW__ && + window.__STORYBOOK_PREVIEW__.extract && + window.__STORYBOOK_PREVIEW__.storyStore.raw) || (window.loki && window.loki.getStorybook); if (!getStorybook) { throw new Error(