Skip to content

Commit

Permalink
feat: added moduleid logic for __webpack_require__
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Mar 15, 2020
1 parent d11cbe6 commit 7c97c9f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
6 changes: 3 additions & 3 deletions core/loader/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ module.exports.default = async function(source: string) {

const store: StoriesStore = await parseStories(source, filePath, options);
if (store) {
const relPath = path.relative(context.rootContext, context.resourcePath);
const moduleId = relPath.startsWith('.') ? relPath : `./${relPath}`;
const time = new Date();
const fileName = path.join(__dirname, 'story-store-data.js');
fs.utimesSync(fileName, time, time);
Expand All @@ -28,9 +30,7 @@ module.exports.default = async function(source: string) {
...acc,
[key]: {
...store.kinds[key],
request: require.resolve(
path.relative(__dirname, context.resourcePath),
),
moduleId: moduleId,
},
}),
{},
Expand Down
41 changes: 35 additions & 6 deletions core/loader/src/story-store-data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/camelcase */
import { StoriesStore, Story } from '@component-controls/specification';
import deepMerge from 'deepmerge';
import { toId, storyNameFromExport } from '@storybook/csf';
const injectedStories = '__STORIES_HASH__INJECTED_STORIES__';

Expand All @@ -22,19 +24,46 @@ const loadStoryStore = (): StoriesStore | undefined => {
if (Object.keys(store.kinds).length > 0) {
Object.keys(store.kinds).forEach(kindName => {
const kind = store.kinds[kindName];
/*
if (kind.request) {

if (kind.moduleId) {
try {
const exports = require(kind.request);
console.log(exports);
// './src/stories/smart-prop-type.stories.js'
const exports = __webpack_require__(kind.moduleId);
Object.keys(exports).forEach(key => {
const exported = exports[key];
if (key === 'default') {
const { storySource, ...rest } = exported;
Object.assign(kind, rest);
} else {
const story = store.stories[key];
if (story) {
story.renderFn = exported;
if (exported.story) {
Object.assign(story, exported.story);
}
}
}
});
} catch (e) {
console.log(e);
console.error(`unable to load module ${kind.moduleId}`);
}
}
*/
globalStore.kinds[kindName] = kind;
Object.keys(store.stories).forEach(storyName => {
const story: Story = store.stories[storyName];
const {
title,
stories,
source,
component,
fileName,
repository,
components,
excludeStories,
includeStories,
...rest
} = kind;
Object.assign(story, deepMerge(rest, story));
if (kind.title && story.name) {
const id = toId(kind.title, storyNameFromExport(story.name));
if (!kind.stories) {
Expand Down
6 changes: 6 additions & 0 deletions core/specification/src/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,16 @@ export interface Story {
* title of the file/group of stories
*/
kind?: string;

/**
* render function for the story
*/
renderFn?: (controls: { [key: string]: any; context: any }) => any;
/**
* arguments pass to a CSF story
* eg `export const story = props => <Story {...props} />;`
*/

arguments?: StoryArguments;
/**
* configuration parameters passed to the story - either CSF or MDX
Expand Down
1 change: 1 addition & 0 deletions ui/blocks/src/BlocksContext/BlockContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export const useControlsContext = ({
? myStoryStore.components[kind.components[componentName]]
: undefined;
const sbStory = (storyStore && storyStore.fromId(previewId)) || {};
console.log(kind, story);
return {
id: previewId,
api,
Expand Down

0 comments on commit 7c97c9f

Please sign in to comment.