Skip to content

Commit 0f9c45b

Browse files
committed
feat(dev-server-storybook): add support for mdx
1 parent da360bc commit 0f9c45b

39 files changed

+802
-439
lines changed

docs/docs/dev-server/plugins/storybook.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Plugin for using Storybook with Web Dev Server using es modules.
66

77
## How it works
88

9-
This plugin uses an [opinionated build](https://github.com/modernweb-dev/storybook-prebuilt) of Storybook, making it possible to use it with native es modules.
9+
This plugin uses an [opinionated build](https://github.com/modernweb-dev/storybook-prebuilt) of Storybook, making it possible to use it with native es modules and buildless workflows.
1010

1111
This build installs a default set of addons:
1212

@@ -100,4 +100,35 @@ export default {
100100

101101
## Production build
102102

103-
Building the storybook for production is currently not supported. We plan to add this later.
103+
You can run a build on your storybook demo before shipping it to production. Regular storybook uses webpack, which might require considerable configuration to make it work for the buildless storybook. To keep the build simple we are using Rollup.
104+
105+
### Running a build
106+
107+
To run a production build, execute the `build-storybook` command. This takes a few parameters:
108+
109+
| name | type | description |
110+
| ---------- | ------ | ---------------------------------------------------------------------- |
111+
| config-dir | string | Directory to read storybook config from. defaults to `.storybook` |
112+
| output-dir | string | Directory to write the build output to. defaults to `storybook-static` |
113+
| type | string | Project type. Defaults to `web-components` |
114+
115+
### Customizing the build
116+
117+
You can customize the rollup config from your `main.js` config:
118+
119+
```js
120+
const pluginA = require('rollup-plugin-a');
121+
const pluginB = require('rollup-plugin-b');
122+
123+
module.exports = {
124+
rollupConfig(config) {
125+
// add a new plugin to the build
126+
config.plugins.push(pluginA());
127+
128+
// use unshift to make sure it runs before other plugins
129+
config.plugins.unshift(pluginB());
130+
131+
return config;
132+
},
133+
};
134+
```

packages/dev-server-storybook/demo/preact/preview.js renamed to packages/dev-server-storybook/demo/preact/.storybook/preview.js

File renamed without changes.

packages/dev-server-storybook/demo/preact/stories/Button.stories.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
title: 'Example/Button',
55
argTypes: {
66
backgroundColor: { control: 'color' },
7-
// onClick: { action: 'onClick' },
7+
onClick: { action: 'onClick' },
88
},
99
};
1010

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
module.exports = {
22
stories: ['../stories/**/*.stories.mdx', '../stories/**/*.stories.@(js|jsx|ts|tsx)'],
3+
rollupConfig(config) {
4+
// console.log('rollup config', config);
5+
},
36
};

packages/dev-server-storybook/demo/wc/preview.js renamed to packages/dev-server-storybook/demo/wc/.storybook/preview.js

File renamed without changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Meta, Story, Canvas, ArgsTable } from '@web/storybook-prebuilt/addon-docs/blocks.js';
2+
import { Button } from '../src/Button.js';
3+
4+
<Meta title="Docs/Button" component={'my-button'} />
5+
6+
# Button
7+
8+
This is a demo showing the button component
9+
10+
export const Template = args => Button(args);
11+
12+
<Canvas>
13+
<Story name="Unchecked" args={{ primary: true, label: 'Button' }}>
14+
{Template.bind({})}
15+
</Story>
16+
<Story name="Secondary" args={{ label: 'Button' }}>
17+
{Template.bind({})}
18+
</Story>
19+
<Story name="Large" args={{ size: 'large', label: 'Button' }}>
20+
{Template.bind({})}
21+
</Story>
22+
<Story name="Small" args={{ size: 'small', label: 'Button' }}>
23+
{Template.bind({})}
24+
</Story>
25+
</Canvas>
26+
27+
## Level 2 heading
28+
29+
- a list
30+
- of things
31+
- to be
32+
- listed
33+
34+
[A link somewhere](./foo.js)

packages/dev-server-storybook/package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"author": "modern-web",
1515
"homepage": "https://github.com/modernweb-dev/web/tree/master/packages/dev-server-storybook",
1616
"main": "dist/index.js",
17+
"bin": {
18+
"build-storybook": "dist/build/cli.js"
19+
},
1720
"engines": {
1821
"node": ">=10.0.0"
1922
},
@@ -44,9 +47,16 @@
4447
"esm"
4548
],
4649
"dependencies": {
50+
"@babel/core": "^7.12.3",
51+
"@babel/generator": "^7.12.5",
52+
"@babel/parser": "^7.12.5",
53+
"@babel/plugin-transform-react-jsx": "^7.12.5",
54+
"@mdx-js/mdx": "^1.6.19",
4755
"@web/dev-server-core": "^0.2.15",
48-
"@web/storybook-prebuilt": "^0.0.5",
56+
"@web/storybook-prebuilt": "^0.1.3",
4957
"globby": "^11.0.1",
58+
"js-string-escape": "^1.0.1",
59+
"lodash": "^4.17.20",
5060
"rollup": "^2.33.1"
5161
},
5262
"devDependencies": {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { RollupOptions } from 'rollup';
2-
import { MainJs } from '../shared/readStorybookConfig';
2+
import { MainJs } from '../shared/config/readStorybookConfig';
33

44
export interface RollupMainJs extends MainJs {
5-
rollupConfigDecorator?: (config: RollupOptions) => Promise<RollupOptions> | RollupOptions;
5+
rollupConfig?: (config: RollupOptions) => Promise<RollupOptions> | RollupOptions;
66
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { readStorybookConfig } from '../shared/config/readStorybookConfig';
2+
import { validatePluginConfig } from '../shared/config/validatePluginConfig';
3+
4+
import { createRollupConfig } from './rollup/createRollupConfig';
5+
import { buildAndWrite } from './rollup/buildAndWrite';
6+
import { MainJs } from '../shared/config/readStorybookConfig';
7+
import { createManagerHtml } from '../shared/html/createManagerHtml';
8+
import { createPreviewHtml } from '../shared/html/createPreviewHtml';
9+
import { findStories } from '../shared/stories/findStories';
10+
import { StorybookPluginConfig } from '../shared/config/StorybookPluginConfig';
11+
import { RollupMainJs } from './RollupMainJs';
12+
13+
interface BuildPreviewParams {
14+
pluginConfig: StorybookPluginConfig;
15+
outputDir: string;
16+
rootDir: string;
17+
mainJs: RollupMainJs;
18+
mainJsPath: string;
19+
previewJsPath: string;
20+
}
21+
22+
async function buildPreview(params: BuildPreviewParams) {
23+
const { pluginConfig, outputDir, rootDir, mainJs, mainJsPath, previewJsPath } = params;
24+
const { storyImports, storyFilePaths } = await findStories(rootDir, mainJsPath, mainJs.stories);
25+
const previewHtml = createPreviewHtml(rootDir, pluginConfig, previewJsPath, storyImports);
26+
27+
let config = createRollupConfig({
28+
outputDir,
29+
indexFilename: 'iframe.html',
30+
indexHtmlString: previewHtml,
31+
storyFilePaths,
32+
});
33+
34+
console.log('main.js', mainJs);
35+
if (mainJs.rollupConfig) {
36+
config = (await mainJs.rollupConfig(config)) ?? config;
37+
}
38+
39+
await buildAndWrite(config);
40+
}
41+
42+
interface BuildmanagerParams {
43+
outputDir: string;
44+
mainJs: MainJs;
45+
}
46+
47+
async function buildManager(params: BuildmanagerParams) {
48+
const managerHtml = createManagerHtml(params.mainJs);
49+
const config = createRollupConfig({
50+
outputDir: params.outputDir,
51+
indexFilename: 'index.html',
52+
indexHtmlString: managerHtml,
53+
});
54+
55+
await buildAndWrite(config);
56+
}
57+
58+
export interface BuildParams {
59+
type: 'web-components' | 'preact';
60+
outputDir: string;
61+
configDir: string;
62+
}
63+
64+
export async function build(params: BuildParams) {
65+
const { outputDir } = params;
66+
validatePluginConfig(params);
67+
68+
const { mainJs, mainJsPath, previewJsPath } = readStorybookConfig(params);
69+
await buildManager({ outputDir, mainJs });
70+
await buildPreview({
71+
pluginConfig: params,
72+
outputDir,
73+
rootDir: process.cwd(),
74+
mainJs,
75+
mainJsPath,
76+
previewJsPath,
77+
});
78+
}

packages/dev-server-storybook/src/build/buildManager.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)