Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds MetaJson types #568

Merged
merged 5 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions .changeset/dirty-zoos-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ladle/react": patch
---

Exports meta.json types from library
2 changes: 1 addition & 1 deletion packages/ladle/lib/app/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import * as msw from "msw";
export { msw };

export type { UserConfig } from "../shared/types";
export type { UserConfig, MetaJson, MetaJsonStory } from "../shared/types";
export { useMDXComponents } from "@mdx-js/react";
export const Story = (props: any) => props.children;
export const Meta = (props: any) => props.children;
Expand Down
6 changes: 4 additions & 2 deletions packages/ladle/lib/cli/vite-plugin/generate/get-meta-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { storyIdToMeta } from "../naming-utils.js";

/**
* @param entryData {import('../../../shared/types').EntryData}
* @returns {import('../../../shared/types').MetaJson}
*/
export const getMetaJson = (entryData) => {
/** @type {string[]} */
Expand All @@ -20,14 +21,15 @@ export const getMetaJson = (entryData) => {
);
storyParams = { ...storyParams, ...entryData[entry].storyParams };
});

/** @type {import('../../../shared/types').MetaJson} */
const result = {
about: {
homepage: "https://www.ladle.dev",
github: "https://github.com/tajo/ladle",
version: 1,
},
stories:
/** @type {{[key: string]: {name: string; levels: string[]; meta: any, locStart: number; locEnd: number;}}} */ ({}),
stories: {},
};
storyIds.forEach((storyId) => {
result.stories[storyId] = {
Expand Down
17 changes: 17 additions & 0 deletions packages/ladle/lib/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,20 @@ export type GetUserViteConfig = {
export type EntryData = {
[key: string]: ParsedStoriesResult;
};

export type MetaJson<M = any> = {
about: {
homepage: string;
github: string;
version: number;
};
stories: { [key: string]: MetaJsonStory<M> };
};

export type MetaJsonStory<M = any> = {
name: string;
levels: string[];
meta: M;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be made stricter, an extension of this type https://github.com/tajo/ladle/blob/main/packages/ladle/lib/app/exports.ts#L138

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is what I wanted to do, but it's a bit awkward since Meta is defined in the app exports but these types are in the shared types.

I would just move the Meta interface over into the shared types and re-export it but I get a conflict with this Meta const:
image

I think I have something that should work though, so let's see what you think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tajo How about this implementation?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

locStart: number;
locEnd: number;
};
Loading