-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vite: Add partial SvelteKit support #19338
Changes from 5 commits
70f36b4
7d4c117
ceaf69b
41091a6
f205c1c
314d985
6056660
8d38821
0a7f052
bb3583a
22ff5e7
ea50752
5a53bca
fef7850
ab91bc8
c2ccb55
5deb296
7ecab8d
26a2614
da547d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import type { StorybookConfig } from '@storybook/builder-vite'; | ||
import { PluginOption } from 'vite'; | ||
import { svelteDocgen } from './plugins/svelte-docgen'; | ||
|
||
export const addons: StorybookConfig['addons'] = ['@storybook/svelte']; | ||
|
@@ -24,8 +25,23 @@ export const viteFinal: StorybookConfig['viteFinal'] = async (config, { presets | |
|
||
plugins.push(svelteDocgen(config)); | ||
|
||
removeSvelteKitPlugin(plugins); | ||
|
||
return { | ||
...config, | ||
plugins, | ||
}; | ||
}; | ||
|
||
const removeSvelteKitPlugin = (plugins: PluginOption[]) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd still love to find a way to avoid having to do this, since we'll start getting complaints about sveltekit aliases not working (even though there seems to be some debate over whether they should be used at all inside components). But, this seems like a reasonable workaround for now, until we can figure it out. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, let's continue investigating the core issue and do a follow up to this. |
||
plugins.forEach((plugin, index) => { | ||
if (plugin && 'name' in plugin && plugin.name === 'vite-plugin-svelte-kit') { | ||
// eslint-disable-next-line no-param-reassign -- we explicitly want to mutate the array as stated in Vite docs | ||
JReinhold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
plugins[index] = undefined; | ||
} | ||
if (Array.isArray(plugin)) { | ||
// recursive, Vite plugins can be nested | ||
removeSvelteKitPlugin(plugin); | ||
} | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,13 @@ | ||
import fse from 'fs-extra'; | ||
import { logger } from '@storybook/node-logger'; | ||
|
||
import { CoreBuilder } from '../../project_types'; | ||
import { baseGenerator } from '../baseGenerator'; | ||
import { Generator } from '../types'; | ||
|
||
const generator: Generator = async (packageManager, npmOptions, options) => { | ||
let extraMain; | ||
// svelte.config.js ? | ||
if (fse.existsSync('./svelte.config.js')) { | ||
logger.info("Configuring preprocessor from 'svelte.config.js'"); | ||
|
||
extraMain = { | ||
svelteOptions: { preprocess: '%%require("../svelte.config.js").preprocess%%' }, | ||
}; | ||
} else if (fse.existsSync('./svelte.config.cjs')) { | ||
logger.info("Configuring preprocessor from 'svelte.config.cjs'"); | ||
|
||
extraMain = { | ||
svelteOptions: { preprocess: '%%require("../svelte.config.cjs").preprocess%%' }, | ||
}; | ||
} else { | ||
// svelte-preprocess dependencies ? | ||
const packageJson = packageManager.retrievePackageJson(); | ||
if (packageJson.devDependencies && packageJson.devDependencies['svelte-preprocess']) { | ||
logger.info("Configuring preprocessor with 'svelte-preprocess'"); | ||
|
||
extraMain = { | ||
svelteOptions: { preprocess: '%%require("svelte-preprocess")()%%' }, | ||
}; | ||
} | ||
} | ||
|
||
Comment on lines
-9
to
-34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as per #19280 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need any MIGRATION.md notes? @JReinhold There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, users needs to remove their I'll write it up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about pulling this change out to its own PR? I'd love to get it into the next release, since svelte apps are currently broken after bootstrapping with Storybook. |
||
const extraPackages = options.builder === CoreBuilder.Webpack5 ? ['svelte', 'svelte-loader'] : []; | ||
|
||
await baseGenerator(packageManager, npmOptions, options, 'svelte', { | ||
extraPackages, | ||
extensions: ['js', 'jsx', 'ts', 'tsx', 'svelte'], | ||
extraMain, | ||
commonJs: true, | ||
}); | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally this should have been a plugin, but it doesn't seem to have an affect. The plugin correctly removes
vite-plugin-svelte-kit
from the config but it doesn't change any behaviorThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes unfortunately there's no way to remove plugins from within a plugin, as far as I can tell.