Server: Add json indexer to preset

Server: Add json indexer to preset

Co-authored-by: Donnie Flood <donnie@floodfx.com>
This commit is contained in:
Michael Shilman 2023-05-09 00:27:23 +08:00
parent 0c12ac28ee
commit f8cb089d65
4 changed files with 47 additions and 1 deletions

View File

@ -31,6 +31,11 @@
"require": "./dist/config.js",
"import": "./dist/config.mjs"
},
"./preset": {
"types": "./dist/preset.d.ts",
"require": "./dist/preset.js",
"import": "./dist/preset.mjs"
},
"./package.json": "./package.json"
},
"main": "dist/index.js",
@ -49,9 +54,12 @@
},
"dependencies": {
"@storybook/core-client": "7.1.0-alpha.12",
"@storybook/csf": "^0.1.0",
"@storybook/global": "^5.0.0",
"@storybook/preview-api": "7.1.0-alpha.12",
"@storybook/types": "7.1.0-alpha.12",
"@types/fs-extra": "^11.0.1",
"fs-extra": "^11.1.0",
"ts-dedent": "^2.0.0"
},
"devDependencies": {
@ -66,7 +74,8 @@
"bundler": {
"entries": [
"./src/index.ts",
"./src/config.ts"
"./src/config.ts",
"./src/preset.ts"
],
"platform": "browser"
},

View File

@ -0,0 +1 @@
module.exports = require('./dist/preset');

View File

@ -0,0 +1,33 @@
import fs from 'fs-extra';
import { toId } from '@storybook/csf';
import type { StaticMeta } from '@storybook/csf-tools';
import type { IndexerOptions, IndexedStory, StoryIndexer } from '@storybook/types';
export const storyIndexers = (indexers: StoryIndexer[] | null) => {
const jsonIndexer = async (fileName: string, opts: IndexerOptions) => {
const json = await fs.readJson(fileName, 'utf-8');
const meta: StaticMeta = {
title: json.title,
};
const stories: IndexedStory[] = json.stories.map((story: any) => {
const id = toId(meta.title, story.name);
const { name } = story;
const indexedStory: IndexedStory = {
id,
name,
};
return indexedStory;
});
return {
meta,
stories,
};
};
return [
{
test: /(stories|story)\.json$/,
indexer: jsonIndexer,
},
...(indexers || []),
];
};

View File

@ -7051,9 +7051,12 @@ __metadata:
resolution: "@storybook/server@workspace:renderers/server"
dependencies:
"@storybook/core-client": 7.1.0-alpha.12
"@storybook/csf": ^0.1.0
"@storybook/global": ^5.0.0
"@storybook/preview-api": 7.1.0-alpha.12
"@storybook/types": 7.1.0-alpha.12
"@types/fs-extra": ^11.0.1
fs-extra: ^11.1.0
ts-dedent: ^2.0.0
typescript: ~4.9.3
languageName: unknown