UNPKG

1.28 kBMarkdownView Raw
1# Next.js + Webpack Bundle Analyzer
2
3Use `webpack-bundle-analyzer` in your Next.js project
4
5## Installation
6
7```
8npm install @next/bundle-analyzer
9```
10
11or
12
13```
14yarn add @next/bundle-analyzer
15```
16
17### Usage with environment variables
18
19Create a next.config.js (and make sure you have next-bundle-analyzer set up)
20
21```js
22const withBundleAnalyzer = require('@next/bundle-analyzer')({
23 enabled: process.env.ANALYZE === 'true',
24})
25module.exports = withBundleAnalyzer({})
26```
27
28Or configuration as a function:
29
30```js
31module.exports = (phase, defaultConfig) => {
32 return withBundleAnalyzer(defaultConfig)
33}
34```
35
36Then you can run the command below:
37
38```bash
39# Analyze is done on build when env var is set
40ANALYZE=true yarn build
41```
42
43When 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.
44
45### Usage with next-compose-plugins
46
47From version 2.0.0 of next-compose-plugins you need to call bundle-analyzer in this way to work
48
49```js
50const withPlugins = require('next-compose-plugins')
51const withBundleAnalyzer = require('@next/bundle-analyzer')({
52 enabled: process.env.ANALYZE === 'true',
53})
54
55module.exports = withPlugins([
56 [withBundleAnalyzer],
57 // your other plugins here
58])
59```