“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: What is pretty-bytes? pretty-bytes usage in unbuild 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 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' pretty-bytes usage in unbuild pretty-bytes is found to be at line 10 in build.ts. import prettyBytes from "pretty-bytes"; 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(", ") + ")"; 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 https://www.npmjs.com/package/pretty-bytes https://github.com/unjs/unbuild/blob/main/src/build.ts#L10 https://github.com/unjs/unbuild/blob/main/src/build.ts#L335

Jan 10, 2025 - 08:54
 0
“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

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'

pretty-bytes usage in unbuild

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

import prettyBytes from "pretty-bytes";

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(", ") +
      ")";

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