From ff7b7e176737a67fa73b655175e11f1433f3241b Mon Sep 17 00:00:00 2001 From: j3rem1e Date: Wed, 6 Sep 2023 21:12:16 +0200 Subject: [PATCH] Svelte: Always inject doc metadata --- .../svelte-vite/src/plugins/svelte-docgen.ts | 19 +++++++------ .../src/svelte-docgen-loader.ts | 27 ++++++++++--------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/code/frameworks/svelte-vite/src/plugins/svelte-docgen.ts b/code/frameworks/svelte-vite/src/plugins/svelte-docgen.ts index b9e8ac5fa762..7f6dc2314a5f 100644 --- a/code/frameworks/svelte-vite/src/plugins/svelte-docgen.ts +++ b/code/frameworks/svelte-vite/src/plugins/svelte-docgen.ts @@ -79,21 +79,24 @@ export function svelteDocgen(svelteOptions: Record = {}): 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 }), diff --git a/code/presets/svelte-webpack/src/svelte-docgen-loader.ts b/code/presets/svelte-webpack/src/svelte-docgen-loader.ts index 5267e73a317a..b8c6ae1a656d 100644 --- a/code/presets/svelte-webpack/src/svelte-docgen-loader.ts +++ b/code/presets/svelte-webpack/src/svelte-docgen-loader.ts @@ -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;