Skip to content

Commit

Permalink
fix: import mdx stories by id and mdx exports refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Apr 14, 2020
1 parent 8174079 commit af8abf0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 42 deletions.
2 changes: 1 addition & 1 deletion core/instrument/src/babel/mdx-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const extractMDXStories = (
path.scope,
path,
);
store.stories[name] = story;
store.stories[id] = story;
if (exports) {
store.exports[id] = exports;
}
Expand Down
33 changes: 4 additions & 29 deletions core/instrument/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { extractMDXStories } from './babel/mdx-stories';
import { removeMDXAttributes } from './babel/remove-mdx-attributes';
import { extractStoreComponent } from './babel/extract-component';
import { packageInfo } from './misc/package-info';
import { mdxFunctionExport, mdxPropertiesExport } from './misc/mdx-exports';
import { extractStoryExports } from './misc/mdx-exports';
import {
InstrumentOptions,
ParserOptions,
Expand Down Expand Up @@ -199,35 +199,10 @@ export const parseStories = async (
mergedOptions,
);
const { stories, kinds, components, exports } = store;
const exportsSource = extractStoryExports(exports);
let transformed = source;
if (transformMDX && exports) {
let defaultExportCode = '';
if (exports.default && exports.default.story) {
const expCode = mdxPropertiesExport(exports.default);
if (expCode) {
defaultExportCode = `export default { ${expCode} };`;
}
}

let storiesExports: string[] = [];
const expStories = Object.keys(exports).filter(id => id !== 'default');
if (expStories.length) {
for (const exportStory of expStories) {
const expFn = mdxFunctionExport(exportStory, exports[exportStory]);
if (expFn) {
storiesExports.push(expFn);
}
const expCode = mdxPropertiesExport(exports[exportStory]);
if (expCode) {
storiesExports.push(`${exportStory}.story = {
${expCode}
}`);
}
}
}
transformed = `${renderer}\n${code}\n${defaultExportCode}\n${storiesExports.join(
'\n',
)}`;
if (transformMDX && exportsSource) {
transformed = `${renderer}\n${code}\n${exportsSource}`;
}
return {
transformed,
Expand Down
42 changes: 37 additions & 5 deletions core/instrument/src/misc/mdx-exports.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ExportType } from '../types';
import { ExportType, ExportTypes } from '../types';

export const mdxPropertiesExport = (
exportType: ExportType,
): string | undefined => {
const mdxPropertiesExport = (exportType: ExportType): string | undefined => {
return exportType && exportType.story
? `${Object.keys(exportType.story)
//@ts-ignore
Expand All @@ -11,11 +9,45 @@ export const mdxPropertiesExport = (
: undefined;
};

export const mdxFunctionExport = (
const mdxFunctionExport = (
name: string,
exportType: ExportType,
): string | undefined => {
return exportType && exportType.render
? `export const ${name} = ${exportType.render}`
: undefined;
};

export const extractStoryExports = (exports?: ExportTypes): string => {
if (exports) {
const exportNames = Object.keys(exports);
if (exportNames.length) {
let defaultExportCode = '';
if (exports.default && exports.default.story) {
const expCode = mdxPropertiesExport(exports.default);
if (expCode) {
defaultExportCode = `export default { ${expCode} };`;
}
}

let storiesExports: string[] = [];
const expStories = Object.keys(exports).filter(id => id !== 'default');
if (expStories.length) {
for (const exportStory of expStories) {
const expFn = mdxFunctionExport(exportStory, exports[exportStory]);
if (expFn) {
storiesExports.push(expFn);
}
const expCode = mdxPropertiesExport(exports[exportStory]);
if (expCode) {
storiesExports.push(`${exportStory}.story = {
${expCode}
}`);
}
}
}
return `${defaultExportCode}\n${storiesExports.join('\n')}`;
}
}
return 'result';
};
7 changes: 4 additions & 3 deletions core/instrument/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,9 @@ export interface ExportType {
[key: string]: string;
};
}
export interface ExportTypes {
[key: string]: ExportType;
}
export type ParseStorieReturnType = {
exports?: {
[key: string]: ExportType;
};
exports?: ExportTypes;
} & StoriesStore;
8 changes: 4 additions & 4 deletions examples/storybook-6-no-docs/.storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ module.exports = {
},
],
stories: [
'../../../ui/editors/src/**/*.stories.(js|tsx|mdx)',
'../../../ui/components/src/**/*.stories.(js|tsx|mdx)',
'../../../ui/blocks/src/**/*.stories.(js|tsx|mdx)',
'../../stories/src/**/*.stories.(js|tsx|mdx)',
//'../../../ui/editors/src/**/*.stories.(js|tsx|mdx)',
//'../../../ui/components/src/**/*.stories.(js|tsx|mdx)',
//'../../../ui/blocks/src/**/*.stories.(js|tsx|mdx)',
'../../stories/src/stories/smart-controls.stories.mdx',
],
webpackFinal: async (config, { configType }) => {
return {
Expand Down

0 comments on commit af8abf0

Please sign in to comment.