Skip to content

Commit

Permalink
poc: storybook devServer + build
Browse files Browse the repository at this point in the history
  • Loading branch information
kamranayub committed Nov 9, 2023
1 parent f4d7291 commit acdfd59
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
2 changes: 1 addition & 1 deletion site/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*

/static/examples
/build-storybook
69 changes: 50 additions & 19 deletions site/docusaurus.config.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -41,6 +40,8 @@ const config: Config = {
locales: ['en']
},

staticDirectories: ['static', 'build-storybook'],

presets: [
[
'classic',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
7 changes: 5 additions & 2 deletions site/src/components/docs/Example.tsx
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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 (
<iframe
title="Excalibur Storybook Example"
allowFullScreen
src={storyUrl}
src={address ? devUrl : buildUrl}
frameBorder={0}
width="100%"
height="600"
Expand Down

0 comments on commit acdfd59

Please sign in to comment.