【问题标题】:Configure multiple next plugins: withMDX, withBundleAnalyzer配置多个next插件:withMDX、withBundleAnalyzer
【发布时间】:2021-04-18 04:18:16
【问题描述】:

我用一个顺风博客启动器创建了一个 nextjs 站点,该启动器在 next.config.js 中已经带有 withBundleAnalyzer

我现在正试图让.mdx 文件直接从页面工作。文档说我在 nextjs.config 文件中需要withMDX。怎么让两个人一起玩?我应该只保留其中一个吗?我已经安装了next-compose-plugins,但不知道如何设置。

更新

这是我的下一个配置文件;它仍然失败。错误:

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
TypeError: undefined is not a function
    at _withPlugins (/home/xxx/xxx/nodeapps/my-web/node_modules/next-compose-plugins/lib/index.js:17:22)
    at Object.<anonymous> (/home/xxx/xxx/nodeapps/my-web/next.config.js:11:18)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at loadConfig (/home/xxx/xxx/nodeapps/my-web/node_modules/next/dist/next-server/server/config.js:8:94)
    at async NextServer.loadConfig (/home/xxx/xxx/nodeapps/my-web/node_modules/next/dist/server/next.js:1:2962)
const withPlugins = require('next-compose-plugins')

const withMDX = require('@next/mdx')({
  extension: /\.mdx$/,
})

const withBundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: process.env.ANALYZE === 'true',
})

module.exports = withPlugins(
  withMDX(
    withBundleAnalyzer({
      pageExtensions: ['js', 'jsx', 'md', 'mdx'],
      future: {
        webpack5: true,
      },
      webpack: (config, { dev, isServer }) => {
        config.module.rules.push({
          test: /\.(png|jpe?g|gif|mp4)$/i,
          use: [
            {
              loader: 'file-loader',
              options: {
                publicPath: '/_next',
                name: 'static/media/[name].[hash].[ext]',
              },
            },
          ],
        })

        config.module.rules.push({
          test: /\.svg$/,
          use: ['@svgr/webpack'],
        })

        if (!dev && !isServer) {
          // Replace React with Preact only in client production build
          Object.assign(config.resolve.alias, {
            react: 'preact/compat',
            'react-dom/test-utils': 'preact/test-utils',
            'react-dom': 'preact/compat',
          })
        }

        return config
      },
    })
  )
)

【问题讨论】:

    标签: reactjs next.js mdxjs


    【解决方案1】:

    正如你提到的,你可以像这样使用next-compose-plugins

    const withPlugins = require('next-compose-plugins');
    const withMDX = require('@next/mdx');
    const withBundleAnalyzer = require('@next/bundle-analyzer')({
      enabled: process.env.ANALYZE === 'true',
    });
    
    module.exports = withPlugins([
     
      // add a plugin with specific configuration
      [withMDX, {
       // MDX plugin specific options
      }],
     
      // add it like this if no plugin configuration is needed
      [withBundleAnalyzer],
    ]);
    
    

    编辑: 这是您的完整配置文件:

    const withPlugins = require("next-compose-plugins");
    
    const withMDX = require("@next/mdx")({
        extension: /\.mdx?$/,
    });
    const withBundleAnalyzer = require("@next/bundle-analyzer")({
        enabled: process.env.ANALYZE === "true",
    });
    module.exports = withPlugins([[withBundleAnalyzer], [withMDX]], {
        pageExtensions: ["js", "jsx", "md", "mdx"],
        future: {
            webpack5: true,
        },
        webpack: (config, { dev, isServer }) => {
            config.module.rules.push({
                test: /\.(png|jpe?g|gif|mp4)$/i,
                use: [
                    {
                        loader: "file-loader",
                        options: {
                            publicPath: "/_next",
                            name: "static/media/[name].[hash].[ext]",
                        },
                    },
                ],
            });
    
            config.module.rules.push({
                test: /\.svg$/,
                use: ["@svgr/webpack"],
            });
    
            if (!dev && !isServer) {
                // Replace React with Preact only in client production build
                Object.assign(config.resolve.alias, {
                    react: "preact/compat",
                    "react-dom/test-utils": "preact/test-utils",
                    "react-dom": "preact/compat",
                });
            }
    
            return config;
        },
    });
    
    
    

    【讨论】:

    • 嗨,谢谢。他们都需要webpack: 选项吗?现在 withBundleAnalyzer 块已经有 webpack 部分,它提到了各种与降价相关的东西。我想我会把它留在原地并测试。将报告...
    • 好的。我得到:``` ./pages/test.mdx Module parse failed: Unexpected token (1:0) 你可能需要一个合适的加载器来处理这个文件类型,目前没有配置加载器来处理这个文件。见webpack.js.org/concepts#loaders```
    【解决方案2】:

    您也可以在不使用next-compose-plugins 的情况下设置配置。

    const withMDX = require('@next/mdx')({
      extension: /\.mdx$/,
    })
    const withBundleAnalyzer = require('@next/bundle-analyzer')({
        enabled: process.env.ANALYZE === 'true',
    })
    
    module.exports = withMDX(withBundleAnalyzer({
        // Your Next.js configs, including withMDX-specific options
    }))
    

    【讨论】:

    • 我已经尝试了您的方法并将结果发布在我的问题中。我什至重新安装了 node_modules 但似乎无法克服这个错误......
    • 显然我有阅读理解能力......这就是我最终做的事情,哈哈!
    猜你喜欢
    • 2019-06-08
    • 2021-05-12
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 2021-06-06
    • 2022-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多