diff --git a/addons/docs/src/frameworks/svelte/preset.ts b/addons/docs/src/frameworks/svelte/preset.ts index 9feb2647f424..a4862b8bb585 100644 --- a/addons/docs/src/frameworks/svelte/preset.ts +++ b/addons/docs/src/frameworks/svelte/preset.ts @@ -5,6 +5,7 @@ import { Configuration } from 'webpack'; export function webpackFinal(webpackConfig: Configuration, options: any = {}) { webpackConfig.module.rules.push({ test: /\.svelte$/, + exclude: /\.stories\.svelte$/, loader: path.resolve(`${__dirname}/svelte-docgen-loader`), enforce: 'post', }); diff --git a/app/svelte/jest-transform.js b/app/svelte/jest-transform.js new file mode 100644 index 000000000000..c790c624162a --- /dev/null +++ b/app/svelte/jest-transform.js @@ -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; diff --git a/app/svelte/package.json b/app/svelte/package.json index 1716ee1c7bdc..3be3f930e54d 100644 --- a/app/svelte/package.json +++ b/app/svelte/package.json @@ -42,6 +42,8 @@ }, "dependencies": { "@storybook/addons": "6.2.0-alpha.19", + "@storybook/client-api": "6.2.0-alpha.19", + "@storybook/client-logger": "6.2.0-alpha.19", "@storybook/core": "6.2.0-alpha.19", "core-js": "^3.8.2", "global": "^4.4.0", diff --git a/app/svelte/src/client/components/Meta.svelte b/app/svelte/src/client/components/Meta.svelte new file mode 100644 index 000000000000..1f4060bd8fab --- /dev/null +++ b/app/svelte/src/client/components/Meta.svelte @@ -0,0 +1,6 @@ + + diff --git a/app/svelte/src/client/components/RegisterContext.svelte b/app/svelte/src/client/components/RegisterContext.svelte new file mode 100644 index 000000000000..12f446ba3695 --- /dev/null +++ b/app/svelte/src/client/components/RegisterContext.svelte @@ -0,0 +1,11 @@ + + + diff --git a/app/svelte/src/client/components/RenderContext.svelte b/app/svelte/src/client/components/RenderContext.svelte new file mode 100644 index 000000000000..16c665f0e03d --- /dev/null +++ b/app/svelte/src/client/components/RenderContext.svelte @@ -0,0 +1,13 @@ + + + \ No newline at end of file diff --git a/app/svelte/src/client/components/Story.svelte b/app/svelte/src/client/components/Story.svelte new file mode 100644 index 000000000000..033054a0684f --- /dev/null +++ b/app/svelte/src/client/components/Story.svelte @@ -0,0 +1,24 @@ + + +{#if render} + +{/if} diff --git a/app/svelte/src/client/components/Template.svelte b/app/svelte/src/client/components/Template.svelte new file mode 100644 index 000000000000..83ef90345c39 --- /dev/null +++ b/app/svelte/src/client/components/Template.svelte @@ -0,0 +1,16 @@ + + +{#if render} + +{/if} + \ No newline at end of file diff --git a/app/svelte/src/client/components/__tests__/TestStories.svelte b/app/svelte/src/client/components/__tests__/TestStories.svelte new file mode 100644 index 000000000000..290283883809 --- /dev/null +++ b/app/svelte/src/client/components/__tests__/TestStories.svelte @@ -0,0 +1,21 @@ + + + + + + + + + + + +
story3
+
diff --git a/app/svelte/src/client/components/context.ts b/app/svelte/src/client/components/context.ts new file mode 100644 index 000000000000..769fb1d45482 --- /dev/null +++ b/app/svelte/src/client/components/context.ts @@ -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); +} diff --git a/app/svelte/src/client/index.ts b/app/svelte/src/client/index.ts index 8034a9d6433d..c25165c7fc28 100644 --- a/app/svelte/src/client/index.ts +++ b/app/svelte/src/client/index.ts @@ -9,6 +9,11 @@ export { raw, } from './preview'; +export { default as Meta } from './components/Meta.svelte'; +export { default as Story } from './components/Story.svelte'; +export { default as Template } from './components/Template.svelte'; +export { useContext } from './components/context'; + if (module && module.hot && module.hot.decline) { module.hot.decline(); } diff --git a/app/svelte/src/client/parse-stories.js b/app/svelte/src/client/parse-stories.js new file mode 100644 index 000000000000..b9051ecc3b84 --- /dev/null +++ b/app/svelte/src/client/parse-stories.js @@ -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 and