@storybook/addon-styling-webpack
Get started in Storybook 7 faster with popular styling tools.
To get started, install the package using the Storybook CLI:
pnpm:
pnpm dlx storybook@latest add @storybook/addon-styling-webpack
yarn:
yarn dlx storybook@latest add @storybook/addon-styling-webpack
npm:
npx storybook@latest add @storybook/addon-styling-webpack
What does this do?
Under the hood, this installs the package in your project and adds the addon to your main.js
file.
After that, it will run npx @storybook/auto-config styling
. This is a codemod package that will attempt to detect the styling tools in your project and configure your storybook accordingly.
If the codemod fails, please try running npx @storybook/auto-config styling
manually. If that fails, please file an issue in the auto-config repo.
@storybook/addon-styling-webpack
takes Webpack module rules for your styling tools and replaces the existing rules in Storybook's Webpack config. This avoids duplicating rules that will break your Storybook build.
{
name: '@storybook/addon-styling-webpack',
options: {
rules: [
// Replaces existing CSS rules with given rule
{
test: /\.css$/,
use: [
'style-loader',
'css-loader',
],
}
]
}
}
It can also take Webpack plugins to add to the Storybook config.
{
name: '@storybook/addon-styling-webpack',
options: {
plugins: [
new MiniCssExtractPlugin(),
]
}
}
Below are a few popular configurations for common styling tools to get you started. More complex configurations are possible by combining the different rules below.
// Often used for tailwind
{
name: '@storybook/addon-styling-webpack',
options: {
rules: [
// Replaces existing CSS rules to support PostCSS
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: { importLoaders: 1 }
},
{
// Gets options from `postcss.config.js` in your project root
loader: 'postcss-loader',
options: { implementation: require.resolve('postcss') }
}
],
}
]
}
}
You can also take a look at this example project that uses PostCSS for Tailwind with Storybook:
{
name: '@storybook/addon-styling-webpack',
options: {
rules: [
// Replaces existing CSS rules to support CSS Modules
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: {
auto: true,
localIdentName: '[name]__[local]--[hash:base64:5]',
},
},
}
],
}
]
}
}
{
name: '@storybook/addon-styling-webpack',
options: {
rules: [
// Replaces any existing Sass rules with given rules
{
test: /\.s[ac]ss$/i,
use: [
"style-loader",
"css-loader",
{
loader: "sass-loader",
options: { implementation: require.resolve("sass") }
},
],
},
]
}
}
{
name: '@storybook/addon-styling-webpack',
options: {
rules: [
// Replaces any existing Sass rules with given rules
{
test: /\.less$/i,
use: [
"style-loader",
"css-loader",
{
loader: "less-loader",
options: { implementation: require.resolve("less") }
},
],
},
]
}
}
{
name: '@storybook/addon-styling-webpack',
options: {
plugins: [
new VanillaExtractPlugin(),
new MiniCssExtractPlugin(),
],
rules: [
{
test: /\.css$/,
sideEffects: true,
use: [
require.resolve("style-loader"),
{
loader: require.resolve("css-loader"),
options: {},
},
],
exclude: /\.vanilla\.css$/,
},
{
// Targets only CSS files generated by vanilla-extract
test: /\.vanilla\.css$/i,
sideEffects: true,
use: [
MiniCssExtractPlugin.loader,
{
loader: require.resolve('css-loader'),
options: {
// Required as image imports should be handled via JS/TS import statements
url: false,
},
},
],
},
]
}
}
If you'd like to contribute to this addon, THANK YOU, I'd love your help π
pnpm build
build and package your addon codenext
version on npm, and the development branch where most work occurslatest
version on npm and the stable version that most users usenext
branch, which depends on the next
version of Storybook.next
NPM tag.pick
will get cherry-picked back to the main
branch and will generate a release on the latest
npm tag.