File tree 6 files changed +59
-30
lines changed
examples/with-webpack-bundle-analyzer
6 files changed +59
-30
lines changed Original file line number Diff line number Diff line change @@ -11,21 +11,18 @@ curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2
11
11
cd with-webpack-bundle-analyzer
12
12
```
13
13
14
- Install it and run:
14
+ Install it
15
15
16
16
``` bash
17
17
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
25
18
```
26
19
27
20
## The idea behind the example
28
21
29
22
This example shows how to analyze the output bundles using [ webpack-bundle-analyzer] ( https://github.com/th0r/webpack-bundle-analyzer#as-plugin )
30
23
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 number Diff line number Diff line change 1
- const { BundleAnalyzerPlugin} = require ( 'webpack-bundle-analyzer' )
1
+ const { BundleAnalyzerPlugin } = require ( 'webpack-bundle-analyzer' )
2
+ const { ANALYZE } = process . env
3
+
2
4
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
+
15
14
return config
16
15
}
17
16
}
Original file line number Diff line number Diff line change 5
5
"dev" : " next" ,
6
6
"build" : " next build" ,
7
7
"start" : " next start" ,
8
- "bundle:view " : " webpack-bundle-analyzer . next/stats.json "
8
+ "analyze " : " cross-env ANALYZE=1 next build "
9
9
},
10
10
"dependencies" : {
11
11
"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"
15
17
},
16
18
"author" : " " ,
17
19
"license" : " ISC"
Original file line number Diff line number Diff line change
1
+ export default ( ) => (
2
+ < div >
3
+ This is the contact page.
4
+ </ div >
5
+ )
Original file line number Diff line number Diff line change
1
+ import React from 'react'
1
2
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
+ }
Original file line number Diff line number Diff line change @@ -15,7 +15,8 @@ export default class CombineAssetsPlugin {
15
15
if ( ! asset ) return
16
16
17
17
newSource += `${ asset . source ( ) } \n`
18
- delete compilation . assets [ name ]
18
+
19
+ // We keep existing assets since that helps when analyzing the bundle
19
20
} )
20
21
21
22
compilation . assets [ this . output ] = {
You can’t perform that action at this time.
0 commit comments