From d0ba0bb0fb98c311c8294b0e2077e2c88020144a Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Mon, 18 Apr 2022 13:35:50 -0500 Subject: [PATCH] WIP: stories add props to module metadata --- examples/stories/src/components/A.astro | 4 ++ packages/integrations/stories/package.json | 4 ++ packages/integrations/stories/src/index.ts | 30 +++++++++++++- packages/integrations/stories/stories.astro | 2 +- pnpm-lock.yaml | 46 +++++++++++++++++++++ 5 files changed, 84 insertions(+), 2 deletions(-) diff --git a/examples/stories/src/components/A.astro b/examples/stories/src/components/A.astro index d7200818f1..0ac937403c 100644 --- a/examples/stories/src/components/A.astro +++ b/examples/stories/src/components/A.astro @@ -1,4 +1,8 @@ --- +export interface Props { + message?: string; +} + const { message = "Hello Astro (A)" } = Astro.props; ---
diff --git a/packages/integrations/stories/package.json b/packages/integrations/stories/package.json index 1b6b688cb9..35971e5740 100644 --- a/packages/integrations/stories/package.json +++ b/packages/integrations/stories/package.json @@ -29,5 +29,9 @@ "devDependencies": { "astro": "workspace:*", "astro-scripts": "workspace:*" + }, + "dependencies": { + "@ts-morph/bootstrap": "^0.13.0", + "ts-morph": "^14.0.0" } } diff --git a/packages/integrations/stories/src/index.ts b/packages/integrations/stories/src/index.ts index 1cbd5736ca..ad2dfca415 100644 --- a/packages/integrations/stories/src/index.ts +++ b/packages/integrations/stories/src/index.ts @@ -2,12 +2,38 @@ import type { AstroConfig, AstroIntegration } from 'astro'; import { ssr, preload } from 'astro/core/render/dev'; import { renderComponent } from 'astro/internal/index.js'; import { fileURLToPath } from 'url'; +import { createProject } from '@ts-morph/bootstrap' + +async function getViteConfiguration() { + const project = await createProject(); + return { + plugins: [{ + name: '@astrojs/stories', + enforce: 'pre', + transform(code, id, opts) { + if (opts?.ssr !== true) return; + if (/\.[cm]?js(\?.*)?$/.test(id)) return null; + if (/\.css(\?.*)?$/.test(id)) return null; + if (/^plugin-vue/.test(id)) return null; + if (/stories\.astro/.test(id)) return null; + + // TODO: get Props and attach to `test` + return { meta: { ['@astrojs/stories']: { test: true } } }; + } + }] + }; +} export default function createPlugin(): AstroIntegration { let config: AstroConfig; return { name: '@astrojs/stories', hooks: { + 'astro:config:setup': async ({ updateConfig }) => { + return updateConfig({ + vite: await getViteConfiguration(), + }); + }, 'astro:config:done': ({ config: _config }) => { config = _config; }, @@ -21,9 +47,11 @@ export default function createPlugin(): AstroIntegration { let componentProps = url.searchParams.has('props') ? JSON.parse(url.searchParams.get('props')) : {}; const componentPath = rootRelativeComponentPath ? new URL(`.${rootRelativeComponentPath}`, config.root) : undefined; const pagePath = new URL('../stories.astro', import.meta.url); - const mod = componentPath ? (await server.ssrLoadModule(fileURLToPath(componentPath))) : undefined; + const mod = componentPath ? (await server.ssrLoadModule(fileURLToPath(componentPath), true)) : undefined; let component; if (mod) { + const { info: { meta: { ['@astrojs/stories']: metadata } } } = await server.moduleGraph.getModuleByUrl(fileURLToPath(componentPath)); + console.log({ metadata }); if (exportName && !(exportName in mod)) { console.log(`Unable to resolve export "${exportName}" from "${fileURLToPath(componentPath)}"`); } diff --git a/packages/integrations/stories/stories.astro b/packages/integrations/stories/stories.astro index caf8ef2ef2..2bcdf55f25 100644 --- a/packages/integrations/stories/stories.astro +++ b/packages/integrations/stories/stories.astro @@ -98,7 +98,7 @@ const activePath = new URL(Astro.request.url).searchParams.get('path');
- + {activePath ? :

Select a component to get started!

}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1ed5ab95e1..34620c6a2b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1380,8 +1380,13 @@ importers: packages/integrations/stories: specifiers: + '@ts-morph/bootstrap': ^0.13.0 astro: workspace:* astro-scripts: workspace:* + ts-morph: ^14.0.0 + dependencies: + '@ts-morph/bootstrap': 0.13.0 + ts-morph: 14.0.0 devDependencies: astro: link:../../astro astro-scripts: link:../../../scripts @@ -3908,6 +3913,21 @@ packages: engines: {node: '>= 6'} dev: true + /@ts-morph/bootstrap/0.13.0: + resolution: {integrity: sha512-pvzBE1ub6T+JZqqCzF+bHwM8LsT0T0tAwrYTaNOX9pKEJamzjQzg2owWcQTLeY3dXK0gIQpNKN/EDdY7tQmobQ==} + dependencies: + '@ts-morph/common': 0.13.0 + dev: false + + /@ts-morph/common/0.13.0: + resolution: {integrity: sha512-fEJ6j7Cu8yiWjA4UmybOBH9Efgb/64ZTWuvCF4KysGu4xz8ettfyaqFt8WZ1btCxXsGZJjZ2/3svOF6rL+UFdQ==} + dependencies: + fast-glob: 3.2.11 + minimatch: 5.0.1 + mkdirp: 1.0.4 + path-browserify: 1.0.1 + dev: false + /@types/acorn/4.0.6: resolution: {integrity: sha512-veQTnWP+1D/xbxVrPC3zHnCZRjSrKfhbMUlEA43iMZLu7EsnTtkJklIuwrCPbOi8YkvDQAiW05VQQFvvz9oieQ==} dependencies: @@ -4919,6 +4939,12 @@ packages: balanced-match: 1.0.2 concat-map: 0.0.1 + /brace-expansion/2.0.1: + resolution: {integrity: sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==} + dependencies: + balanced-match: 1.0.2 + dev: false + /braces/3.0.2: resolution: {integrity: sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==} engines: {node: '>=8'} @@ -5186,6 +5212,12 @@ packages: resolution: {integrity: sha1-2jCcwmPfFZlMaIypAheco8fNfH4=} engines: {node: '>=0.8'} + /code-block-writer/11.0.0: + resolution: {integrity: sha512-GEqWvEWWsOvER+g9keO4ohFoD3ymwyCnqY3hoTr7GZipYFwEhMHJw+TtV0rfgRhNImM6QWZGO2XYjlJVyYT62w==} + dependencies: + tslib: 2.3.1 + dev: false + /code-point-at/1.1.0: resolution: {integrity: sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=} engines: {node: '>=0.10.0'} @@ -7977,6 +8009,13 @@ packages: brace-expansion: 1.1.11 dev: true + /minimatch/5.0.1: + resolution: {integrity: sha512-nLDxIFRyhDblz3qMuq+SoRZED4+miJ/G+tdDrjkkkRnjAsBexeGpgjLEQ0blJy7rHhR2b93rhQY4SvyWu9v03g==} + engines: {node: '>=10'} + dependencies: + brace-expansion: 2.0.1 + dev: false + /minimist-options/4.1.0: resolution: {integrity: sha512-Q4r8ghd80yhO/0j1O3B2BjweX3fiHg9cdOwjJd2J76Q135c+NDxGCqdYKQ1SKBuFfgWbAUzBfvYjPUEeNgqN1A==} engines: {node: '>= 6'} @@ -9932,6 +9971,13 @@ packages: /trough/2.1.0: resolution: {integrity: sha512-AqTiAOLcj85xS7vQ8QkAV41hPDIJ71XJB4RCUrzo/1GM2CQwhkJGaf9Hgr7BOugMRpgGUrqRg/DrBDl4H40+8g==} + /ts-morph/14.0.0: + resolution: {integrity: sha512-tO8YQ1dP41fw8GVmeQAdNsD8roZi1JMqB7YwZrqU856DvmG5/710e41q2XauzTYrygH9XmMryaFeLo+kdCziyA==} + dependencies: + '@ts-morph/common': 0.13.0 + code-block-writer: 11.0.0 + dev: false + /tsconfig-resolver/3.0.1: resolution: {integrity: sha512-ZHqlstlQF449v8glscGRXzL6l2dZvASPCdXJRWG4gHEZlUVx2Jtmr+a2zeVG4LCsKhDXKRj5R3h0C/98UcVAQg==} dependencies: