generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 32
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 #72 from storybookjs/vite-support
Add vite support
- Loading branch information
Showing
6 changed files
with
314 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ['@storybook/eslint-config-storybook'], | ||
rules: { | ||
'@typescript-eslint/dot-notation': 'off', | ||
'@typescript-eslint/no-implied-eval': 'off', | ||
'@typescript-eslint/no-throw-literal': 'off', | ||
'@typescript-eslint/return-await': 'off', | ||
}, | ||
overrides: [], | ||
}; |
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,59 @@ | ||
import { readFileSync } from 'fs'; | ||
import * as svelte from 'svelte/compiler'; | ||
import MagicString from 'magic-string'; | ||
import { createFilter } from 'vite'; | ||
import { getNameFromFilename } from '../parser/svelte-stories-loader'; | ||
import { extractStories } from '../parser/extract-stories'; | ||
|
||
const parser = require.resolve('../../esm/parser/collect-stories').replace(/[/\\]/g, '/'); | ||
|
||
export default function csfPlugin(svelteOptions) { | ||
const include = /\.stories\.svelte$/; | ||
const filter = createFilter(include); | ||
|
||
return { | ||
name: 'storybook:addon-svelte-csf-plugin', | ||
enforce: 'post', | ||
async transform(code, id) { | ||
if (!filter(id)) return undefined; | ||
|
||
const s = new MagicString(code); | ||
const component = getNameFromFilename(id); | ||
let source = readFileSync(id).toString(); | ||
if (svelteOptions && svelteOptions.preprocess) { | ||
source = (await svelte.preprocess(source, svelteOptions.preprocess, { filename: id })).code; | ||
} | ||
const all = extractStories(source); | ||
const { stories } = all; | ||
const storyDef = Object.entries(stories) | ||
.filter(([, def]) => !def.template) | ||
.map( | ||
([storyId]) => | ||
`export const ${storyId} = __storiesMetaData.stories[${JSON.stringify(storyId)}];` | ||
) | ||
.join('\n'); | ||
|
||
s.replace('export default', '// export default'); | ||
|
||
const namedExportsOrder = Object.entries(stories) | ||
.filter(([, def]) => !def.template) | ||
.map(([storyId]) => storyId); | ||
|
||
const output = [ | ||
'', | ||
`import parser from '${parser}';`, | ||
`const __storiesMetaData = parser(${component}, ${JSON.stringify(all)});`, | ||
'export default __storiesMetaData.meta;', | ||
`export const __namedExportsOrder = ${JSON.stringify(namedExportsOrder)};`, | ||
storyDef, | ||
].join('\n'); | ||
|
||
s.append(output); | ||
|
||
return { | ||
code: s.toString(), | ||
map: s.generateMap({ hires: true, source: id }), | ||
}; | ||
}, | ||
}; | ||
} |
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.