-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move store resolution to loader
- Loading branch information
1 parent
f0b0beb
commit 7caf7d5
Showing
9 changed files
with
63 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ Object { | |
"description": "", | ||
"displayName": "Button", | ||
"methods": Array [], | ||
"props": undefined, | ||
} | ||
`; | ||
|
||
|
8 changes: 8 additions & 0 deletions
8
core/instrument/test/fixtures/csf/exclude-stories/include-stories-array.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default { | ||
title: 'Storybook/Blocks/IncludeStoryArray', | ||
includeStories: ['story1', 'story2'], | ||
}; | ||
|
||
export const story1 = () => {}; | ||
export const story2 = () => {}; | ||
export const story3 = () => {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,8 @@ | ||
import { loader } from 'webpack'; | ||
import { StoriesStore } from '@component-controls/specification'; | ||
|
||
const store: StoriesStore = { | ||
kinds: {}, | ||
stories: {}, | ||
components: {}, | ||
}; | ||
const stores: StoriesStore[] = []; | ||
|
||
export const addStoriesKind = async ( | ||
added: StoriesStore, | ||
context: loader.LoaderContext, | ||
) => { | ||
Object.keys(added.kinds).forEach(key => { | ||
store.kinds[key] = { | ||
...added.kinds[key], | ||
fileName: context.resourcePath, | ||
}; | ||
}); | ||
Object.keys(added.components).forEach(key => { | ||
store.components[key] = { | ||
...added.components[key], | ||
}; | ||
const { from } = added.components[key]; | ||
if (typeof from === 'string') { | ||
context.resolve(context.context, from, function( | ||
err: any, | ||
result: string, | ||
) { | ||
if (!err) { | ||
store.components[key].request = result; | ||
} else { | ||
console.error(err); | ||
} | ||
}); | ||
} | ||
}); | ||
Object.keys(added.stories).forEach(key => { | ||
store.stories[key] = { ...added.stories[key] }; | ||
}); | ||
export const addStoriesKind = async (added: StoriesStore) => { | ||
stores.push(added); | ||
}; | ||
export default store; | ||
export default stores; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,49 @@ | ||
import { StoriesStore, Story } from '@component-controls/specification'; | ||
import { toId, storyNameFromExport } from '@storybook/csf'; | ||
const injectedStories = '__STORIES_HASH__INJECTED_STORIES__'; | ||
|
||
const storyStore = () => { | ||
let storyStore: StoriesStore | undefined = undefined; | ||
|
||
const loadStoryStore = (): StoriesStore | undefined => { | ||
if (storyStore) { | ||
return storyStore; | ||
} | ||
if (injectedStories) { | ||
const globalStore: StoriesStore = { | ||
kinds: {}, | ||
stories: {}, | ||
components: {}, | ||
}; | ||
try { | ||
return JSON.parse(injectedStories); | ||
} catch (e) { | ||
return {}; | ||
} | ||
const stores: StoriesStore[] = (storyStore = JSON.parse(injectedStories)); | ||
if (stores) { | ||
stores.forEach(store => { | ||
if (Object.keys(store.kinds).length > 0) { | ||
Object.keys(store.kinds).forEach(kindName => { | ||
const kind = store.kinds[kindName]; | ||
globalStore.kinds[kindName] = kind; | ||
Object.keys(store.stories).forEach(storyName => { | ||
const story: Story = store.stories[storyName]; | ||
if (kind.title && story.name) { | ||
const id = toId(kind.title, storyNameFromExport(story.name)); | ||
if (!kind.stories) { | ||
kind.stories = []; | ||
} | ||
kind.stories.push(id); | ||
globalStore.stories[id] = { ...story, id, kind: kind.title }; | ||
} | ||
}); | ||
Object.keys(store.components).forEach(key => { | ||
globalStore.components[key] = store.components[key]; | ||
}); | ||
}); | ||
} | ||
}); | ||
} | ||
} catch (e) {} | ||
storyStore = globalStore; | ||
} | ||
return undefined; | ||
return storyStore; | ||
}; | ||
|
||
export default storyStore(); | ||
export default storyStore ? storyStore : loadStoryStore(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters