5

我正在尝试通过使用https://www.npmjs.com/package/@next/bundle-analyzer来减小我网站的捆绑包大小

此刻,当我npm analyze

"analyze": "cross-env ANALYZE=true next build",

它没有输出具有所需可视化效果的 html 文件。

这是我的next.config.js

const withPWA = require('next-pwa')
const runtimeCaching = require('next-pwa/cache')


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

module.exports = withPWA({
  pwa: {
    dest: 'public',
    runtimeCaching,
  },
  poweredByHeader: false,
},
withBundleAnalyzer(),

)

使用这个nextjs-analyze-app-bundle 教程

出了什么问题?

4

2 回答 2

2

看起来这已经在 Vercel 的问题板上得到了回答。在这里复制他们的解决方案:

这些插件是增强配置对象的函数,因此您必须将它们包装起来,而不是将它们作为参数提供:

const withPWA = require('next-pwa')
const runtimeCaching = require('next-pwa/cache')


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

module.exports = withBundleAnalyzer(withPWA({
    pwa: {
        dest: 'public',
        runtimeCaching,
    },
    poweredByHeader: false,
}));
于 2021-03-10T15:56:38.900 回答
0

在我这样做之前,

module.exports = withBundleAnalyzer(
  withPWA({
    pwa: {
      dest: 'public',
      runtimeCaching,
    },
    poweredByHeader: false,
  })
)

module.exports = 
  {
    env: {
      BASE_URL: process.env.BASE_URL,
    },
    future: {
      webpack5: true,
    },
    reactStrictMode: true,
  }

不确定,但我认为你应该只需要一个module.exports,所以我把我的其他东西包裹在withBundleAnalyzer这样的里面

module.exports = withBundleAnalyzer(
  withPWA({
    pwa: {
      dest: 'public',
      runtimeCaching,
    },
    poweredByHeader: false,
  }),
  {
    env: {
      BASE_URL: process.env.BASE_URL,
    },
    future: {
      webpack5: true,
    },
    reactStrictMode: true,
  }
)
于 2021-06-14T11:46:48.733 回答