-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19680 from storybookjs/shilman/csf-plugin
Addon-docs: Replace source-loader with csf-plugin
- Loading branch information
Showing
22 changed files
with
825 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# CSF Plugin | ||
|
||
The CSF plugin reads CSF files and enriches their content via static analysis. | ||
It supports Webpack, Vite, and other bundlers using [unplugin](https://github.com/unjs/unplugin). | ||
|
||
## Source snippets | ||
|
||
CSF plugin can add static source snippets to each story. For example: | ||
|
||
```js | ||
export const Basic = () => <Button />; | ||
``` | ||
|
||
Would be transformed to: | ||
|
||
```js | ||
export const Basic = () => <Button />; | ||
Basic.parameters = { | ||
storySource: { | ||
source: '() => <Button />', | ||
}, | ||
...Basic.parameters, | ||
}; | ||
``` | ||
|
||
This allows `@storybook/addon-docs` to display the static source snippet. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{ | ||
"name": "@storybook/csf-plugin", | ||
"version": "7.0.0-alpha.46", | ||
"description": "Enrich CSF files via static analysis", | ||
"keywords": [ | ||
"storybook" | ||
], | ||
"homepage": "https://github.com/storybookjs/storybook/tree/main/lib/csf-plugin", | ||
"bugs": { | ||
"url": "https://github.com/storybookjs/storybook/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/storybookjs/storybook.git", | ||
"directory": "lib/csf-plugin" | ||
}, | ||
"funding": { | ||
"type": "opencollective", | ||
"url": "https://opencollective.com/storybook" | ||
}, | ||
"license": "MIT", | ||
"sideEffects": false, | ||
"exports": { | ||
".": { | ||
"require": "./dist/cjs/index.js", | ||
"import": "./dist/esm/index.mjs", | ||
"types": "./dist/types/index.d.ts" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"main": "dist/cjs/index.js", | ||
"module": "dist/esm/index.js", | ||
"types": "dist/types/index.d.ts", | ||
"files": [ | ||
"dist/**/*", | ||
"README.md", | ||
"*.js", | ||
"*.d.ts" | ||
], | ||
"scripts": { | ||
"check": "../../../scripts/node_modules/.bin/tsc --noEmit", | ||
"prep": "node ../../../scripts/prepare.js" | ||
}, | ||
"dependencies": { | ||
"@storybook/csf-tools": "7.0.0-alpha.46", | ||
"unplugin": "^0.10.2" | ||
}, | ||
"devDependencies": { | ||
"typescript": "~4.6.3" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"bundler": { | ||
"entries": [ | ||
"./src/index.ts" | ||
] | ||
}, | ||
"gitHead": "3ef14366115c56c1d45c0359ff681cc47ed50532" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { createUnplugin } from 'unplugin'; | ||
import fs from 'fs/promises'; | ||
import { loadCsf, enrichCsf, formatCsf } from '@storybook/csf-tools'; | ||
import type { EnrichCsfOptions } from '@storybook/csf-tools'; | ||
|
||
export type CsfPluginOptions = EnrichCsfOptions; | ||
|
||
const STORIES_REGEX = /\.(story|stories)\.[tj]sx?$/; | ||
|
||
const logger = console; | ||
|
||
export const unplugin = createUnplugin<CsfPluginOptions>((options) => { | ||
return { | ||
name: 'unplugin-csf', | ||
enforce: 'pre', | ||
loadInclude(id) { | ||
return STORIES_REGEX.test(id); | ||
}, | ||
async load(fname) { | ||
const code = await fs.readFile(fname, 'utf-8'); | ||
try { | ||
const csf = loadCsf(code, { makeTitle: (userTitle) => userTitle || 'default' }).parse(); | ||
enrichCsf(csf); | ||
return formatCsf(csf); | ||
} catch (err: any) { | ||
logger.warn(err.message); | ||
return code; | ||
} | ||
}, | ||
}; | ||
}); | ||
|
||
export const { vite, rollup, webpack, esbuild } = unplugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"strict": true | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": [ | ||
"src/**/*.test.*", | ||
"src/**/tests/**/*", | ||
"src/**/__tests__/**/*", | ||
"src/**/*.stories.*", | ||
"src/**/*.mockdata.*", | ||
"src/**/__testfixtures__/**" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.