File size: 1,790 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# 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)
```js
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
})
module.exports = withBundleAnalyzer({})
```
Or configuration as a function:
```js
module.exports = (phase, defaultConfig) => {
return withBundleAnalyzer(defaultConfig)
}
```
Then you can run the command below:
```bash
# Analyze is done on build when env var is set
ANALYZE=true yarn build
```
When enabled three HTML files (client.html, edge.html and nodejs.html) will be outputted to `<distDir>/analyze/`. One will be for the nodejs server bundle, one for the edge server bundle, and one for the browser bundle.
#### Options
To disable automatically opening the report in your default browser, set `openAnalyzer` to false:
```js
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
```js
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
])
```
|