Skip to content

Commit

Permalink
StoryIndex: Don't sort story index in v6-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
shilman committed Nov 19, 2021
1 parent e90bbaf commit 43f9f14
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
1 change: 1 addition & 0 deletions lib/core-server/src/build-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export async function buildStaticStandalone(options: CLIOptions & LoadOptions &
await extractStoriesJson(path.join(options.outputDir, 'stories.json'), stories, {
...directories,
storiesV2Compatibility: !features?.breakingChangesV7 && !features?.storyStoreV7,
storyStoreV7: features?.storyStoreV7,
});
}

Expand Down
1 change: 1 addition & 0 deletions lib/core-server/src/utils/StoryIndexGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const options = {
configDir: path.join(__dirname, '__mockdata__'),
workingDir: path.join(__dirname, '__mockdata__'),
storiesV2Compatibility: false,
storyStoreV7: true,
};

describe('StoryIndexGenerator', () => {
Expand Down
30 changes: 15 additions & 15 deletions lib/core-server/src/utils/StoryIndexGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@ import { logger } from '@storybook/node-logger';
import { readCsfOrMdx, getStorySortParameter } from '@storybook/csf-tools';
import { ComponentTitle } from '@storybook/csf';

function sortExtractedStories(
stories: StoryIndex['stories'],
storySortParameter: any,
fileNameOrder: string[]
) {
const sortableStories = Object.values(stories);
sortStoriesV7(sortableStories, storySortParameter, fileNameOrder);
return sortableStories.reduce((acc, item) => {
acc[item.id] = item;
return acc;
}, {} as StoryIndex['stories']);
}

type SpecifierStoriesCache = Record<Path, StoryIndex['stories'] | false>;

export class StoryIndexGenerator {
Expand All @@ -47,6 +34,7 @@ export class StoryIndexGenerator {
workingDir: Path;
configDir: Path;
storiesV2Compatibility: boolean;
storyStoreV7: boolean;
}
) {
this.storyIndexEntries = new Map();
Expand Down Expand Up @@ -132,8 +120,20 @@ export class StoryIndexGenerator {
Object.assign(stories, subStories);
});

const storySortParameter = await this.getStorySortParameter();
return sortExtractedStories(stories, storySortParameter, this.storyFileNames());
const sortableStories = Object.values(stories);

// Skip sorting if we're in v6 mode because we don't have
// all the info we need here
if (this.options.storyStoreV7) {
const storySortParameter = await this.getStorySortParameter();
const fileNameOrder = this.storyFileNames();
sortStoriesV7(sortableStories, storySortParameter, fileNameOrder);
}

return sortableStories.reduce((acc, item) => {
acc[item.id] = item;
return acc;
}, {} as StoryIndex['stories']);
}

async getIndex() {
Expand Down
8 changes: 7 additions & 1 deletion lib/core-server/src/utils/stories-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ export const DEBOUNCE = 100;
export async function extractStoriesJson(
outputFile: string,
normalizedStories: NormalizedStoriesSpecifier[],
options: { configDir: string; workingDir: string; storiesV2Compatibility: boolean }
options: {
configDir: string;
workingDir: string;
storiesV2Compatibility: boolean;
storyStoreV7: boolean;
}
) {
const generator = new StoryIndexGenerator(normalizedStories, options);
await generator.initialize();
Expand All @@ -42,6 +47,7 @@ export async function useStoriesJson(
configDir: options.configDir,
workingDir,
storiesV2Compatibility: !features?.breakingChangesV7 && !features?.storyStoreV7,
storyStoreV7: features?.storyStoreV7,
});

// Wait until someone actually requests `stories.json` before we start generating/watching.
Expand Down

0 comments on commit 43f9f14

Please sign in to comment.