-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
51 changed files
with
952 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const svelte = require('svelte/compiler'); | ||
|
||
const parser = require.resolve('./src/client/parse-stories').replace(/[/\\]/g, '/'); | ||
|
||
function process(src, filename) { | ||
const result = svelte.compile(src, { | ||
format: 'cjs', | ||
filename, | ||
}); | ||
|
||
const code = result.js ? result.js.code : result.code; | ||
|
||
const z = { | ||
code: `${code} | ||
const { default: parser } = require('${parser}'); | ||
module.exports = parser(module.exports, {}); | ||
Object.defineProperty(exports, "__esModule", { value: true });`, | ||
map: result.js ? result.js.map : result.map, | ||
}; | ||
return z; | ||
} | ||
|
||
exports.process = process; |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<script> | ||
import { useContext } from './context'; | ||
useContext().meta = $$props; | ||
</script> | ||
|
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,11 @@ | ||
<script> | ||
import { createRegistrationContext } from './context'; | ||
export let Stories; | ||
export let repositories; | ||
createRegistrationContext(repositories); | ||
</script> | ||
|
||
<svelte:component this={Stories} /> |
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,13 @@ | ||
<script> | ||
/** | ||
* @component | ||
* @wrapper | ||
*/ | ||
import { createRenderContext } from './context'; | ||
export let Stories; | ||
createRenderContext($$props); | ||
</script> | ||
|
||
<svelte:component this={Stories}/> |
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,24 @@ | ||
<script> | ||
import { useContext } from './context'; | ||
const context = useContext(); | ||
export let name; | ||
export let template; | ||
if (!name) { | ||
throw new Error('Missing Story name'); | ||
} | ||
context.register({ | ||
name, | ||
...$$restProps, | ||
template: template != null ? template : !$$slots.default ? 'default' : null, | ||
}); | ||
$: render = context.render && !context.templateName && context.storyName == name; | ||
</script> | ||
|
||
{#if render} | ||
<slot args={context.args} {...context.args}/> | ||
{/if} |
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,16 @@ | ||
<script> | ||
import { useContext } from './context'; | ||
const context = useContext(); | ||
export let name = 'default'; | ||
context.register({name, isTemplate: true}); | ||
$: render = context.render && context.templateName === name; | ||
</script> | ||
|
||
{#if render} | ||
<slot args={context.args} {...context.args}/> | ||
{/if} | ||
|
21 changes: 21 additions & 0 deletions
21
app/svelte/src/client/components/__tests__/TestStories.svelte
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,21 @@ | ||
<script> | ||
import Meta from '../Meta.svelte'; | ||
import Story from '../Story.svelte'; | ||
import Template from '../Template.svelte'; | ||
</script> | ||
|
||
<Meta title="Test"/> | ||
|
||
<Template name="tpl1"> | ||
<div>tpl1</div> | ||
</Template> | ||
|
||
<Template name="tpl2"> | ||
<div>tpl2</div> | ||
</Template> | ||
|
||
<Story name="Story1" template="tpl1" args={{tpl1:true}}/> | ||
<Story name="Story2" template="tpl2" source args={{tpl1:true}}/> | ||
<Story name="Story3" source="xyz"> | ||
<div>story3</div> | ||
</Story> |
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,34 @@ | ||
import { getContext, hasContext, setContext } from 'svelte'; | ||
|
||
const CONTEXT_KEY = 'storybook-registration-context'; | ||
|
||
export function createRenderContext(props: any = {}) { | ||
setContext(CONTEXT_KEY, { | ||
render: true, | ||
register: () => {}, | ||
meta: {}, | ||
args: {}, | ||
...props, | ||
}); | ||
} | ||
|
||
export function createRegistrationContext(repositories: any) { | ||
setContext(CONTEXT_KEY, { | ||
render: false, | ||
register: (story: any) => { | ||
repositories.stories.push(story); | ||
}, | ||
set meta(value: any) { | ||
// eslint-disable-next-line no-param-reassign | ||
repositories.meta = value; | ||
}, | ||
args: {}, | ||
}); | ||
} | ||
|
||
export function useContext() { | ||
if (!hasContext(CONTEXT_KEY)) { | ||
createRenderContext(); | ||
} | ||
return getContext(CONTEXT_KEY); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* eslint-env browser */ | ||
import { logger } from '@storybook/client-logger'; | ||
import { combineParameters } from '@storybook/client-api'; | ||
|
||
import RegisterContext from './components/RegisterContext.svelte'; | ||
import RenderContext from './components/RenderContext.svelte'; | ||
|
||
/* Called from a webpack loader and a jest transformation. | ||
* | ||
* It mounts a Stories component in a context which disables | ||
* the rendering of every <Story/> and <Template/> but instead | ||
* collects names and properties. | ||
* | ||
* For every discovered <Story/>, it creates a storyFun which | ||
* instanciate the main Stories component: Every Story but | ||
* the one selected is disabled. | ||
*/ | ||
|
||
const createFragment = document.createDocumentFragment | ||
? () => document.createDocumentFragment() | ||
: () => document.createElement('div'); | ||
|
||
export default (module, sources) => { | ||
const { default: Stories } = module; | ||
|
||
const repositories = { | ||
meta: null, | ||
stories: [], | ||
}; | ||
|
||
// extract all stories | ||
try { | ||
const context = new RegisterContext({ | ||
target: createFragment(), | ||
props: { | ||
Stories, | ||
repositories, | ||
}, | ||
}); | ||
context.$destroy(); | ||
} catch (e) { | ||
logger.error(`Error extracting stories ${e.toString()}`); | ||
} | ||
|
||
const { meta } = repositories; | ||
if (!meta) { | ||
logger.error('Missing <Meta/> tag'); | ||
return {}; | ||
} | ||
|
||
const { component: globalComponent } = meta; | ||
|
||
// collect templates id | ||
const templatesId = repositories.stories | ||
.filter((story) => story.isTemplate) | ||
.map((story) => story.name); | ||
|
||
// check for duplicate templates | ||
const duplicateTemplatesId = templatesId.filter( | ||
(item, index) => templatesId.indexOf(item) !== index | ||
); | ||
|
||
if (duplicateTemplatesId.length > 0) { | ||
logger.warn(`Found duplicates templates id :${duplicateTemplatesId}`); | ||
} | ||
|
||
const found = repositories.stories | ||
.filter((story) => !story.isTemplate) | ||
.map((story) => { | ||
const { name, template, component, source = false, ...props } = story; | ||
|
||
const unknowTemplate = template != null && templatesId.indexOf(template) < 0; | ||
|
||
const storyFn = (args) => { | ||
if (unknowTemplate) { | ||
throw new Error(`Story ${name} is referencing an unknown template ${template}`); | ||
} | ||
|
||
return { | ||
Component: RenderContext, | ||
props: { | ||
Stories, | ||
storyName: name, | ||
templateName: template, | ||
args, | ||
sourceComponent: component || globalComponent, | ||
}, | ||
}; | ||
}; | ||
|
||
storyFn.storyName = name; | ||
Object.entries(props).forEach(([k, v]) => { | ||
storyFn[k] = v; | ||
}); | ||
// inject sources into story | ||
const storySource = source === true ? sources[template ? `tpl:${template}` : name] : source; | ||
|
||
if (storySource) { | ||
storyFn.parameters = combineParameters(storyFn.parameters || {}, { | ||
docs: { source: { code: storySource } }, | ||
}); | ||
} | ||
|
||
return storyFn; | ||
}); | ||
|
||
return { | ||
default: meta, | ||
...found, | ||
}; | ||
}; |
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,36 @@ | ||
import parseStories from './parse-stories'; | ||
import TestStories from './components/__tests__/TestStories.svelte'; | ||
|
||
describe('parse-stories', () => { | ||
test('Extract Stories', () => { | ||
const stories = parseStories({ default: TestStories }, { 'tpl:tpl2': 'tpl2src' }); | ||
expect(stories.default).toMatchInlineSnapshot(` | ||
Object { | ||
"title": "Test", | ||
} | ||
`); | ||
|
||
expect(stories['0'].storyName).toBe('Story1'); | ||
expect(stories['0'].parameters).toMatchInlineSnapshot(`undefined`); | ||
expect(stories['1'].storyName).toBe('Story2'); | ||
expect(stories['1'].parameters).toMatchInlineSnapshot(` | ||
Object { | ||
"docs": Object { | ||
"source": Object { | ||
"code": "tpl2src", | ||
}, | ||
}, | ||
} | ||
`); | ||
expect(stories['2'].storyName).toBe('Story3'); | ||
expect(stories['2'].parameters).toMatchInlineSnapshot(` | ||
Object { | ||
"docs": Object { | ||
"source": Object { | ||
"code": "xyz", | ||
}, | ||
}, | ||
} | ||
`); | ||
}); | ||
}); |
Oops, something went wrong.