nextjs加入antd按需加载配置方法

.babelrc文件下,按照antd文档按需加载加入;

{

    "presets": ["next/babel"],

  "plugins": [

    [  "import",

        {

          "libraryName": "antd",

          "style": "css"

        }]

  ]

  }

next.config.js文件里面加入

webpack(config, { isServer }) {

    if (isServer) {

      const antStyles = /antd\/.*?\/style\/css.*?/

      const origExternals = [...config.externals]

      config.externals = [

        (context, request, callback) => {

          if (request.match(antStyles)) return callback()

          if (typeof origExternals[0] === 'function') {

            origExternals[0](context, request, callback)

          } else {

            callback()

          }

        },

        ...(typeof origExternals[0] === 'function' ? [] : origExternals),

      ]

      config.module.rules.unshift({

        test: antStyles,

        use: 'null-loader',

      })

    }

    return config

  }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容