Skip to content

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
benelan committed Feb 17, 2022
1 parent c2d400a commit 133fd63
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 21 deletions.
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Build sizes

A small script which provides sizes of a production build.

## Installation

Install the package globally and use it anywhere:

```bash
npm i -g build-sizes
```

Or use it in a single application:

```bash
npm i -D build-sizes
```

## Usage

The run the script, you need to provide the path to the application's build directory. For example, `create-react-app` creates a directory named `build` for production. Once the production build completes, you can get the sizes by running:

```bash
build-sizes build
```

And the output to the console is:

```
-------------------------------
|-> Application Build Sizes <-|
-------------------------------
Main bundle size: 1.62 MB
On-disk size: 26.45 MB
On-disk files: 419
-------------------------------
```

Other common names frameworks use for production are `dist` and `public`.

### Running from an NPM script

You can get the sizes after every build by adding a `postbuild` NPM script:

```diff
...
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
+ "postbuild": "build-sizes build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
...
```

The sizes will be logged to the console after running `npm run build`.

### Using the function

The package also exports an asynchronous function. To use the function:

```js
const { getBuildSizes } = require("build-sizes");

(async () => {
try {
const buildSizes = await getBuildSizes("dist/prod");
console.log(buildSizes)
...
} catch (err) {
console.error(err);
process.exitCode = 1;
}
})();
```

#### Parameters

The function requires one parameter.

| Parameter | Description | Type |
| --------- | -------------------------------------------------------------------------------- | ------ |
| buildPath | path from the current working directory (`$PWD`) to the sample's build directory | string |

#### Return properties

The function returns an object with three properties.

| Property | Description | Type |
| -------------- | ------------------------------------------ | ------ |
| mainBundleSize | size of the largest JavaScript bundle file | number |
| buildSize | size of all files in the build directory | number |
| fileCount | count of all files in the build directory | number |
13 changes: 6 additions & 7 deletions bin/build-sizes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
const { getBuildSize } = require("../index.js");
const { getBuildSizes } = require("../index.js");

/**
* Emphasizes a message in the console
Expand All @@ -12,18 +12,17 @@ const logHeader = (message) => {

(async () => {
try {
const [buildPath, samplePath] = process.argv.splice(2);
const [buildPath] = process.argv.splice(2);

if (!buildPath) {
throw new Error(
"Error: Invalid or missing arguments. The path to the build directory is a required."
"Error: Invalid or missing arguments. The path from the current working directory to the production build directory is a required."
);
}

const { mainBundleSize, buildSize, buildFileCount } = await getBuildSize({
buildPath,
samplePath,
});
const { mainBundleSize, buildSize, buildFileCount } = await getBuildSizes(
buildPath
);

const headerText = "Application Build Sizes";
logHeader(headerText);
Expand Down
16 changes: 5 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,15 @@ const convertBytes = (bytes) => {
};

/**
* Takes an object containing build information and returns an object containing build sizes.
* The build sizes are logged when the script is ran via CLI.
* @param {{samplePath: string, buildPath: string}}
* - samplePath - relative path to the sample's root directory ("$PWD" default)
* - buildPath - relative path from samplePath to the build directory
* Provides sizes for an application's production build
* @param {string} buildPath - path from the current working directory to the build directory
* @returns {Promise<{ mainBundleSize: string, buildSize:string , buildFileCount: string}>}
* - mainBundleSize - size in megabytes of the largest JavaScript bundle file
* - buildSize - size in megabytes of all files in the build directory
* - buildFileCount - count of all files in the build directory
*/
const getBuildSize = async ({ samplePath, buildPath }) => {
const sample = !!samplePath
? resolve(__dirname, samplePath)
: process.env.PWD;
const build = resolve(sample, buildPath);
const getBuildSizes = async (buildPath) => {
const build = resolve(process.env.PWD, buildPath);

const buildFiles = await getFiles(build);

Expand All @@ -90,4 +84,4 @@ const getBuildSize = async ({ samplePath, buildPath }) => {
return { mainBundleSize, buildSize, buildFileCount };
};

module.exports = { getBuildSize };
module.exports = { getBuildSizes };
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "build-sizes",
"version": "1.0.0",
"description": "Provides build size for your application",
"version": "2.0.0",
"description": "Provides build sizes for your application",
"keywords": [
"metrics",
"size",
Expand All @@ -13,7 +13,8 @@
"name": "Ben Elan",
"url": "https://benelan.dev/"
},
"main": "index.js",
"main": "./index.js",
"exports": "./index.js",
"bin": {
"build-sizes": "bin/build-sizes.js"
},
Expand All @@ -30,5 +31,9 @@
},
"bugs": {
"url": "https://github.com/benelan/build-sizes/issues"
},
"volta": {
"node": "16.14.0",
"npm": "8.5.0"
}
}

0 comments on commit 133fd63

Please sign in to comment.