Skip to content

Commit

Permalink
Svelte: Always inject doc metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
j3rem1e committed Sep 9, 2023
1 parent d627022 commit ff7b7e1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
19 changes: 11 additions & 8 deletions code/frameworks/svelte-vite/src/plugins/svelte-docgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,24 @@ export function svelteDocgen(svelteOptions: Record<string, any> = {}): PluginOpt

const s = new MagicString(src);

let componentDoc: any;
try {
const componentDoc = await svelteDoc.parse(options);
// get filename for source content
const file = path.basename(resource);

componentDoc.name = path.basename(file);

const componentName = getNameFromFilename(resource);
s.append(`;${componentName}.__docgen = ${JSON.stringify(componentDoc)}`);
componentDoc = await svelteDoc.parse(options);
} catch (error: any) {
componentDoc = { keywords: [], data: [] };
if (logDocgen) {
logger.error(error);
}
}

// get filename for source content
const file = path.basename(resource);

componentDoc.name = path.basename(file);

const componentName = getNameFromFilename(resource);
s.append(`;${componentName}.__docgen = ${JSON.stringify(componentDoc)}`);

return {
code: s.toString(),
map: s.generateMap({ hires: true, source: id }),
Expand Down
27 changes: 15 additions & 12 deletions code/presets/svelte-webpack/src/svelte-docgen-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,30 @@ export default async function svelteDocgen(this: any, source: string) {

let docgen = '';

let componentDoc: any;
try {
// FIXME
// @ts-expect-error (Converted from ts-ignore)
const componentDoc = await svelteDoc.parse(options);
componentDoc = await svelteDoc.parse(options);
} catch (error) {
componentDoc = { keywords: [], data: [] };
if (logDocgen) {
logger.error(error as any);
}
}

// get filename for source content
const file = path.basename(resource);
// get filename for source content
const file = path.basename(resource);

// populate filename in docgen
componentDoc.name = path.basename(file);
// populate filename in docgen
componentDoc.name = path.basename(file);

const componentName = getNameFromFilename(resource);
const componentName = getNameFromFilename(resource);

docgen = dedent`
docgen = dedent`
${componentName}.__docgen = ${JSON.stringify(componentDoc)};
`;
} catch (error) {
if (logDocgen) {
logger.error(error as any);
}
}

// inject __docgen prop in svelte component
const output = source + docgen;

Expand Down

0 comments on commit ff7b7e1

Please sign in to comment.