Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions code/core/src/core-server/utils/StoryIndexGenerator.test.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion code/core/src/core-server/utils/StoryIndexGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ export class StoryIndexGenerator {

getProjectTags(previewCode?: string) {
let projectTags = [] as Tag[];
const defaultTags = ['dev', 'test'];
const defaultTags = ['dev', 'test', 'manifest'];
if (previewCode) {
try {
const projectAnnotations = loadConfig(previewCode).parse();
Expand Down
25 changes: 25 additions & 0 deletions code/core/src/core-server/utils/index-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
"component-tag",
"story-tag",
"attached-mdx",
Expand All @@ -140,6 +141,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
"component-tag",
"story-tag",
"attached-mdx",
Expand All @@ -156,6 +158,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
"component-tag",
"story-tag",
],
Expand All @@ -170,6 +173,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
"autodocs",
],
"title": "B",
Expand All @@ -184,6 +188,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
"autodocs",
],
"title": "B",
Expand All @@ -199,6 +204,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
],
"title": "componentPath/extension",
"type": "story",
Expand All @@ -213,6 +219,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
],
"title": "componentPath/noExtension",
"type": "story",
Expand All @@ -227,6 +234,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
],
"title": "componentPath/package",
"type": "story",
Expand All @@ -239,6 +247,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
"autodocs",
],
"title": "D",
Expand All @@ -253,6 +262,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
"autodocs",
],
"title": "D",
Expand All @@ -266,6 +276,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
"unattached-mdx",
],
"title": "docs2/ComponentReference",
Expand All @@ -279,6 +290,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
"unattached-mdx",
],
"title": "docs2/NoTitle",
Expand All @@ -292,6 +304,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
"foo",
"bar",
"unattached-mdx",
Expand All @@ -307,6 +320,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
"unattached-mdx",
],
"title": "docs2/Yabbadabbadooo",
Expand All @@ -321,6 +335,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
"foobar",
],
"title": "Example/Button",
Expand All @@ -335,6 +350,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
],
"title": "first-nested/deeply/F",
"type": "story",
Expand All @@ -348,6 +364,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
],
"title": "first-nested/deeply/Features",
"type": "story",
Expand All @@ -361,6 +378,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
"play-fn",
],
"title": "first-nested/deeply/Features",
Expand All @@ -375,6 +393,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
],
"title": "first-nested/deeply/Features",
"type": "story",
Expand All @@ -388,6 +407,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
],
"title": "first-nested/deeply/Features",
"type": "story",
Expand All @@ -401,6 +421,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
"play-fn",
],
"title": "first-nested/deeply/Features",
Expand All @@ -414,6 +435,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
"autodocs",
],
"title": "H",
Expand All @@ -428,6 +450,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
"autodocs",
],
"title": "H",
Expand All @@ -442,6 +465,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
"component-tag",
],
"title": "nested/Button",
Expand All @@ -456,6 +480,7 @@ describe('registerIndexJsonRoute', () => {
"tags": [
"dev",
"test",
"manifest",
],
"title": "second-nested/G",
"type": "story",
Expand Down
67 changes: 66 additions & 1 deletion code/core/src/core-server/utils/manifests/manifests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ describe('manifests', () => {
let mockManifests: Manifests;

const setupMockPresets = () => {
mockGenerator = { getIndex: vi.fn().mockResolvedValue({} as StoryIndex) };
mockGenerator = {
getIndex: vi.fn().mockResolvedValue({
entries: {},
} as StoryIndex),
};
mockManifests = {};

return {
Expand Down Expand Up @@ -114,6 +118,67 @@ describe('manifests', () => {
expect(vi.mocked(logger).error).toHaveBeenCalledWith('Failed to generate manifests');
expect(vi.mocked(logger).error).toHaveBeenCalledWith(errorString);
});

it('should filter entries by manifest tag and pass manifestEntries to preset', async () => {
mockGenerator.getIndex.mockResolvedValue({
v: 5,
entries: {
'story-with-manifest': {
type: 'story',
subtype: 'story',
id: 'story-with-manifest',
name: 'Story',
title: 'Example',
importPath: './Example.stories.tsx',
tags: ['manifest', 'other'],
},
'story-without-manifest': {
type: 'story',
subtype: 'story',
id: 'story-without-manifest',
name: 'Other',
title: 'Other',
importPath: './Other.stories.tsx',
tags: ['other'],
},
'docs-entry': {
type: 'docs',
id: 'docs',
name: 'Docs',
title: 'Docs',
importPath: './Docs.mdx',
tags: ['manifest'],
storiesImports: [],
},
},
} as StoryIndex);

mockManifests = { custom: { data: 'value' } };

await writeManifests('/output', mockPresets);

expect(mockPresets.apply).toHaveBeenCalledWith(
'experimental_manifests',
undefined,
expect.objectContaining({
manifestEntries: expect.arrayContaining([
expect.objectContaining({ id: 'story-with-manifest' }),
]),
})
);

// Get the specific apply call to the experimental_manifests preset
const manifestsPresetCall = (mockPresets.apply as any).mock.calls.find(
(call: any) => call[0] === 'experimental_manifests'
);
// Should include both story and docs entries with manifest tag
expect(manifestsPresetCall[2].manifestEntries).toHaveLength(2);
const entryIds = manifestsPresetCall[2].manifestEntries.map((entry: any) => entry.id);
expect(entryIds).toContain('story-with-manifest');
expect(entryIds).toContain('docs');
// Should NOT include story without manifest tag
expect(entryIds).not.toContain('story-without-manifest');
});
});

describe('registerManifests', () => {
Expand Down
35 changes: 20 additions & 15 deletions code/core/src/core-server/utils/manifests/manifests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,24 @@ import invariant from 'tiny-invariant';

import { renderComponentsManifest } from './render-components-manifest';

async function getManifests(presets: Presets) {
const generator = await presets.apply('storyIndexGenerator');
invariant(generator, 'storyIndexGenerator must be configured');
const index = await generator.getIndex();
const manifestEntries = Object.values(index.entries).filter(
(entry) => entry.tags?.includes('manifest') ?? false
);

return (
(await presets.apply<Manifests>('experimental_manifests', undefined, {
manifestEntries,
})) ?? {}
);
}

export async function writeManifests(outputDir: string, presets: Presets) {
try {
const generator = await presets.apply('storyIndexGenerator');
invariant(generator, 'storyIndexGenerator must be configured');
const index = await generator.getIndex();
const manifests = await presets.apply('experimental_manifests', {}, { index });
const manifests = await getManifests(presets);
if (Object.keys(manifests).length === 0) {
return;
}
Expand All @@ -37,18 +49,10 @@ export async function writeManifests(outputDir: string, presets: Presets) {
}

export function registerManifests({ app, presets }: { app: Polka; presets: Presets }) {
async function getManifest(manifestName: string) {
const generator = await presets.apply('storyIndexGenerator');
invariant(generator, 'storyIndexGenerator must be configured');
const index = await generator.getIndex();
const manifests = ((await presets.apply('experimental_manifests', {}, { index })) ??
{}) as Manifests;
return manifests[manifestName];
}

app.get('/manifests/:name.json', async (req, res) => {
try {
const manifest = await getManifest(req.params.name);
const manifests = await getManifests(presets);
const manifest = manifests[req.params.name];

if (manifest) {
res.setHeader('Content-Type', 'application/json');
Expand All @@ -66,7 +70,8 @@ export function registerManifests({ app, presets }: { app: Polka; presets: Prese

app.get('/manifests/components.html', async (req, res) => {
try {
const manifest = (await getManifest('components')) as ComponentsManifest | undefined;
const manifests = await getManifests(presets);
const manifest = manifests.components;

if (!manifest) {
res.statusCode = 404;
Expand Down
Loading