Skip to content

Commit b17ee4c

Browse files
committed
✨ Add analyze-bundles example
1 parent 5157621 commit b17ee4c

13 files changed

+210
-0
lines changed

examples/analyze-bundles/.eslintrc.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
extends: 'plugin:nullstack/recommended',
3+
rules: {
4+
'nullstack/prettier': [
5+
'warn',
6+
{
7+
// More options at https://prettier.io/docs/en/options
8+
trailingComma: 'all',
9+
tabWidth: 2,
10+
semi: false,
11+
singleQuote: true,
12+
printWidth: 80,
13+
},
14+
{
15+
usePrettierrc: false,
16+
},
17+
],
18+
},
19+
}

examples/analyze-bundles/.gitignore

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
package-lock.json
8+
yarn.lock
9+
pnpm-lock.yaml
10+
11+
# testing
12+
/coverage
13+
14+
# misc
15+
.DS_Store
16+
*.pem
17+
18+
# debug
19+
npm-debug.log*
20+
yarn-debug.log*
21+
yarn-error.log*
22+
.pnpm-debug.log*
23+
24+
# local env files
25+
.env
26+
27+
# vercel
28+
.vercel
29+
30+
# bundle folders
31+
.development/
32+
.production/

examples/analyze-bundles/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Analyzer Bundles example
2+
3+
This example shows how to analyze the output bundles using [webpack-bundle-analyzer](https://npmjs.com/package/webpack-bundle-analyzer)
4+
5+
## Analyze webpack output
6+
7+
To analyze your webpack output, invoke the following command:
8+
9+
```bash
10+
npm run analyze
11+
```
12+
13+
It will build the app, generate JSON stats files for both server and client bundles and then open them in the browser at `localhost` ports `8880` and `8881` respectively.
14+
15+
> Don't forget to select `Show content of concatenated modules` option on the sidebar and `Stat` is the most complete Treemap size
16+
17+
## Deploy your own
18+
19+
Deploy it now with [Vercel](https://vercel.com) or preview on [StackBlitz](https://stackblitz.com):
20+
21+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/GuiDevloper/nullstack-examples/tree/main/examples/analyze-bundles&project-name=analyze-bundles&repo-name=analyze-bundles&demo-title=Nullstack+analyzeBundles&demo-description=Nullstack+example+of+analyze+bundles&demo-url=https://github.com/GuiDevloper/nullstack-examples/tree/main/examples/analyze-bundles&demo-image=https://nullstack.app/image-1200x630.png)
22+
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/fork/github/GuiDevloper/nullstack-examples/tree/main/examples/analyze-bundles?title=Nullstack+AnalyzeBundles)
23+
24+
## How to use
25+
26+
Execute [`nulla create`](https://github.com/GuiDevloper/nulla) with [npm](https://docs.npmjs.com/cli/init):
27+
28+
```bash
29+
npx nulla create --example analyze-bundles analyze-bundles-app
30+
```
31+
32+
Then deploy it with [Vercel](https://github.com/GuiDevloper/nulla/blob/main/docs/en-US/deploy-vercel.md), [Heroku](https://github.com/GuiDevloper/nulla/blob/main/docs/en-US/deploy-heroku.md) or [Netlify](https://github.com/GuiDevloper/nulla/blob/main/docs/en-US/deploy-netlify.md).

examples/analyze-bundles/client.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Nullstack from 'nullstack'
2+
3+
import Application from './src/Application'
4+
5+
export default Nullstack.start(Application)

examples/analyze-bundles/package.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"private": true,
3+
"dependencies": {
4+
"@faker-js/faker": "^8.0.2"
5+
},
6+
"devDependencies": {
7+
"cross-env": "^7.0.3",
8+
"nullstack": "0.20.0",
9+
"webpack-bundle-analyzer": "^4.9.0"
10+
},
11+
"scripts": {
12+
"start": "npx nullstack start",
13+
"build": "npx nullstack build",
14+
"analyze": "cross-env ANALYZE=true npm run build",
15+
"vercel-build": "npm run build && npx nullstack-adapt-vercel"
16+
},
17+
"stackblitz": {
18+
"installDependencies": false,
19+
"startCommand": "npm i [email protected] -D && nullstack-adapt-babel && npm start",
20+
"compileTrigger": "save"
21+
}
22+
}
930 Bytes
Loading

examples/analyze-bundles/server.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Nullstack from 'nullstack'
2+
3+
import Application from './src/Application'
4+
5+
export default Nullstack.start(Application)
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const About = () => {
2+
return <div>About us</div>
3+
}
4+
5+
export default About
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
* {
2+
padding: 0;
3+
margin: 0;
4+
text-decoration: none;
5+
list-style: none;
6+
box-sizing: border-box;
7+
}
8+
9+
body {
10+
background-color: #111827;
11+
color: #fff;
12+
font-family: sans-serif;
13+
}
14+
15+
a {
16+
color: #f15d9f;
17+
margin-left: .3em;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import './Application.css'
2+
import Nullstack from 'nullstack'
3+
4+
import { faker } from '@faker-js/faker'
5+
6+
import About from './About'
7+
8+
// The name will be generated at build time only
9+
const name = faker.person.fullName()
10+
11+
class Application extends Nullstack {
12+
13+
render() {
14+
return (
15+
<main>
16+
<div route="/">
17+
<h1>Home Page</h1>
18+
<p>Welcome, {name}</p>
19+
<div>
20+
<a href="/about">About Page</a>
21+
</div>
22+
</div>
23+
<About route="/about" />
24+
</main>
25+
)
26+
}
27+
28+
}
29+
30+
export default Application
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const Contact = () => {
2+
return <div>This is the contact page.</div>
3+
}
4+
5+
export default Contact
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"strict": false,
4+
"noImplicitAny": false,
5+
"jsx": "preserve",
6+
"lib": ["ES2020", "DOM"]
7+
}
8+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const configs = require('nullstack/webpack.config')
2+
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
3+
4+
const bundlesTitles = ['server', 'client']
5+
function withBundleAnalyzer(config, id) {
6+
if (!process.env.ANALYZE) return config
7+
8+
const title = bundlesTitles[id]
9+
config.plugins.push(
10+
new BundleAnalyzerPlugin({
11+
generateStatsFile: true,
12+
statsFilename: `${title}.json`,
13+
reportTitle: title,
14+
analyzerPort: `888${id}`,
15+
analyzerHost: 'localhost',
16+
}),
17+
)
18+
return config
19+
}
20+
21+
function customConfigs() {
22+
return configs.map((originalConfig, id) => {
23+
return (...args) => {
24+
return withBundleAnalyzer(originalConfig(...args), id)
25+
}
26+
})
27+
}
28+
29+
module.exports = customConfigs()

0 commit comments

Comments
 (0)