From 024de34b51ecad24fa49154ffe53ae1f18a70647 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Wed, 30 Nov 2022 23:00:06 +0100 Subject: [PATCH 01/14] initial SvelteKit framework --- code/frameworks/svelte-kit/package.json | 79 +++++++++++++++++++ code/frameworks/svelte-kit/preset.js | 1 + code/frameworks/svelte-kit/src/index.ts | 1 + code/frameworks/svelte-kit/src/preset.ts | 21 +++++ .../svelte-kit/template/cli/Button.stories.js | 39 +++++++++ .../svelte-kit/template/cli/Button.svelte | 34 ++++++++ code/frameworks/svelte-kit/tsconfig.json | 9 +++ code/frameworks/svelte-vite/src/preset.ts | 9 ++- .../cli/src/generators/SVELTE_KIT/index.ts | 8 ++ code/lib/cli/src/generators/baseGenerator.ts | 2 +- code/lib/cli/src/initiate.ts | 6 ++ code/lib/cli/src/project_types.ts | 11 ++- code/lib/cli/src/repro-generators/configs.ts | 8 ++ code/lib/cli/src/repro-templates.ts | 5 +- code/lib/cli/src/versions.ts | 1 + code/workspace.json | 5 ++ code/yarn.lock | 16 +++- 17 files changed, 249 insertions(+), 6 deletions(-) create mode 100644 code/frameworks/svelte-kit/package.json create mode 100644 code/frameworks/svelte-kit/preset.js create mode 100644 code/frameworks/svelte-kit/src/index.ts create mode 100644 code/frameworks/svelte-kit/src/preset.ts create mode 100644 code/frameworks/svelte-kit/template/cli/Button.stories.js create mode 100644 code/frameworks/svelte-kit/template/cli/Button.svelte create mode 100644 code/frameworks/svelte-kit/tsconfig.json create mode 100644 code/lib/cli/src/generators/SVELTE_KIT/index.ts diff --git a/code/frameworks/svelte-kit/package.json b/code/frameworks/svelte-kit/package.json new file mode 100644 index 000000000000..33dc8324128f --- /dev/null +++ b/code/frameworks/svelte-kit/package.json @@ -0,0 +1,79 @@ +{ + "name": "@storybook/svelte-kit", + "version": "7.0.0-alpha.54", + "description": "Storybook for SvelteKit", + "keywords": [ + "storybook", + "svelte", + "sveltekit", + "svelte-kit" + ], + "homepage": "https://github.com/storybookjs/storybook/tree/next/code/frameworks/svelte-kit", + "bugs": { + "url": "https://github.com/storybookjs/storybook/issues" + }, + "repository": { + "type": "git", + "url": "https://github.com/storybookjs/storybook.git", + "directory": "code/frameworks/svelte-kit" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/storybook" + }, + "license": "MIT", + "exports": { + ".": { + "require": "./dist/index.js", + "import": "./dist/index.mjs", + "types": "./dist/index.d.ts" + }, + "./preset": { + "require": "./dist/preset.js", + "import": "./dist/preset.mjs", + "types": "./dist/preset.d.ts" + }, + "./package.json": "./package.json" + }, + "main": "dist/index.js", + "module": "dist/index.mjs", + "types": "dist/index.d.ts", + "files": [ + "dist/**/*", + "template/**/*", + "types/**/*", + "README.md", + "*.js", + "*.d.ts" + ], + "scripts": { + "check": "tsc --noEmit", + "prep": "../../../scripts/prepare/bundle.ts" + }, + "dependencies": { + "@storybook/builder-vite": "7.0.0-alpha.54", + "@storybook/svelte-vite": "7.0.0-alpha.54" + }, + "peerDependencies": { + "@storybook/addon-svelte-csf": "^2.0.0" + }, + "peerDependenciesMeta": { + "@storybook/addon-svelte-csf": { + "optional": true + } + }, + "engines": { + "node": "^14.18 || >=16" + }, + "publishConfig": { + "access": "public" + }, + "bundler": { + "entries": [ + "./src/index.ts", + "./src/preset.ts" + ], + "platform": "node" + }, + "gitHead": "91177d4e27daec556a24ae6223c3cbe17a998d9b" +} diff --git a/code/frameworks/svelte-kit/preset.js b/code/frameworks/svelte-kit/preset.js new file mode 100644 index 000000000000..a83f95279e7f --- /dev/null +++ b/code/frameworks/svelte-kit/preset.js @@ -0,0 +1 @@ +module.exports = require('./dist/preset'); diff --git a/code/frameworks/svelte-kit/src/index.ts b/code/frameworks/svelte-kit/src/index.ts new file mode 100644 index 000000000000..1a8d7c1a6e62 --- /dev/null +++ b/code/frameworks/svelte-kit/src/index.ts @@ -0,0 +1 @@ +export * from '@storybook/svelte-vite'; diff --git a/code/frameworks/svelte-kit/src/preset.ts b/code/frameworks/svelte-kit/src/preset.ts new file mode 100644 index 000000000000..2719f02b7b57 --- /dev/null +++ b/code/frameworks/svelte-kit/src/preset.ts @@ -0,0 +1,21 @@ +import { type StorybookConfig } from '@storybook/svelte-vite'; +// @ts-expect-error -- TS picks up the type from preset.js instead of dist/preset.d.ts +import { viteFinal as svelteViteFinal } from '@storybook/svelte-vite/preset'; +import { withoutVitePlugins } from '@storybook/builder-vite'; + +export const core: StorybookConfig['core'] = { + builder: '@storybook/builder-vite', + renderer: '@storybook/svelte', +}; + +export const viteFinal: NonNullable = async (config, options) => { + const baseConfig = await svelteViteFinal(config, options); + + let { plugins = [] } = baseConfig; + + // Remove vite-plugin-svelte-kit from plugins if using SvelteKit + // see https://github.com/storybookjs/storybook/issues/19280#issuecomment-1281204341 + plugins = withoutVitePlugins(plugins, ['vite-plugin-svelte-kit']); + + return { ...baseConfig, plugins }; +}; diff --git a/code/frameworks/svelte-kit/template/cli/Button.stories.js b/code/frameworks/svelte-kit/template/cli/Button.stories.js new file mode 100644 index 000000000000..6377b5dda3dc --- /dev/null +++ b/code/frameworks/svelte-kit/template/cli/Button.stories.js @@ -0,0 +1,39 @@ +import Button from './Button.svelte'; + +// More on how to set up stories at: https://storybook.js.org/docs/7.0/svelte/writing-stories/introduction +export default { + title: 'Example/Button', + component: Button, + tags: ['docsPage'], + argTypes: { + backgroundColor: { control: 'color' }, + }, +}; + +// More on writing stories with args: https://storybook.js.org/docs/7.0/svelte/writing-stories/args +export const Primary = { + args: { + primary: true, + label: 'Button', + }, +}; + +export const Secondary = { + args: { + label: 'Button', + }, +}; + +export const Large = { + args: { + size: 'large', + label: 'Button', + }, +}; + +export const Small = { + args: { + size: 'small', + label: 'Button', + }, +}; diff --git a/code/frameworks/svelte-kit/template/cli/Button.svelte b/code/frameworks/svelte-kit/template/cli/Button.svelte new file mode 100644 index 000000000000..cdf77abadac7 --- /dev/null +++ b/code/frameworks/svelte-kit/template/cli/Button.svelte @@ -0,0 +1,34 @@ + + + diff --git a/code/frameworks/svelte-kit/tsconfig.json b/code/frameworks/svelte-kit/tsconfig.json new file mode 100644 index 000000000000..26be65113f88 --- /dev/null +++ b/code/frameworks/svelte-kit/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "strict": true, + "resolveJsonModule": true, + "skipLibCheck": true + }, + "include": ["src/**/*", "template/**/*"] +} diff --git a/code/frameworks/svelte-vite/src/preset.ts b/code/frameworks/svelte-vite/src/preset.ts index 5a709ef26169..8687e3cbd38c 100644 --- a/code/frameworks/svelte-vite/src/preset.ts +++ b/code/frameworks/svelte-vite/src/preset.ts @@ -1,4 +1,5 @@ import { type StorybookConfig, withoutVitePlugins } from '@storybook/builder-vite'; +import { deprecate } from '@storybook/node-logger'; import { hasPlugin } from './utils'; import { svelteDocgen } from './plugins/svelte-docgen'; @@ -7,7 +8,7 @@ export const core: StorybookConfig['core'] = { renderer: '@storybook/svelte', }; -export const viteFinal: StorybookConfig['viteFinal'] = async (config, options) => { +export const viteFinal: NonNullable = async (config, options) => { let { plugins = [] } = config; const { svelte, loadSvelteConfig } = await import('@sveltejs/vite-plugin-svelte'); const svelteOptions: Record = await options.presets.apply( @@ -25,6 +26,12 @@ export const viteFinal: StorybookConfig['viteFinal'] = async (config, options) = // Add docgen plugin plugins.push(svelteDocgen(svelteConfig)); + // TODO: better SvelteKit detection, this warns when using @storybook/svelte-kit too + if (hasPlugin(plugins, 'vite-plugin-svelte-kit')) { + deprecate( + 'SvelteKit support in @storybook/svelte-vite is deprecated in Storybook 7.0, use the official @storybook/svelte-kit framework instead.' + ); + } // Remove vite-plugin-svelte-kit from plugins if using SvelteKit // see https://github.com/storybookjs/storybook/issues/19280#issuecomment-1281204341 plugins = withoutVitePlugins(plugins, ['vite-plugin-svelte-kit']); diff --git a/code/lib/cli/src/generators/SVELTE_KIT/index.ts b/code/lib/cli/src/generators/SVELTE_KIT/index.ts new file mode 100644 index 000000000000..9ec3eead95b2 --- /dev/null +++ b/code/lib/cli/src/generators/SVELTE_KIT/index.ts @@ -0,0 +1,8 @@ +import { baseGenerator } from '../baseGenerator'; +import type { Generator } from '../types'; + +const generator: Generator = async (packageManager, npmOptions, options) => { + await baseGenerator(packageManager, npmOptions, options, 'svelte', undefined, 'svelte-kit'); +}; + +export default generator; diff --git a/code/lib/cli/src/generators/baseGenerator.ts b/code/lib/cli/src/generators/baseGenerator.ts index 027a45df129b..b7b8b39e0a11 100644 --- a/code/lib/cli/src/generators/baseGenerator.ts +++ b/code/lib/cli/src/generators/baseGenerator.ts @@ -101,7 +101,7 @@ const hasInteractiveStories = (rendererId: SupportedRenderers) => ['react', 'angular', 'preact', 'svelte', 'vue', 'vue3', 'html'].includes(rendererId); const hasFrameworkTemplates = (framework?: SupportedFrameworks) => - ['angular', 'nextjs'].includes(framework); + ['angular', 'nextjs', 'svelte-kit'].includes(framework); export async function baseGenerator( packageManager: JsPackageManager, diff --git a/code/lib/cli/src/initiate.ts b/code/lib/cli/src/initiate.ts index d8cad6f19e8b..4377209ab9a2 100644 --- a/code/lib/cli/src/initiate.ts +++ b/code/lib/cli/src/initiate.ts @@ -26,6 +26,7 @@ import webComponentsGenerator from './generators/WEB-COMPONENTS'; import riotGenerator from './generators/RIOT'; import preactGenerator from './generators/PREACT'; import svelteGenerator from './generators/SVELTE'; +import svelteKitGenerator from './generators/SVELTE_KIT'; import raxGenerator from './generators/RAX'; import serverGenerator from './generators/SERVER'; import type { JsPackageManager } from './js-package-manager'; @@ -183,6 +184,11 @@ const installStorybook = ( commandLog('Adding Storybook support to your "Svelte" app\n') ); + case ProjectType.SVELTE_KIT: + return svelteKitGenerator(packageManager, npmOptions, generatorOptions).then( + commandLog('Adding Storybook support to your "SvelteKit" app\n') + ); + case ProjectType.RAX: return raxGenerator(packageManager, npmOptions, generatorOptions).then( commandLog('Adding Storybook support to your "Rax" app\n') diff --git a/code/lib/cli/src/project_types.ts b/code/lib/cli/src/project_types.ts index 2b0ac24d219b..0bbc73d2e6a1 100644 --- a/code/lib/cli/src/project_types.ts +++ b/code/lib/cli/src/project_types.ts @@ -16,7 +16,7 @@ function eqMajor(versionRange: string, major: number) { } // Should match @storybook/ -export type SupportedFrameworks = 'nextjs' | 'angular'; +export type SupportedFrameworks = 'nextjs' | 'angular' | 'svelte-kit'; // Should match @storybook/ export type SupportedRenderers = @@ -78,6 +78,7 @@ export enum ProjectType { RIOT = 'RIOT', PREACT = 'PREACT', SVELTE = 'SVELTE', + SVELTE_KIT = 'SVELTE_KIT', RAX = 'RAX', AURELIA = 'AURELIA', SERVER = 'SERVER', @@ -254,6 +255,14 @@ export const supportedTemplates: TemplateConfiguration[] = [ return dependencies.every(Boolean); }, }, + { + // TODO: This only works because it is before the SVELTE template. could be more explicit + preset: ProjectType.SVELTE_KIT, + dependencies: ['@sveltejs/kit'], + matcherFunction: ({ dependencies }) => { + return dependencies.every(Boolean); + }, + }, { preset: ProjectType.SVELTE, dependencies: ['svelte'], diff --git a/code/lib/cli/src/repro-generators/configs.ts b/code/lib/cli/src/repro-generators/configs.ts index 3a2194c152be..acdae8dbdaf9 100644 --- a/code/lib/cli/src/repro-generators/configs.ts +++ b/code/lib/cli/src/repro-generators/configs.ts @@ -266,3 +266,11 @@ export const svelte: Parameters = { version: 'latest', generator: 'npx giget github:sveltejs/template#master {{appName}}', }; + +export const svelteKit: Parameters = { + renderer: 'svelte', + name: 'svelteKit', + version: 'latest', + generator: + 'yarn create svelte-with-args --name={{appName}} --directory=. --template=skeleton --types=null --no-prettier --no-eslint --no-playwright', +}; diff --git a/code/lib/cli/src/repro-templates.ts b/code/lib/cli/src/repro-templates.ts index ec243a090c2d..2e6ede5ef653 100644 --- a/code/lib/cli/src/repro-templates.ts +++ b/code/lib/cli/src/repro-templates.ts @@ -169,10 +169,11 @@ export const allTemplates = { }, 'svelte-kit/skeleton-js': { name: 'Svelte Kit (JS)', + inDevelopment: true, script: 'yarn create svelte-with-args --name=svelte-kit/skeleton-js --directory=. --template=skeleton --types=null --no-prettier --no-eslint --no-playwright', expected: { - framework: '@storybook/svelte-vite', + framework: '@storybook/svelte-kit', renderer: '@storybook/svelte', builder: '@storybook/builder-vite', }, @@ -182,7 +183,7 @@ export const allTemplates = { script: 'yarn create svelte-with-args --name=svelte-kit/skeleton-ts --directory=. --template=skeleton --types=typescript --no-prettier --no-eslint --no-playwright', expected: { - framework: '@storybook/svelte-vite', + framework: '@storybook/svelte-kit', renderer: '@storybook/svelte', builder: '@storybook/builder-vite', }, diff --git a/code/lib/cli/src/versions.ts b/code/lib/cli/src/versions.ts index 4b16d815e4b4..4eccda50297e 100644 --- a/code/lib/cli/src/versions.ts +++ b/code/lib/cli/src/versions.ts @@ -68,6 +68,7 @@ export default { '@storybook/source-loader': '7.0.0-alpha.54', '@storybook/store': '7.0.0-alpha.54', '@storybook/svelte': '7.0.0-alpha.54', + '@storybook/svelte-kit': '7.0.0-alpha.54', '@storybook/svelte-vite': '7.0.0-alpha.54', '@storybook/svelte-webpack5': '7.0.0-alpha.54', '@storybook/telemetry': '7.0.0-alpha.54', diff --git a/code/workspace.json b/code/workspace.json index ad82327b6ada..b3823712f466 100644 --- a/code/workspace.json +++ b/code/workspace.json @@ -356,6 +356,11 @@ "root": "renderers/svelte", "type": "library" }, + "@storybook/svelte-kit": { + "implicitDependencies": [], + "root": "frameworks/svelte-kit", + "type": "library" + }, "@storybook/svelte-vite": { "implicitDependencies": [], "root": "frameworks/svelte-vite", diff --git a/code/yarn.lock b/code/yarn.lock index 834d9e989047..06328b3ea9e1 100644 --- a/code/yarn.lock +++ b/code/yarn.lock @@ -7431,7 +7431,21 @@ __metadata: languageName: unknown linkType: soft -"@storybook/svelte-vite@workspace:frameworks/svelte-vite": +"@storybook/svelte-kit@workspace:frameworks/svelte-kit": + version: 0.0.0-use.local + resolution: "@storybook/svelte-kit@workspace:frameworks/svelte-kit" + dependencies: + "@storybook/builder-vite": 7.0.0-alpha.54 + "@storybook/svelte-vite": 7.0.0-alpha.54 + peerDependencies: + "@storybook/addon-svelte-csf": ^2.0.0 + peerDependenciesMeta: + "@storybook/addon-svelte-csf": + optional: true + languageName: unknown + linkType: soft + +"@storybook/svelte-vite@7.0.0-alpha.54, @storybook/svelte-vite@workspace:frameworks/svelte-vite": version: 0.0.0-use.local resolution: "@storybook/svelte-vite@workspace:frameworks/svelte-vite" dependencies: From 842a1539285425b580e54ee44b03bf3014096e0e Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Wed, 30 Nov 2022 23:50:57 +0100 Subject: [PATCH 02/14] make svelte-kit framework more like svelte-vite --- code/frameworks/svelte-kit/README.md | 1 + code/frameworks/svelte-kit/jest.config.js | 7 +++++++ code/frameworks/svelte-kit/package.json | 5 +++++ code/frameworks/svelte-kit/tsconfig.json | 8 ++++---- code/frameworks/svelte-vite/src/preset.ts | 2 +- code/lib/cli/src/generators/baseGenerator.ts | 6 +++++- 6 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 code/frameworks/svelte-kit/README.md create mode 100644 code/frameworks/svelte-kit/jest.config.js diff --git a/code/frameworks/svelte-kit/README.md b/code/frameworks/svelte-kit/README.md new file mode 100644 index 000000000000..d9243c63d0a4 --- /dev/null +++ b/code/frameworks/svelte-kit/README.md @@ -0,0 +1 @@ +# Storybook for SvelteKit diff --git a/code/frameworks/svelte-kit/jest.config.js b/code/frameworks/svelte-kit/jest.config.js new file mode 100644 index 000000000000..343e4c7a7f32 --- /dev/null +++ b/code/frameworks/svelte-kit/jest.config.js @@ -0,0 +1,7 @@ +const path = require('path'); +const baseConfig = require('../../jest.config.node'); + +module.exports = { + ...baseConfig, + displayName: __dirname.split(path.sep).slice(-2).join(path.posix.sep), +}; diff --git a/code/frameworks/svelte-kit/package.json b/code/frameworks/svelte-kit/package.json index 33dc8324128f..d927647ee472 100644 --- a/code/frameworks/svelte-kit/package.json +++ b/code/frameworks/svelte-kit/package.json @@ -54,6 +54,11 @@ "@storybook/builder-vite": "7.0.0-alpha.54", "@storybook/svelte-vite": "7.0.0-alpha.54" }, + "devDependencies": { + "@types/node": "^16.0.0", + "typescript": "^4.9.3", + "vite": "^3.1.3" + }, "peerDependencies": { "@storybook/addon-svelte-csf": "^2.0.0" }, diff --git a/code/frameworks/svelte-kit/tsconfig.json b/code/frameworks/svelte-kit/tsconfig.json index 26be65113f88..13e04352a3c7 100644 --- a/code/frameworks/svelte-kit/tsconfig.json +++ b/code/frameworks/svelte-kit/tsconfig.json @@ -1,9 +1,9 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - "strict": true, - "resolveJsonModule": true, - "skipLibCheck": true + "rootDir": "./src", + "types": ["node"], + "resolveJsonModule": true }, - "include": ["src/**/*", "template/**/*"] + "include": ["src/**/*"] } diff --git a/code/frameworks/svelte-vite/src/preset.ts b/code/frameworks/svelte-vite/src/preset.ts index 8687e3cbd38c..226cd5f8c490 100644 --- a/code/frameworks/svelte-vite/src/preset.ts +++ b/code/frameworks/svelte-vite/src/preset.ts @@ -29,7 +29,7 @@ export const viteFinal: NonNullable = async (confi // TODO: better SvelteKit detection, this warns when using @storybook/svelte-kit too if (hasPlugin(plugins, 'vite-plugin-svelte-kit')) { deprecate( - 'SvelteKit support in @storybook/svelte-vite is deprecated in Storybook 7.0, use the official @storybook/svelte-kit framework instead.' + 'SvelteKit support in @storybook/svelte-vite is deprecated in Storybook 7.0, use @storybook/svelte-kit instead.' ); } // Remove vite-plugin-svelte-kit from plugins if using SvelteKit diff --git a/code/lib/cli/src/generators/baseGenerator.ts b/code/lib/cli/src/generators/baseGenerator.ts index b7b8b39e0a11..6dfba9eb5ba5 100644 --- a/code/lib/cli/src/generators/baseGenerator.ts +++ b/code/lib/cli/src/generators/baseGenerator.ts @@ -209,7 +209,11 @@ export async function baseGenerator( await configurePreview(rendererId); // FIXME: temporary workaround for https://github.com/storybookjs/storybook/issues/17516 - if (frameworkPackages.find((pkg) => pkg.match(/^@storybook\/.*-vite$/))) { + if ( + frameworkPackages.find( + (pkg) => pkg.match(/^@storybook\/.*-vite$/) || pkg === '@storybook/svelte-kit' + ) + ) { const previewHead = dedent`