Skip to content

Commit 23c2f02

Browse files
authored
Make it easy and meaningful to analyze the bundle. (vercel#2393)
1 parent 12e8b12 commit 23c2f02

File tree

6 files changed

+59
-30
lines changed

6 files changed

+59
-30
lines changed

examples/with-webpack-bundle-analyzer/README.md

+6-9
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,18 @@ curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2
1111
cd with-webpack-bundle-analyzer
1212
```
1313

14-
Install it and run:
14+
Install it
1515

1616
```bash
1717
npm install
18-
npm run dev
19-
```
20-
21-
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
22-
23-
```bash
24-
now
2518
```
2619

2720
## The idea behind the example
2821

2922
This example shows how to analyze the output bundles using [webpack-bundle-analyzer](https://github.com/th0r/webpack-bundle-analyzer#as-plugin)
3023

31-
To view the stats use `npm run bundle:view`
24+
To analyze your webpack output, invoke the following command:
25+
26+
```bash
27+
npm run analyze
28+
```
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer')
1+
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
2+
const { ANALYZE } = process.env
3+
24
module.exports = {
3-
webpack: (config, { dev }) => {
4-
// Perform customizations to config
5-
config.plugins.push(
6-
new BundleAnalyzerPlugin({
7-
analyzerMode: 'disabled',
8-
// For all options see https://github.com/th0r/webpack-bundle-analyzer#as-plugin
9-
generateStatsFile: true,
10-
// Will be available at `.next/stats.json`
11-
statsFilename: 'stats.json'
12-
})
13-
)
14-
// Important: return the modified config
5+
webpack: function (config) {
6+
if (ANALYZE) {
7+
config.plugins.push(new BundleAnalyzerPlugin({
8+
analyzerMode: 'server',
9+
analyzerPort: 8888,
10+
openAnalyzer: true
11+
}))
12+
}
13+
1514
return config
1615
}
1716
}

examples/with-webpack-bundle-analyzer/package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
"dev": "next",
66
"build": "next build",
77
"start": "next start",
8-
"bundle:view": "webpack-bundle-analyzer .next/stats.json"
8+
"analyze": "cross-env ANALYZE=1 next build"
99
},
1010
"dependencies": {
1111
"next": "latest",
12-
"react": "^15.4.2",
13-
"react-dom": "^15.4.2",
14-
"webpack-bundle-analyzer": "^2.3.0"
12+
"cross-env": "^5.0.1",
13+
"faker": "^4.1.0",
14+
"react": "^15.6.1",
15+
"react-dom": "^15.6.1",
16+
"webpack-bundle-analyzer": "^2.8.2"
1517
},
1618
"author": "",
1719
"license": "ISC"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default () => (
2+
<div>
3+
This is the contact page.
4+
</div>
5+
)
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
1+
import React from 'react'
12
import Link from 'next/link'
2-
export default () => (
3-
<div>Hello World. <Link href='/about'><a>About</a></Link></div>
4-
)
3+
4+
export default class Index extends React.Component {
5+
static getInitialProps ({ req }) {
6+
if (req) {
7+
// Runs only in the server
8+
const faker = require('faker')
9+
const name = faker.name.findName()
10+
return { name }
11+
}
12+
13+
// Runs only in the client
14+
return { name: 'Arunoda' }
15+
}
16+
17+
render () {
18+
const { name } = this.props
19+
return (
20+
<div>
21+
<h1>Home Page</h1>
22+
<p>Welcome, {name}</p>
23+
<div>
24+
<Link href='/about'><a>About Page</a></Link>
25+
</div>
26+
</div>
27+
)
28+
}
29+
}

server/build/plugins/combine-assets-plugin.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export default class CombineAssetsPlugin {
1515
if (!asset) return
1616

1717
newSource += `${asset.source()}\n`
18-
delete compilation.assets[name]
18+
19+
// We keep existing assets since that helps when analyzing the bundle
1920
})
2021

2122
compilation.assets[this.output] = {

0 commit comments

Comments
 (0)