reactjs - 配置多个next插件:withMDX、withBundleAnalyzer
问题描述
我使用next.config.js中已经附带的 tailwind 博客启动器创建了一个nextjswithBundleAnalyzer
站点。
我现在正试图让.mdx
文件直接从页面工作。文档说我需要withMDX
在我的 nextjs.config 文件中。怎么让两个人一起玩?我应该只保留其中一个吗?我已经安装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
},
})
)
)
解决方案
正如你提到的,你可以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;
},
});
推荐阅读
- jquery - 使用 if-else 语句在单击时隐藏显示 div 和添加删除类
- c# - 如何从 C# 的 Code Runner(VSCode 扩展)运行命令中删除文件路径?
- amazon-web-services - AWS Amplify 和 Elastic Beanstalk 之间的安全连接
- go - 元胞自动机的观察者模式或发布/订阅模式
- javascript - 从数组中给出对象值的每个属性
- javascript - 从对象数组中删除属性,内存影响
- flutter - 为什么视频在颤动的 VideoPlayer 中不是全屏的?
- javascript - 是否可以使用 GTM 替换 dataLayer 中的增强型电子商务类别?
- python - 如何在情节主题中设置宽度和高度?
- java - 如何将 MouseListener 添加到图像并使图像在 JPanel 中移动