如何将 nextjs 与多个插件一起使用

How to use nextjs with multiple plugins

我将 Nextjs 与 with-antd 样板一起使用,它带有一个预配置的 next.config.js 文件。

看起来像这样;

/* eslint-disable */
const withCss = require('@zeit/next-css')

// fix: prevents error when .css files are required by node
if (typeof require !== 'undefined') {
  require.extensions['.css'] = (file) => {}
}

module.exports = withCss()

我想编辑这个配置文件并添加类似exportPathMap的配置。

像这样:

module.exports = {
  exportPathMap: function () {
    return {
      '/': { page: '/' },
      '/about': { page: '/about' },
      '/p/hello-nextjs': { page: '/post', query: { title: 'Hello Next.js' } },
      '/p/learn-nextjs': { page: '/post', query: { title: 'Learn Next.js is awesome' } },
      '/p/deploy-nextjs': { page: '/post', query: { title: 'Deploy apps with Zeit' } }
    }
  }
}

但我不知道如何在不破坏 withCss 插件的情况下实现它,请帮忙。

通过意识到我正在使用的 @zeit/next-css 期望更多的下一个配置作为对象从插件传递来解决这个问题。

来自 @zeit/next-css 插件的片段。

module.exports = (nextConfig = {}) => {
  return Object.assign({}, nextConfig, {
    webpack(config, options) {
      if (!options.defaultLoaders) {
        throw new Error(
          'This plugin is not compatible with Next.js versions below 5.0.0 https://err.sh/next-plugins/upgrade'
        )
}

所以我想通了,我将 exportPathMap 固定为 withCss 中的一个对象。

module.exports = withCss({
    exportPathMap: function() {
        return {
            '/': {page: '/'},
            '/sevices': {page: '/services'},
            '/about': {page: '/about'},
            '/contacts': {page: '/contacts'},
        }
    }
})

就是这样!