Skip to content

Commit

Permalink
feat: hmr
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Feb 19, 2020
1 parent 15c4413 commit a783581
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
5 changes: 5 additions & 0 deletions core/loader/src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as fs from 'fs';
import * as path from 'path';
import { getOptions } from 'loader-utils';
import { parseCSF, parseMDX } from '@component-controls/instrument';
import { LoaderOptions } from './types';
Expand All @@ -16,6 +18,9 @@ module.exports.default = async function(source: string) {
break;
}
if (stories) {
const time = new Date();
const fileName = path.join(__dirname, 'story-store-data.js');
fs.utimesSync(fileName, time, time);
addStoriesKind(stories);
}
return source;
Expand Down
11 changes: 10 additions & 1 deletion core/loader/src/runtimeLoader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
/* eslint-disable mdx/no-unused-expressions */
import { createHash } from 'crypto';

module.exports = function(content: string) {
//@ts-ignore
this.cacheable && this.cacheable();
//@ts-ignore
const params = JSON.parse(this.query.slice(1));
content = content.replace(/__STORIES_HASH__/g, params.compilationHash);
return content;
const hash = createHash('md5')
.update(new Date().getTime().toString())
.digest('hex');

return `
module.exports.hash = "${hash}";
${content}
`;
};
8 changes: 2 additions & 6 deletions core/loader/src/story-store-data.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { StoryStore } from './types';

const injectedStories = '__STORIES_HASH__INJECTED_STORIES__';

type StringifyFunction = () => StoryStore | undefined;

const storyStore: StringifyFunction = () => {
const storyStore = () => {
if (injectedStories) {
const store = JSON.parse(injectedStories);
return store;
}
return undefined;
};

export default storyStore;
export default storyStore();
2 changes: 1 addition & 1 deletion integrations/storybook/src/config-legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ addDecorator(
storyId: string;
controls: ComponentControls;
}[] = [];
const injectedStoryStore = myStoryStore();
const injectedStoryStore = myStoryStore;
Object.keys(storyStore._data).forEach((id: string) => {
const story = storyStore._data[id];
const parameters = story.parameters || {};
Expand Down

0 comments on commit a783581

Please sign in to comment.