首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Storybook导入scss文件

使用Storybook导入scss文件
EN

Stack Overflow用户
提问于 2017-08-13 21:07:45
回答 3查看 19.5K关注 0票数 17

我现在正面临着一个关于Storybook的问题。在我的应用程序中,有了webpack,一切都运行得很好。Storybook似乎对我的配置有问题。

这是我的webpack.config.js:

代码语言:javascript
复制
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在解析scss文件时有问题,我需要为Storybook创建一个特定的webpack.config.js来解决这个问题吗?

在我的主应用中,我以这样的方式导入我的scss文件:import './styles/base.scss'

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-08-13 22:17:52

只需添加一个与我现有的webpack.config.js非常类似的one即可工作:

代码语言:javascript
复制
const path = require('path')

module.exports = {
    module: {
     rules: [
     {
        test: /\.scss$/,
        loaders: ['style-loader', 'css-loader', 'sass-loader'],
        include: path.resolve(__dirname, '../')
     },
     {  test: /\.css$/,
        loader: 'style-loader!css-loader',
        include: __dirname
     },
     {
        test: /\.(woff|woff2)$/,
        use: {
          loader: 'url-loader',
          options: {
            name: 'fonts/[hash].[ext]',
            limit: 5000,
            mimetype: 'application/font-woff'
          }
         }
     },
     {
       test: /\.(ttf|eot|svg|png)$/,
       use: {
          loader: 'file-loader',
          options: {
            name: 'fonts/[hash].[ext]'
          }
       }
     }
   ]
 }
}
票数 14
EN

Stack Overflow用户

发布于 2020-03-17 16:03:03

对于那些在Create React App上运行storybook的人来说,在.storybook/webpack.config.jon中添加MiniCssExtractPlugin解决了我加载sass文件的问题:

代码语言:javascript
复制
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;
};

Credits to Nigel Sim!

票数 6
EN

Stack Overflow用户

发布于 2021-06-10 22:14:42

在StoryBook6中有简单的min.js代码,它对我来说很好用!

代码语言:javascript
复制
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;
  },
};

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45660679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档