'import scss file with Storybook
I'm currently facing an issue with Storybook. Everything is working great in my app, with webpack. Storybook seems to have issue with my configuration.
Here's my webpack.config.js :
module.exports = {
entry: './index.js',
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
include: __dirname
},
{
test: /\.scss$/,
use: [
{loader: "style-loader"},
{loader: "css-loader"},
{loader: "sass-loader",
options: {
includePaths: [__dirname]
}
}]
},
Storybook have issue with parsing the scss file, do I need to create a specific webpack.config.js for Storybook to solve this?
In my main app I'm importing my scss file this way : import './styles/base.scss'
Solution 1:[1]
For those running storybook on Create React App, adding MiniCssExtractPlugin to .storybook/webpack.config.jon solved my problem loading sass files:
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = function({ config }) {
config.module.rules.push({
test: /\.scss$/,
loaders: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
include: path.resolve(__dirname, '../')
});
config.plugins.push(new MiniCssExtractPlugin({ filename: '[name].css' }))
return config;
};
Solution 2:[2]
There is simple code for main.js in Storybook 6, and it works fine for me!
const path = require('path');
// Export a function. Accept the base config as the only param.
module.exports = {
stories: [...],
addons:[...],
webpackFinal: async (config, { configType }) => {
// `configType` has a value of 'DEVELOPMENT' or 'PRODUCTION'
// You can change the configuration based on that.
// 'PRODUCTION' is used when building the static version of storybook.
// Make whatever fine-grained changes you need
config.module.rules.push({
test: /\.scss$/,
use: ['style-loader', 'css-loader?url=false', 'sass-loader'],
include: path.resolve(__dirname, '../'),
});
// Return the altered config
return config;
},
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dani Riera |
| Solution 2 | Steven Grimaldo |
