2019-05-31 06:21:26 +00:00
|
|
|
import byteSize from 'byte-size'
|
|
|
|
import got from '../libs/got'
|
2019-11-18 15:52:07 +00:00
|
|
|
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
|
2019-05-31 06:21:26 +00:00
|
|
|
|
2019-11-18 15:52:07 +00:00
|
|
|
export default createBadgenHandler({
|
2019-05-31 06:21:26 +00:00
|
|
|
title: 'Bundlephobia',
|
|
|
|
examples: {
|
|
|
|
'/bundlephobia/min/react': 'minified',
|
|
|
|
'/bundlephobia/minzip/react': 'minified + gzip',
|
|
|
|
'/bundlephobia/minzip/@material-ui/core': '(scoped pkg) minified + gzip',
|
2019-11-18 15:52:07 +00:00
|
|
|
},
|
|
|
|
handlers: {
|
|
|
|
'/bundlephobia/:topic/:scope<@.*>/:name': handler,
|
|
|
|
'/bundlephobia/:topic/:name': handler,
|
2019-05-31 06:21:26 +00:00
|
|
|
}
|
2019-11-18 15:52:07 +00:00
|
|
|
})
|
2019-05-31 06:21:26 +00:00
|
|
|
|
|
|
|
// https://github.com/pastelsky/bundlephobia/issues/4
|
|
|
|
|
2019-11-18 15:52:07 +00:00
|
|
|
async function handler ({ topic, scope, name }: PathArgs) {
|
2019-05-31 06:21:26 +00:00
|
|
|
const pkg = scope ? `${scope}/${name}` : name
|
|
|
|
const endpoint = `https://bundlephobia.com/api/size?package=${pkg}`
|
2020-01-26 07:43:40 +00:00
|
|
|
const resp = await got(endpoint).json<any>()
|
2019-06-04 01:12:51 +00:00
|
|
|
|
|
|
|
if (!resp) {
|
|
|
|
return {
|
|
|
|
subject: 'bundlephobia',
|
|
|
|
status: 'unknown',
|
|
|
|
color: 'grey'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const { size, gzip } = resp
|
2019-05-31 06:21:26 +00:00
|
|
|
|
|
|
|
switch (topic) {
|
|
|
|
case 'min':
|
|
|
|
return {
|
|
|
|
subject: 'minified size',
|
|
|
|
status: byteSize(size, { units: 'iec' }).toString().replace(/iB\b/, 'B'),
|
|
|
|
color: 'blue'
|
|
|
|
}
|
|
|
|
case 'minzip':
|
|
|
|
return {
|
|
|
|
subject: 'minzipped size',
|
|
|
|
status: byteSize(gzip, { units: 'iec' }).toString().replace(/iB\b/, 'B'),
|
|
|
|
color: 'blue'
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return {
|
|
|
|
subject: 'bundlephobia',
|
|
|
|
status: 'unknown topic',
|
|
|
|
color: 'grey'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|