message: Added an attempt to load bundle size from bundlephobia instead of size-limit
diff: @@ -9,6 +9,7 @@ const https = require('https');
  const lightCodeTheme = require('prism-react-renderer/themes/github');
  const darkCodeTheme = require('prism-react-renderer/themes/dracula');
  const { default: pluginDocusaurus } = require('docusaurus-plugin-typedoc');
  +const { default: logger } = require('@docusaurus/logger');
  const versions = require('./versions.json');
  const sizeLimits = require('../.size-limit');
  @@ -22,10 +23,30 @@ const cacheDir = path.resolve(__dirname, './.previous-typings-cache/');
  const typedocWatch = process.env.TYPEDOC_WATCH === 'true';
  -const size = sizeLimits
  - .map(limit => parseFloat(limit.limit.split(' ')[0]))
  - .reduce((a, b) => Math.max(a, b))
  - .toFixed(1);
  +function httpGetJson(url) {
  + return new Promise((resolve, reject) => {
  + const request = https.get(url, response => {
  + if (response.statusCode && (response.statusCode < 100 || response.statusCode > 299)) {
  + reject(new Error(`Cannot load "${url}", error code=${response.statusCode}`));
  + return;
  + }
  +
  + let data = '';
  +
  + response.on('data', d => {
  + data += d;
  + });
  +
  + response.on('end', () => {
  + resolve(JSON.parse(data));
  + });
  + });
  +
  + request.on('error', error => {
  + reject(error);
  + });
  + });
  +}
  function downloadTypingsToFile(typingsFilePath, version) {
  return new Promise((resolve, reject) => {
  @@ -104,6 +125,22 @@ function typedocPluginForVersion(version) {
  });
  }
  +async function getConfig() {
  + let size = sizeLimits
  + .map(limit => parseFloat(limit.limit.split(' ')[0]))
  + .reduce((a, b) => Math.max(a, b));
  +
  + try {
  + // TODO: cache the result to avoid requesting the size every time on config change
  + // load the latest version every time
  + const bhResult = await httpGetJson(`https://bundlephobia.com/api/size?package=lightweight-charts@${versions[0] || 'latest'}&record=true`);
  +
  + logger.info(`The bundlephobia result has been loaded: version=${bhResult.version}, gzip=${bhResult.gzip}`);
  + size = bhResult.gzip / 1024;
  + } catch (e) {
  + logger.warn(`Cannot use size from bundlephobia, use size from size-limit instead, error=${e.toString()}`);
  + }
  +
  /** @type {import('@docusaurus/types').Config} */
  const config = {
  title: 'Lightweight Charts',
  @@ -134,7 +171,7 @@ const config = {
  ],
  customFields: {
  - bundleSize: size,
  + bundleSize: size.toFixed(0),
  },
  themeConfig:
  @@ -251,4 +288,7 @@ const config = {
  ],
  };
  -module.exports = config;
  + return config;
  +}
  +
  +module.exports = getConfig();
  