Skip to content

Commit

Permalink
Merge pull request #2 from storybookjs/fix/default-export-postprocessing
Browse files Browse the repository at this point in the history
Fix default export postprocessing
  • Loading branch information
shilman authored May 5, 2022
2 parents b9873f8 + facc4f7 commit bc01bdf
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"deepscan.enable": true
}
65 changes: 65 additions & 0 deletions src/mdx2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,71 @@ describe('mdx2', () => {
const mdxStoryNameToKey = { foo: 'foo' };
`);
});

it('full snapshot', () => {
const input = dedent`
# hello
<Meta title="foobar" />
world {2 + 1}
<Story name="foo">bar</Story>
`;
// @ts-ignore
expect(compileSync(input)).toMatchInlineSnapshot(`
/*@jsxRuntime automatic @jsxImportSource react*/
import { assertIsFn, AddContext } from "@storybook/addon-docs";
import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from "react/jsx-runtime";
function MDXContent(props = {}) {
const {wrapper: MDXLayout} = props.components || ({});
return MDXLayout ? _jsx(MDXLayout, Object.assign({}, props, {
children: _jsx(_createMdxContent, {})
})) : _createMdxContent();
function _createMdxContent() {
const _components = Object.assign({
h1: "h1",
p: "p"
}, props.components), {Meta, Story} = _components;
if (!Meta) _missingMdxReference("Meta", true);
if (!Story) _missingMdxReference("Story", true);
return _jsxs(_Fragment, {
children: [_jsx(_components.h1, {
children: "hello"
}), "\\n", _jsx(Meta, {
title: "foobar"
}), "\\n", _jsxs(_components.p, {
children: ["world ", 2 + 1]
}), "\\n", _jsx(Story, {
name: "foo",
children: "bar"
})]
});
}
}
function _missingMdxReference(id, component) {
throw new Error("Expected " + (component ? "component" : "object") + " \`" + id + "\` to be defined: you likely forgot to import, pass, or provide it.");
}
// =========
export const foo = () => (
"bar"
);
foo.storyName = 'foo';
foo.parameters = { storySource: { source: '\\"bar\\"' } };
const componentMeta = { title: 'foobar', includeStories: ["foo"], };
const mdxStoryNameToKey = {"foo":"foo"};
componentMeta.parameters = componentMeta.parameters || {};
componentMeta.parameters.docs = {
...(componentMeta.parameters.docs || {}),
page: () => <AddContext mdxStoryNameToKey={mdxStoryNameToKey} mdxComponentAnnotations={componentMeta}><MDXContent /></AddContext>,
};
export default componentMeta;
`);
});
});

describe('docs-mdx-compiler-plugin', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/mdx2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ export const plugin = (store: any) => (root: any) => {

export const postprocess = (code: string, extractedExports: string) => {
const lines = code.toString().trim().split('\n');
const last = lines.pop();

// /*@jsxRuntime automatic @jsxImportSource react*/
const first = lines.shift();

return [
first,
'import { assertIsFn, AddContext } from "@storybook/addon-docs";',
...lines,
last,
...lines.filter((line) => !line.match(/^export default/)),
SEPARATOR,
extractedExports,
].join('\n');
Expand Down

0 comments on commit bc01bdf

Please sign in to comment.