Storybook doesn't understand import on demand for antd components

后端 未结 3 973
鱼传尺愫
鱼传尺愫 2021-01-18 02:49

I have followed instructions here to get antd working fine with CRA. But while using it from storybook, I was getting an <

3条回答
  •  暖寄归人
    2021-01-18 03:15

    If you are using AntD Advanced-Guides for React and storybook v5 create .storybook/webpack.config.js with the following:

    const path = require('path');
    
    module.exports = async ({ config, mode }) => {
    
      config.module.rules.push({
          loader: 'babel-loader',
          exclude: /node_modules/,
          test: /\.(js|jsx)$/,
          options: {
              presets: ['@babel/react'],
              plugins: [
                  ['import', {
                    libraryName: 'antd',
                    libraryDirectory: 'es',
                    style: true
                  }]
              ]
          },
      });
    
      config.module.rules.push({
          test: /\.less$/,
          loaders: [
              'style-loader',
              'css-loader',
              {
                  loader: 'less-loader',
                  options: {
                      modifyVars: {'@primary-color': '#f00'},
                      javascriptEnabled: true
                  }
              }
          ],
          include: [
            path.resolve(__dirname, '../src'),
            /[\\/]node_modules[\\/].*antd/
          ]
      });
    
      return config;
    };
    

    Then you can use import { Button } from 'antd' to import antd components.

提交回复
热议问题