Skip to content

Commit

Permalink
Add webpack-to-vite migration guidance
Browse files Browse the repository at this point in the history
  • Loading branch information
kylegach committed Dec 27, 2024
1 parent eaaa59b commit 50a61c5
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
83 changes: 83 additions & 0 deletions docs/_snippets/webpack-final-to-vite-final.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
```js filename=".storybook/main.js" renderer="common" language="js" tabTitle="With Webpack"
export default {
// Replace your-framework with the framework you are using (e.g., react-webpack5, nextjs, angular)
framework: '@storybook/your-framework',
stories: ['../src/**/*.mdx', '../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
async webpackFinal(config) {
storybookBaseConfig.module?.rules?.push({
test: /\.(graphql|gql)$/,
include: [path.resolve('./lib/emails')],
exclude: /node_modules/,
loader: 'graphql-tag/loader',
});
storybookBaseConfig.module?.rules?.push({
test: /\.(graphql|gql)$/,
include: [path.resolve('./lib/schema')],
exclude: /node_modules/,
loader: 'raw-loader',
});

return config;
},
};
```
```js filename=".storybook/main.js" renderer="common" language="js" tabTitle="With Vite"
export default {
// Replace your-framework with the framework you are using (e.g., react-vite, vue3-vite)
framework: '@storybook/your-framework',
stories: ['../src/**/*.mdx', '../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
async viteFinal(config) {
return {
...config,
plugins: [...(config.plugins ?? []), graphql()],
};
},
};
```
```ts filename=".storybook/main.ts" renderer="common" language="ts" tabTitle="With Webpack"
// Replace your-framework with the framework you are using (e.g., react-webpack5, nextjs, angular)
import type { StorybookConfig } from '@storybook/your-framework';

const config: StorybookConfig = {
framework: '@storybook/your-framework',
stories: ['../src/**/*.mdx', '../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
async webpackFinal(config) {
storybookBaseConfig.module?.rules?.push({
test: /\.(graphql|gql)$/,
include: [path.resolve('./lib/emails')],
exclude: /node_modules/,
loader: 'graphql-tag/loader',
});
storybookBaseConfig.module?.rules?.push({
test: /\.(graphql|gql)$/,
include: [path.resolve('./lib/schema')],
exclude: /node_modules/,
loader: 'raw-loader',
});

return config;
},
};

export default config;
```
```ts filename=".storybook/main.ts" renderer="common" language="ts" tabTitle="With Vite"
// Replace your-framework with the framework you are using (e.g., react-vite, vue3-vite)
import type { StorybookConfig } from '@storybook/your-framework';

const config: StorybookConfig = {
framework: '@storybook/your-framework',
stories: ['../src/**/*.mdx', '../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
async viteFinal(config) {
return {
...config,
plugins: [...(config.plugins ?? []), graphql()],
};
},
};

export default config;
```
14 changes: 14 additions & 0 deletions docs/builders/vite.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ If you need, you can also configure Storybook's Vite builder using TypeScript. R

## Troubleshooting

### Migrating from Webpack

Vite generally handles more use cases out of the box than Webpack. For example, loading styles just works for most projects. So, when migrating a Webpack-based project to Vite, you may find that you don't need all of your previous configuration.

We recommend starting with no Storybook-specific Vite configuration and only adding what you determine your project actually requires.

For reference, here is a Webpack configuration to handle loading graphql queries and its equivalent, using a plugin, in Vite:

{/* prettier-ignore-start */}

<CodeSnippets path="webpack-final-to-vite-final.md" />

{/* prettier-ignore-end */}

### Working directory not being detected

By default, the Vite builder enables Vite's [`server.fs.strict`](https://vitejs.dev/config/#server-fs-strict) option for increased security, defining the project's `root` to Storybook's configuration directory.
Expand Down
2 changes: 2 additions & 0 deletions docs/get-started/frameworks/nextjs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ Storybook for Next.js is a [framework](../../contribute/framework.mdx) that make

<Callout variant="info">
If your Storybook configuration contains custom Webpack operations in [`webpackFinal`](../../api/main-config/main-config-webpack-final.mdx), you will likely need to create equivalents in [`viteFinal`](../../api/main-config/main-config-vite-final.mdx).

For more information, see the [Vite builder documentation](../../builders/vite.mdx#migrating-from-webpack).
</Callout>

Finally, if you were using Storybook plugins to integrate with Next.js, those are no longer necessary when using this framework and can be removed:
Expand Down

0 comments on commit 50a61c5

Please sign in to comment.