From acdfd5994b86a6a4008b8469bcfaf05fceaa2ca0 Mon Sep 17 00:00:00 2001 From: Kamran Ayub Date: Thu, 9 Nov 2023 00:03:01 -0600 Subject: [PATCH] poc: storybook devServer + build --- site/.gitignore | 2 +- site/docusaurus.config.ts | 69 ++++++++++++++++++++-------- site/src/components/docs/Example.tsx | 7 ++- 3 files changed, 56 insertions(+), 22 deletions(-) diff --git a/site/.gitignore b/site/.gitignore index 22aea1bb5..d4c01209e 100644 --- a/site/.gitignore +++ b/site/.gitignore @@ -19,4 +19,4 @@ npm-debug.log* yarn-debug.log* yarn-error.log* -/static/examples \ No newline at end of file +/build-storybook \ No newline at end of file diff --git a/site/docusaurus.config.ts b/site/docusaurus.config.ts index 1237da8cc..b27bc3f47 100644 --- a/site/docusaurus.config.ts +++ b/site/docusaurus.config.ts @@ -1,8 +1,7 @@ -import { Config } from '@docusaurus/types'; +import { Config, Plugin } from '@docusaurus/types'; import { Options as ClassicPresetOptions, ThemeConfig as ClassicPresetThemeConfig } from '@docusaurus/preset-classic'; import { ReflectionKind } from 'typedoc'; -import { sync as readUpSync } from 'read-pkg-up'; -import { buildStaticStandalone } from '@storybook/core-server'; +import { build } from '@storybook/core-server'; import { cache } from '@storybook/core-common'; import path from 'path'; import webpack from 'webpack'; @@ -41,6 +40,8 @@ const config: Config = { locales: ['en'] }, + staticDirectories: ['static', 'build-storybook'], + presets: [ [ 'classic', @@ -79,21 +80,51 @@ const config: Config = { themes: ['@docusaurus/theme-live-codeblock'], plugins: [ - async function storybookPlugin(context, options) { - return { - name: 'storybook-plugin', - async loadContent() { - await buildStaticStandalone({ - configDir: path.join(__dirname, '..', '.storybook'), - outputDir: path.join(__dirname, 'static', 'examples'), - ignorePreview: false, - configType: 'PRODUCTION', - cache, - packageJson: readUpSync({ cwd: __dirname }).packageJson - }); - } - }; - }, + [ + async function storybookPlugin(context, options: { staticDir: string; storybookOptions?: any }) { + const isProd = process.env.NODE_ENV === 'production'; + let storybookDevServer: any = null; + + return { + name: 'storybook-plugin', + async postBuild() { + await build({ + mode: 'static', + configDir: path.join(__dirname, '..', '.storybook'), + outputDir: path.join(__dirname, context.outDir, 'examples'), + ignorePreview: false, + cache, + ...(options?.storybookOptions ?? {}) + }); + }, + async loadContent() { + if (isProd) { + return null; + } + + if (!storybookDevServer) { + storybookDevServer = await build({ + mode: 'dev', + ci: true, + configDir: path.join(__dirname, '..', '.storybook'), + outputDir: path.join(__dirname, options.staticDir, 'examples'), + ignorePreview: false, + cache, + ...(options?.storybookOptions ?? {}) + }); + } + + return { address: storybookDevServer.address } + }, + async contentLoaded({ content, actions }) { + actions.setGlobalData({ + address: content?.address + }); + } + } as Plugin<{ address?: string}>; + }, + { staticDir: 'build-storybook' } + ], async function excaliburPlugin(context, options) { return { name: 'excalibur-plugin', @@ -169,7 +200,7 @@ const config: Config = { label: 'Learn' }, { to: '/api', label: 'API', position: 'left' }, - { href: '/examples', label: 'Examples', position: 'left' }, + { href: '/examples', label: 'Examples', position: 'left', prependBaseUrlToHref: true }, { to: '/blog', label: 'Blog', position: 'left' }, { href: 'https://github.com/excaliburjs/Excalibur', diff --git a/site/src/components/docs/Example.tsx b/site/src/components/docs/Example.tsx index 692a36bbd..90bdf94c8 100644 --- a/site/src/components/docs/Example.tsx +++ b/site/src/components/docs/Example.tsx @@ -1,5 +1,6 @@ import React from 'react' import useBaseUrl from '@docusaurus/useBaseUrl' +import { usePluginData } from '@docusaurus/useGlobalData'; const FRAME_STYLE = { border: '1px solid #aaa', @@ -11,13 +12,15 @@ const FRAME_STYLE = { * @prop story The story ID in the URL to navigate to */ export default function Example({ story = '' }) { - const storyUrl = useBaseUrl(`/examples/?nav=0&path=/docs/${story}`); + const { address } = usePluginData('storybook-plugin') as { address?: string }; + const buildUrl = useBaseUrl(`/examples/?nav=0&path=/docs/${story}`); + const devUrl = `${address}?nav=0&path=/docs/${story}`; return (