Skip to content

Commit 9eaac2a

Browse files
authored
feat(detail): show bundled size of a library (#740)
* feat(detail): show bundled size of a library the default size is visible in Usage, and the gzip size on hovering. The link goes to bundlesize closes algolia/npm-search#91 cc @pastelsky. I added `yarn website; ${navigator.userAgent}` as the `User-Agent` header to the request. * chore: make size text i18n ready * chore: change User-Agent to X-Bundlephobia-User
1 parent 6aa5c67 commit 9eaac2a

File tree

7 files changed

+57
-8
lines changed

7 files changed

+57
-8
lines changed

_data/i18n/en.yml

+2
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ script:
362362
versions: Versions
363363
display_all: Display all
364364
hide: Hide
365+
bundlesize: Size in browser
366+
bundlesize_text: 'size: {size}, gzip: {gzip}'
365367

366368
not_found:
367369
whoa: Whoa, {package_name} does not exist yet

assets/detail/ico-download-size.svg

+3
Loading

js/src/lib/Details/Aside.js

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const Aside = ({
2727
versions,
2828
version,
2929
devDependencies,
30+
bundlesize,
3031
onOpenFileBrowser,
3132
}) => (
3233
<aside className="details-side col-lg-4">
@@ -51,6 +52,7 @@ const Aside = ({
5152
<Usage
5253
dependencies={dependencies}
5354
devDependencies={devDependencies}
55+
bundlesize={bundlesize}
5456
{...packageJSONLink({ githubRepo })}
5557
/>
5658
<Tags tags={tags} name={name} />

js/src/lib/Details/Usage.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ const deps = ({ dependencies, title }) => {
3535
const Usage = ({
3636
dependencies,
3737
devDependencies,
38-
packageJSONLink,
3938
versions,
39+
bundlesize,
40+
packageJSONLink,
4041
}) => (
4142
<article className="details-side--usage">
4243
<h1>{window.i18n.detail.usage}</h1>
@@ -66,6 +67,24 @@ const Usage = ({
6667
}
6768
/>
6869
)}
70+
{bundlesize && (
71+
<Di
72+
icon="download-size"
73+
title={window.i18n.detail.bundlesize}
74+
description={
75+
<a
76+
target="_blank"
77+
rel="noopener noreferrer"
78+
href={bundlesize.href}
79+
title={window.i18n.detail.bundlesize_text
80+
.replace('{size}', bundlesize.size)
81+
.replace('{gzip}', bundlesize.gzip)}
82+
>
83+
{bundlesize.size}
84+
</a>
85+
}
86+
/>
87+
)}
6988
</dl>
7089
</article>
7190
);

js/src/lib/Details/index.js

+27-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { Component } from 'react';
2+
import bytes from 'bytes';
23
import algoliasearch from 'algoliasearch/lite';
34

45
import Aside from './Aside';
@@ -55,7 +56,7 @@ class Details extends Component {
5556
};
5657
}
5758

58-
componentWillMount() {
59+
componentDidMount() {
5960
index
6061
.getObject(this.props.objectID)
6162
.then(content => {
@@ -118,17 +119,37 @@ class Details extends Component {
118119
}
119120

120121
this.getGithub({
121-
url: `repos/${this.state.githubRepo.user}/${this.state.githubRepo
122-
.project}/stats/commit_activity`,
122+
url: `repos/${this.state.githubRepo.user}/${
123+
this.state.githubRepo.project
124+
}/stats/commit_activity`,
123125
state: 'activity',
124126
});
125127

126128
this.getGithub({
127-
url: `repos/${this.state.githubRepo.user}/${this.state.githubRepo
128-
.project}`,
129+
url: `repos/${this.state.githubRepo.user}/${
130+
this.state.githubRepo.project
131+
}`,
129132
state: 'github',
130133
});
131134
}
135+
136+
const { name, version } = this.state;
137+
138+
get({
139+
url: `https://bundlephobia.com/api/size?package=${name}@${version}`,
140+
type: 'json',
141+
headers: {
142+
'X-Bundlephobia-User': 'yarn website',
143+
},
144+
}).then(res =>
145+
this.setState({
146+
bundlesize: {
147+
href: `https://bundlephobia.com/result?p=${name}@${version}`,
148+
size: bytes(res.size),
149+
gzip: bytes(res.gzip),
150+
},
151+
})
152+
);
132153
}
133154

134155
maybeRenderReadme() {
@@ -252,6 +273,7 @@ class Details extends Component {
252273
versions={this.state.versions}
253274
version={this.state.version}
254275
tags={this.state.tags}
276+
bundlesize={this.state.bundlesize}
255277
onOpenFileBrowser={this._openFileBrowser}
256278
/>
257279
);

js/src/lib/schema.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default {
1212
github: {
1313
stargazers_count: 0,
1414
},
15+
bundlesize: undefined,
1516
githubRepo: {
1617
user: '',
1718
project: '',

js/src/lib/util.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ const status = res =>
197197
}
198198
});
199199

200-
export const get = ({ url, type }) =>
201-
fetch(url)
200+
export const get = ({ url, type, headers }) =>
201+
fetch(url, { headers })
202202
.then(status)
203203
.then(res => res[type]())
204204
.catch(err => {

0 commit comments

Comments
 (0)