DEV Community

Ramu Narasinga
Ramu Narasinga

Posted on

“pretty-bytes” usage in unbuild

For this week, I have been reading unbuild source code and found few packages that I have never seen before or used. I wanted to share some interesting packages that are used in these OSS projects so we can learn a thing or two ;)

The following are discussed in this article:

  1. What is pretty-bytes?

  2. pretty-bytes usage in unbuild

Image description

What is pretty-bytes?

pretty-bytes is written by Sindre. It converts bytes to a human readable string: 1337 → 1.34 kB.

Install

npm install pretty-bytes
Enter fullscreen mode Exit fullscreen mode

Usage

import prettyBytes from 'pretty-bytes';

prettyBytes(1337);
//=> '1.34 kB'

prettyBytes(100);
//=> '100 B'

// Display with units of bits
prettyBytes(1337, {bits: true});
//=> '1.34 kbit'

// Display file size differences
prettyBytes(42, {signed: true});
//=> '+42 B'

// Localized output using German locale
prettyBytes(1337, {locale: 'de'});
//=> '1,34 kB'
Enter fullscreen mode Exit fullscreen mode

pretty-bytes usage in unbuild

pretty-bytes is found to be at line 10 in build.ts.

import prettyBytes from "pretty-bytes";
Enter fullscreen mode Exit fullscreen mode

Then at line 332, you will find this below code:

let line =
      `  ${colors.bold(rPath(entry.path))} (` +
      [
        totalBytes && `total size: ${colors.cyan(prettyBytes(totalBytes))}`,
        entry.bytes && `chunk size: ${colors.cyan(prettyBytes(entry.bytes))}`,
        entry.exports?.length &&
          `exports: ${colors.gray(entry.exports.join(", "))}`,
      ]
        .filter(Boolean)
        .join(", ") +
      ")";
Enter fullscreen mode Exit fullscreen mode

pretty-bytes is applied to get human readable string, in this case, it is used to report total size, chunk size.

About me:

Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.

I am open to work on interesting projects. Send me an email at ramu.narasinga@gmail.com

My Github — https://github.com/ramu-narasinga

My website — https://ramunarasinga.com

My Youtube channel — https://www.youtube.com/@thinkthroo

Learning platform — https://thinkthroo.com

Codebase Architecture — https://app.thinkthroo.com/architecture

Best practices — https://app.thinkthroo.com/best-practices

Production-grade projects — https://app.thinkthroo.com/production-grade-projects

References

  1. https://www.npmjs.com/package/pretty-bytes

  2. https://github.com/unjs/unbuild/blob/main/src/build.ts#L10

  3. https://github.com/unjs/unbuild/blob/main/src/build.ts#L335

Top comments (0)