使用Storybook

时间:2017-08-13 13:07:45

标签: reactjs webpack sass storybook

我目前面临着故事书的问题。使用webpack,我的应用程序中的一切都运行良好。故事书似乎与我的配置有关。

这是我的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]
    }
  }]
},

故事书在解析scss文件时遇到问题,我是否需要为Storybook创建一个特定的webpack.config.js来解决这个问题?

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

3 个答案:

答案 0 :(得分:8)

我只是通过添加一个与现有的webpack.config.js非常相似的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]'
          }
       }
     }
   ]
 }
}

答案 1 :(得分:4)

对于那些在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!

答案 2 :(得分:0)

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