npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

filesize-badger

v0.4.0

Published

Create file size badges. Also works for `rollup-plugin-filesize` usage.

Downloads

518

Readme

npm Dependencies devDependencies

testing badge coverage badge

Known Vulnerabilities Total Alerts Code Quality: Javascript

Licenses badge

(see also licenses for dev. deps.)

issuehunt-to-marktext

filesize-badger

Create file size badges. Also works for rollup-plugin-filesize usage.

Here is a badge generated against the dist file for this project.

filesize-badge

Install

npm i -D filesize-badger

Usage (independent of rollup-plugin-filesize)

import {filesizeBadger} from 'filesize-badger/dist/index.esm.js';

const file = 'dist/index.js';

// See also example below where these strings are obtained from
//  `getFilesizesForCode`.
const
  bundleSize = '4 Kb',
  brotliSize = '359 Kb',
  minSize = '1 Б',
  gzipSize = '500 B';

filesizeBadger({
  // REQUIRED

  // The (full) file path string of the file for which this badge is
  //  being made to indicate its size (for `rollup-plugin-filesize`, this
  //  will come from Rollup's output options)
  file,

  // These are all strings (for `rollup-plugin-filesize`, this will
  //   come from `info` gathered by it); one is required for each
  //   of `sizeTypes` (if any) unless obtained by `filesizeFormat`
  bundleSize,
  brotliSize,
  minSize,
  gzipSize,

  // This may be used in place of the above size types; will cause
  //   `file` to be retrieved, its sizes calculated, and formatting
  //   strings generated per `filesizeFormat` (see https://github.com/avoidwork/filesize.js#optional-settings )
  //   for its structure
  // filesizeFormat,

  // `fileName` is the file name portion of `file`; defaults to
  //    `basename(file)`
  // Commenting out as we can let this be auto-calculated
  // fileName,

  // OPTIONAL
  // (These are identically named to the corresponding options in
  //   `rollup-plugin-filesize`)
  showMinifiedSize: true,
  showGzippedSize: true,
  showBrotliSize: false,

  // For our own options which can also be used here, see the options for
  //  `rollupPluginFilesizeBadger` below.
  ...rollupPluginFilesizeBadgerOptions
});

You might find the built-in utility getFilesizesForCode helpful in obtaining the formatted strings (bundleSize, brotliSize, minSize, gzipSize) based on a string of code and desired configuration for separators, i18n, etc.

import {
  filesizeBadger, getFilesizesForCode
} from 'filesize-badger/dist/index.esm.js';

const file = 'dist/index.js';

(async () => {
const fileSizes = await getFilesizesForCode(
  // Could get this by using `fs.readFile` against `file`
  'someJSCodeToMeasure();',
  {
    // See https://github.com/avoidwork/filesize.js#optional-settings
    format: {},

    // AT LEAST ONE OF THESE SHOULD BE SET TO `true`
    // Whether `brotliSize`
    showBrotliSize: false,
    // Whether `minSize`
    showMinifiedSize: false,
    // Whether `gzipSize`
    showGzippedSize: false
  }
);

await filesizeBadger({
  file,
  ...fileSizes
  // And any other options
});
})();

Usage with rollup-plugin-filesize

You can pass in a call to rollupPluginFilesizeBadger for the reporter option in my reporter branch of rollup-plugin-filesize (adding "rollup-plugin-filesize": "https://github.com/brettz9/rollup-plugin-filesize#reporter-dist" as a dependency).

import fileSize from 'rollup-plugin-filesize';
import {rollupPluginFilesizeBadger} from 'filesize-badger/dist/index.esm.js';

export default {
  plugins: [
    fileSize({
      reporter: rollupPluginFilesizeBadger()
      // Or to also allow rollup-plugin-filesize default `boxen`
      //   console reporter
      // reporter: ['boxen', rollupPluginFilesizeBadger()]
    })
  ]
  // , ...
};

Options

You may also invoke with options (with defaults shown):

rollupPluginFilesizeBadger({
  outputPath: 'filesize-badge.svg',
  // Evaluable string-as-ES6 template (see `es6-template-strings`) to
  //  run once for the whole badge (the main panel); will be passed:
  //  `fileName`, `filePath`, `bundleSize`, `brotliSize`, `minSize`,
  //  `gzipSize`
  textTemplate: 'File size (${filePath})',
  // Badge-up color array (second color for stroke color)
  textColor: ['navy'],
  // Which size types (if any) to iterate within their own badge panel
  sizeTypes: ['bundleSize', 'brotliSize', 'minSize', 'gzipSize'],
  // Evaluable string-as-ES6 template (see `es6-template-strings`) to
  //  run once for each item of `sizeTypes`; will be passed:
  //  `fileName`, `filePath`, `sizeType` (human readable title for size),
  //   `size`
  sizeTemplate: '${sizeType}: ${size}',
  // Badge-up color arrays (second color for stroke color); one should
  //   exist for each item of `sizeTypes`
  sizeColors: [['orange'], ['blue'], ['green'], ['indigo']]
});

CLI usage

cli.svg

See also

  • mocha-badge-generator - Locally created badges for Mocha test results
  • coveradge - Locally-created badges for nyc/istanbul coverage
  • license-badger - Locally-created badges for indicating aggregate information on license types in use

To-dos

  1. Could potentially add code to make badges for package size