Skip to content

Commit

Permalink
Improve story template compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
squrious committed Mar 1, 2024
1 parent f0ba671 commit 393d022
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 137 deletions.
2 changes: 1 addition & 1 deletion storybook/dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion storybook/dist/index.mjs.map

Large diffs are not rendered by default.

126 changes: 63 additions & 63 deletions storybook/dist/preset.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion storybook/dist/preset.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion storybook/dist/preview.mjs.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 43 additions & 23 deletions storybook/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,71 @@ import { readCsf, StaticStory } from '@storybook/csf-tools';
import { Indexer, IndexInput } from '@storybook/types';
import { TwigTemplate } from './utils';
import crypto from 'crypto';
import dedent from 'ts-dedent';
import { logger } from '@storybook/node-logger';
import dedent from 'ts-dedent';

type TwigTemplateSource = string;
// type TwigTemplateSource = string;
type StoryId = StaticStory['id'];

type TwigStory = {
id: StoryId,
template: TwigTemplate,
hash: string
};

class TwigStoryIndex {
private templates: Map<string, TwigTemplateSource> = new Map<string, TwigTemplateSource>();
private storyIndex: Map<StoryId, string> = new Map<StoryId, string>();
private componentsInFiles = new Map<string, string[]>();
private storiesInFiles = new Map<string, Set<StoryId>>();

private stories: TwigStory[] = [];

register(id: StoryId, component: TwigTemplate, declaringFile: string) {
const hash = crypto.createHash('sha1').update(component.getSource()).digest('hex');
if (!this.templates.has(hash)) {
this.templates.set(hash, component.getSource());
}

if (!this.componentsInFiles.has(declaringFile)) {
this.componentsInFiles.set(declaringFile, []);
}
this.stories.push({
id,
hash,
template: component
});

// @ts-ignore
this.componentsInFiles.get(declaringFile).push(...component.getComponents());
const storiesInFile = this.storiesInFiles.get(declaringFile) ?? new Set<StoryId>();
storiesInFile.add(id);
this.storiesInFiles.set(declaringFile, storiesInFile);
}

unregister(fileName: string)
{
const stories = this.storiesInFiles.get(fileName);

if (!stories) return;

this.storyIndex.set(id, hash);
this.stories = this.stories.filter(story => !stories.has(story.id));
this.storiesInFiles.delete(fileName);
}

getMap() {
return Object.fromEntries(this.storyIndex);
getFiles() {
return Array.from(this.storiesInFiles.keys());
}

getTemplates() {
return this.templates;
return new Map(
this.stories.map(story => [story.hash, story.template.getSource()])
);
}

fileHasTemplates(fileName: string): boolean {
return this.componentsInFiles.has(fileName);
hasStories(fileName: string)
{
return this.storiesInFiles.has(fileName);
}

getComponentsInFile(fileName: string) {
return this.componentsInFiles.get(fileName);
getStories(fileName: string)
{
const ids = this.storiesInFiles.get(fileName);
return ids ? this.stories.filter(story => ids.has(story.id)) : [];
}
}

let twigIndexer: TwigStoryIndex;
export function getTwigStoriesIndexer() {
export const getTwigStoriesIndex = () => {
if (twigIndexer !== undefined) {
return twigIndexer;
}
Expand All @@ -68,8 +87,9 @@ export const createTwigCsfIndexer = (twigStoryIndex: TwigStoryIndex) => {
/* eslint-disable @typescript-eslint/no-var-requires */
const module = require(fileName);

const indexedStories: IndexInput[] = [];
twigStoryIndex.unregister(fileName); // Clear existing stories

const indexedStories: IndexInput[] = [];
csf.indexInputs.forEach((story) => {
try {
const template = module[story.exportName]?.template ?? module['default']?.template ?? undefined;
Expand Down
Loading

0 comments on commit 393d022

Please sign in to comment.