Usage with storybook #371
Replies: 14 comments 10 replies
-
Also worth noting that Storybook has experimental Vite support. I tried it out and it works well — and is super fast. |
Beta Was this translation helpful? Give feedback.
-
Thanks @tim-richter for the reproduction repository! I was exactly trying to do the same things but then on the current release of storybook (with Webpack 4) but got eventually stuck. No luck so far 😞 but I'll continue to also try as this looks promising. |
Beta Was this translation helpful? Give feedback.
-
Good news I was able to make For yarn add --dev @vanilla-extract/css @vanilla-extract/webpack-plugin [email protected] Then similar as @tim-richter mentioned the storybook configuration needs to be extended to exclude This can be done by adapting storybooks following files: // ./.storybook/main.js
const path = require('path');
module.exports = {
stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
addons: [
path.resolve('./.storybook/vanilla-extract.js'),
'@storybook/addon-links',
'@storybook/addon-essentials',
'@storybook/preset-create-react-app',
],
}; Then in // ./.storybook/vanilla-extract.js
const { VanillaExtractPlugin } = require('@vanilla-extract/webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
webpackFinal(baseConfig, options) {
const { module = {}, plugins = {} } = baseConfig;
const isCssRule = (rule) =>
[]
.concat(rule?.test)
.filter(Boolean)
.find((expr) => expr.test('random.css'));
module.rules
.map(({ oneOf, ...rule }) =>
oneOf ? oneOf.find(isCssRule) : isCssRule(rule),
)
.filter(Boolean)
// Here we exclude *.vanilla.css files which will be taken up by the vanilla-extract
.forEach((match) => match.exclude.push(/\.vanilla\.css$/));
return {
...baseConfig,
plugins: [
...plugins,
new VanillaExtractPlugin(),
new MiniCssExtractPlugin(),
],
module: {
...module,
rules: [
...module.rules,
{
test: /\.vanilla\.css$/i, // Targets only CSS files generated by vanilla-extract
use: [
MiniCssExtractPlugin.loader,
{
loader: require.resolve('css-loader'),
options: {
url: false, // Required as image imports should be handled via JS/TS import statements
},
},
],
},
],
},
};
},
}; For storybook with webpack@5@tim-richter for Line 18 you disable the default const { VanillaExtractPlugin } = require('@vanilla-extract/webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
webpackFinal(baseConfig, options) {
const { module = {}, plugins = {} } = baseConfig;
const cssRule = module.rules.find((rule) => rule?.test?.test('test.css'));
cssRule.test = /.*(?<!\.vanilla)\.css$/;
return {
...baseConfig,
plugins: [
...plugins,
new VanillaExtractPlugin(),
new MiniCssExtractPlugin(),
],
module: {
...module,
rules: [
...module.rules,
{
test: /\.vanilla\.css$/i, // Targets only CSS files generated by vanilla-extract
use: [
MiniCssExtractPlugin.loader,
{
loader: require.resolve('css-loader'),
options: {
url: false, // Required as image imports should be handled via JS/TS import statements
},
},
],
},
],
},
};
},
}; |
Beta Was this translation helpful? Give feedback.
-
@tim-richter thanks so much for putting this together. Huge time saver for me to pick out the bits. I used @SaschaDens suggestion for Webpack 5, and also had to add this to my Storybook |
Beta Was this translation helpful? Give feedback.
-
For anyone struggling to get storybook up and running I have a simple example repo which has Storybook and Vanilla Extact with Vite. Feel free to go through the repo. Its simple, hassle free and works well. |
Beta Was this translation helpful? Give feedback.
-
Best solution with webpack4! create a file called webpack.config.js inside the .storybook folder, with the content: // .storybook/webpack.config.js
const { VanillaExtractPlugin } = require('@vanilla-extract/webpack-plugin');
module.exports = ({ config }) => {
const plugins = [new VanillaExtractPlugin(), ...config.plugins]
config.plugins = plugins
return config;
}; If you need another plugin, example: // .storybook/webpack.config.js
const { VanillaExtractPlugin } = require('@vanilla-extract/webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = ({ config }) => {
const plugins = [new VanillaExtractPlugin(), new MiniCssExtractPlugin(), ...config.plugins]
config.plugins = plugins
return config;
}; |
Beta Was this translation helpful? Give feedback.
-
For anyone struggling to get Vanilla-Extract working with Storybook Vite, here's my storybook main.js: const { mergeConfig } = require('vite');
module.exports = {
"stories": [
"../src/**/*.stories.mdx",
"../src/**/*.stories.@(js|jsx|ts|tsx)"
],
"addons": [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-interactions"
],
"framework": "@storybook/react",
"core": {
"builder": "@storybook/builder-vite"
},
"features": {
"storyStoreV7": true
},
async viteFinal(config) {
return mergeConfig(config, {
plugins: [require("@vanilla-extract/vite-plugin").vanillaExtractPlugin()]
});
}
} |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I think it could be simpler. const { VanillaExtractPlugin } = require("@vanilla-extract/webpack-plugin");
const { merge } = require("webpack-merge");
module.exports = {
stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
addons: [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-interactions",
],
framework: "@storybook/react",
core: {
builder: "@storybook/builder-webpack5",
},
webpackFinal: async (config) =>
merge(config, {
plugins: [new VanillaExtractPlugin()],
}),
}; |
Beta Was this translation helpful? Give feedback.
-
It takes roughly 15 seconds between making code changes, and actually seeing those changes in Storybook. Is anybody else having these issues? I do have a large amount of stories, but 15 seconds to notice a change still seems pretty excessive. I dont believe this to be storybook itself, as if I update a prop in the side panel, it updates immediately. So i am assuming its something in the build. I am using vite |
Beta Was this translation helpful? Give feedback.
-
Thank you! Your solution helped point me in the right direction. However, instead of removing Storybook's css rule, I found it better to just add an Here's my working config with Storybook 7 and webpack 5: // .storybook/main.js
import { VanillaExtractPlugin } from "@vanilla-extract/webpack-plugin";
import MiniCssExtractPlugin from "mini-css-extract-plugin";
export default {
// ... other options
framework: {
name: "@storybook/react-webpack5",
options: {},
},
webpackFinal: async (config) => {
const customConfig = { ...config };
// modify css rule of storybook to exclude Vanilla Extract files
for (let i = 0; i < customConfig.module.rules.length; i++) {
if (customConfig.module.rules[i].test.test("sample.css")) {
customConfig.module.rules[i].exclude = /\.vanilla\.css$/i;
break;
}
}
customConfig.module.rules.push(
{
test: /\.vanilla\.css$/i,
use: [
MiniCssExtractPlugin.loader,
{ loader: "css-loader", options: { url: false } },
],
},
);
customConfig.plugins.push(
new VanillaExtractPlugin(),
new MiniCssExtractPlugin()
);
return customConfig;
},
}; Full config for reference is here. |
Beta Was this translation helpful? Give feedback.
-
Storybook 6 + Webpack 5 with working HMR (#905) // .storybook/main.js
module.exports = {
...
webpackFinal: async (config) => ({
...config,
optimization: {
...config.optimization,
// https://github.com/vanilla-extract-css/vanilla-extract/issues/905
splitChunks: false,
},
plugins: [...config.plugins, new VanillaExtractPlugin()],
})
} |
Beta Was this translation helpful? Give feedback.
-
This Vite solutions doesn't work for me. Any update? |
Beta Was this translation helpful? Give feedback.
-
@gabrieloureiro check out this recipe from the Storybook team. Since Storybook reads your vite config out of the box, try removing
|
Beta Was this translation helpful? Give feedback.
-
Hey everyone 👋
I've made storybook work with vanilla-extract and wanted to introduce the idea of adding documentation about the process so other developers don't run into the same issues I had with it.
Problems were mainly:
As far as I can see everything works perfectly fine now even hot reloading etc.
Reproduction Repo
Beta Was this translation helpful? Give feedback.
All reactions