rsnext/packages/next-bundle-analyzer
vercel-release-bot 8fd2ad27b9 v13.4.20-canary.34
2023-09-18 06:25:35 +00:00
..
index.d.ts Allow configuring analyzerMode in bundle-analyzer (#47468) 2023-06-14 16:33:39 -07:00
index.js Allow configuring analyzerMode in bundle-analyzer (#47468) 2023-06-14 16:33:39 -07:00
package.json v13.4.20-canary.34 2023-09-18 06:25:35 +00:00
readme.md next-bundle-analyzer: openAnalyzer option (#36493) 2022-05-22 17:23:23 -05:00

Next.js + Webpack Bundle Analyzer

Use webpack-bundle-analyzer in your Next.js project

Installation

npm install @next/bundle-analyzer

or

yarn add @next/bundle-analyzer

Note: if installing as a devDependency make sure to wrap the require in a process.env check as next.config.js is loaded during next start as well.

Usage with environment variables

Create a next.config.js (and make sure you have next-bundle-analyzer set up)

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

Or configuration as a function:

module.exports = (phase, defaultConfig) => {
  return withBundleAnalyzer(defaultConfig)
}

Then you can run the command below:

# Analyze is done on build when env var is set
ANALYZE=true yarn build

When enabled two HTML files (client.html and server.html) will be outputted to <distDir>/analyze/. One will be for the server bundle, one for the browser bundle.

Options

To disable automatically opening the report in your default browser, set openAnalyzer to false:

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

Usage with next-compose-plugins

From version 2.0.0 of next-compose-plugins you need to call bundle-analyzer in this way to work

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

module.exports = withPlugins([
  [withBundleAnalyzer],
  // your other plugins here
])