mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-05 16:11:33 +08:00
feat(svelte): adding svelte cli generator
This commit is contained in:
parent
638cbd5fcb
commit
f215a50cf0
43
lib/cli/generators/SVELTE/index.js
Normal file
43
lib/cli/generators/SVELTE/index.js
Normal file
@ -0,0 +1,43 @@
|
||||
import path from 'path';
|
||||
import mergeDirs from 'merge-dirs';
|
||||
import { getVersions, getPackageJson, writePackageJson, installBabel } from '../../lib/helpers';
|
||||
|
||||
export default async npmOptions => {
|
||||
const [
|
||||
storybookVersion,
|
||||
actionsVersion,
|
||||
linksVersion,
|
||||
addonsVersion,
|
||||
svelte,
|
||||
svelteLoader,
|
||||
] = await getVersions(
|
||||
npmOptions,
|
||||
'@storybook/svelte',
|
||||
'@storybook/addon-actions',
|
||||
'@storybook/addon-links',
|
||||
'@storybook/addons',
|
||||
'svelte',
|
||||
'svelte-loader'
|
||||
);
|
||||
|
||||
mergeDirs(path.resolve(__dirname, 'template/'), '.', 'overwrite');
|
||||
|
||||
const packageJson = getPackageJson();
|
||||
|
||||
packageJson.dependencies = packageJson.dependencies || {};
|
||||
packageJson.devDependencies = packageJson.devDependencies || {};
|
||||
packageJson.devDependencies['@storybook/svelte'] = storybookVersion;
|
||||
packageJson.devDependencies['@storybook/addon-actions'] = actionsVersion;
|
||||
packageJson.devDependencies['@storybook/addon-links'] = linksVersion;
|
||||
packageJson.devDependencies['@storybook/addons'] = addonsVersion;
|
||||
packageJson.devDependencies.svelte = svelte;
|
||||
packageJson.devDependencies['svelte-loader'] = svelteLoader;
|
||||
|
||||
await installBabel(npmOptions, packageJson);
|
||||
|
||||
packageJson.scripts = packageJson.scripts || {};
|
||||
packageJson.scripts.storybook = 'start-storybook -p 6006';
|
||||
packageJson.scripts['build-storybook'] = 'build-storybook';
|
||||
|
||||
writePackageJson(packageJson);
|
||||
};
|
2
lib/cli/generators/SVELTE/template/.storybook/addons.js
Normal file
2
lib/cli/generators/SVELTE/template/.storybook/addons.js
Normal file
@ -0,0 +1,2 @@
|
||||
import '@storybook/addon-actions/register';
|
||||
import '@storybook/addon-links/register';
|
9
lib/cli/generators/SVELTE/template/.storybook/config.js
Normal file
9
lib/cli/generators/SVELTE/template/.storybook/config.js
Normal file
@ -0,0 +1,9 @@
|
||||
import { configure } from '@storybook/svelte';
|
||||
|
||||
// automatically import all files ending in *.stories.js
|
||||
const req = require.context('../stories', true, /.stories.js$/);
|
||||
function loadStories() {
|
||||
req.keys().forEach(filename => req(filename));
|
||||
}
|
||||
|
||||
configure(loadStories, module);
|
25
lib/cli/generators/SVELTE/template/stories/button.svelte
Normal file
25
lib/cli/generators/SVELTE/template/stories/button.svelte
Normal file
@ -0,0 +1,25 @@
|
||||
<button class="button" on:click="onClick(event)">
|
||||
{text}
|
||||
</button>
|
||||
|
||||
<style>
|
||||
.button {
|
||||
border: 1px solid #eee;
|
||||
border-radius: 3px;
|
||||
backgroundColor: #FFFFFF;
|
||||
cursor: pointer;
|
||||
font-size: 15px;
|
||||
padding: 3px 10px;
|
||||
margin: 10px;
|
||||
}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
methods: {
|
||||
onClick(event) {
|
||||
this.fire('click', event)
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
18
lib/cli/generators/SVELTE/template/stories/index.stories.js
Normal file
18
lib/cli/generators/SVELTE/template/stories/index.stories.js
Normal file
@ -0,0 +1,18 @@
|
||||
import { storiesOf } from '@storybook/svelte';
|
||||
import { action } from '@storybook/addon-actions';
|
||||
|
||||
import Button from './button.svelte';
|
||||
|
||||
storiesOf('Button', module)
|
||||
.add('with text', () => ({
|
||||
Component: Button,
|
||||
data: { text: 'Hello Button' },
|
||||
on: { click: action('clicked') },
|
||||
}))
|
||||
.add('with some emoji', () => ({
|
||||
Component: Button,
|
||||
data: {
|
||||
text: '😀 😎 👍 💯',
|
||||
},
|
||||
on: { click: action('clicked') },
|
||||
}));
|
@ -21,6 +21,7 @@ import markoGenerator from '../generators/MARKO';
|
||||
import htmlGenerator from '../generators/HTML';
|
||||
import riotGenerator from '../generators/RIOT';
|
||||
import preactGenerator from '../generators/PREACT';
|
||||
import svelteGenerator from '../generators/SVELTE';
|
||||
|
||||
const logger = console;
|
||||
|
||||
@ -168,6 +169,11 @@ const installStorybook = (projectType, options) => {
|
||||
.then(commandLog('Adding storybook support to your "Preact" app'))
|
||||
.then(end);
|
||||
|
||||
case types.SVELTE:
|
||||
return svelteGenerator(npmOptions)
|
||||
.then(commandLog('Adding storybook support to your "Svelte" app'))
|
||||
.then(end);
|
||||
|
||||
default:
|
||||
paddedLog(`We couldn't detect your project type. (code: ${projectType})`);
|
||||
paddedLog(
|
||||
|
@ -18,6 +18,7 @@ const projectTypes = {
|
||||
HTML: 'HTML',
|
||||
RIOT: 'RIOT',
|
||||
PREACT: 'PREACT',
|
||||
SVELTE: 'SVELTE',
|
||||
};
|
||||
|
||||
export default projectTypes;
|
||||
@ -34,6 +35,7 @@ export const supportedFrameworks = [
|
||||
'marko',
|
||||
'meteor',
|
||||
'preact',
|
||||
'svelte',
|
||||
];
|
||||
|
||||
const notInstallableProjectTypes = [
|
||||
|
@ -67,6 +67,7 @@
|
||||
"@storybook/react": "5.1.0-alpha.7",
|
||||
"@storybook/react-native": "5.1.0-alpha.7",
|
||||
"@storybook/riot": "5.1.0-alpha.7",
|
||||
"@storybook/svelte": "5.1.0-alpha.7",
|
||||
"@storybook/ui": "5.1.0-alpha.7",
|
||||
"@storybook/vue": "5.1.0-alpha.7"
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user