【问题标题】:import scss file with Storybook使用 Storybook 导入 scss 文件
【发布时间】:2018-01-21 11:24:31
【问题描述】:

我目前遇到了 Storybook 的问题。使用 webpack 在我的应用程序中一切正常。 Storybook 似乎与我的配置有问题。

这是我的 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 在解析 scss 文件时遇到问题,我需要为 Storybook 创建一个特定的 webpack.config.js 来解决这个问题吗?

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

【问题讨论】:

  • 需要更多信息来回答您的问题,例如您如何将 scss 文件导入到您的应用中。
  • 感谢您通知我。我会完成我的问题
  • 您的用例文档中有一个部分storybook.js.org/configurations/custom-webpack-config
  • 非常感谢,我会按照文档步骤尝试解决。

标签: reactjs webpack sass storybook


【解决方案1】:

故事书 6 中有 min.js 的简单代码,对我来说很好用!

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;
  },
};

【讨论】:

    【解决方案2】:

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

    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]'
              }
           }
         }
       ]
     }
    }
    

    【讨论】:

      【解决方案3】:

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

      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!

      【讨论】:

      • 哇,我花了几个小时寻找一个 sass 解决方案,这是唯一对我有用的解决方案。我在 React 之上运行 Gatsby。
      • 如果您运行 Storybook,这可以工作,但构建失败! - github.com/storybookjs/storybook/issues/11052
      猜你喜欢
      • 2023-02-10
      • 2018-06-24
      • 2020-10-24
      • 1970-01-01
      • 2018-02-22
      • 2019-08-21
      • 2022-01-16
      • 2018-11-08
      • 2020-06-27
      相关资源
      最近更新 更多