{"id":15770391,"url":"https://github.com/benelan/build-sizes","last_synced_at":"2025-07-03T00:06:25.523Z","repository":{"id":57191233,"uuid":"460374793","full_name":"benelan/build-sizes","owner":"benelan","description":"A minimal script that provides build sizes to assist with optimization.","archived":false,"fork":false,"pushed_at":"2024-12-01T23:12:40.000Z","size":616,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-21T13:51:09.136Z","etag":null,"topics":["automation","cli","library","nodejs","npm-package","optimization","published","vanilla-javascript"],"latest_commit_sha":null,"homepage":"https://benelan.github.io/build-sizes/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/benelan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.MD","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-02-17T09:54:12.000Z","updated_at":"2024-12-01T23:11:18.000Z","dependencies_parsed_at":"2024-04-25T13:47:54.281Z","dependency_job_id":null,"html_url":"https://github.com/benelan/build-sizes","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/benelan/build-sizes","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benelan%2Fbuild-sizes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benelan%2Fbuild-sizes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benelan%2Fbuild-sizes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benelan%2Fbuild-sizes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/benelan","download_url":"https://codeload.github.com/benelan/build-sizes/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benelan%2Fbuild-sizes/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261279913,"owners_count":23134895,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["automation","cli","library","nodejs","npm-package","optimization","published","vanilla-javascript"],"created_at":"2024-10-04T14:41:02.729Z","updated_at":"2025-07-03T00:06:25.250Z","avatar_url":"https://github.com/benelan.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Build sizes\n\nA small script that provides build sizes to assist with optimization.\n\n\u003cbr\u003e\n\u003cdiv align=\"center\"\u003e\n    \u003cb\u003e🚀 Zero dependencies! 🚀\u003c/b\u003e\n\u003c/div\u003e\n\n## Installation\n\nInstall [the package](https://www.npmjs.com/package/build-sizes) globally and\nuse it anywhere:\n\n```sh\nnpm i -g build-sizes\n```\n\nUse it in a single application:\n\n```sh\nnpm i -D build-sizes\n```\n\nOr try it out before installing:\n\n```sh\nnpx build-sizes your/build/directory\n```\n\n\u003cbr\u003e\n\n## Using the CLI\n\nTo run the script, you need to provide the path (absolute or relative) to the\napplication's build directory. For example, `create-react-app` generates a\ndirectory named `build` for production (other common names include `dist` and\n`public`). After building the application, you can get the sizes by running the\nfollowing from the application's root directory:\n\n```sh\nbuild-sizes build\n```\n\nAnd the output to the console is:\n\n```sh\n-----------------------------\n|\u003e Application Build Sizes \u003c|\n-----------------------------\nBuild\n --\u003e file count: 419\n --\u003e size: 27.73 MB\n-----------------------------\nMain JS bundle\n --\u003e name: main.6e924e92.js\n --\u003e size: 1.70 MB\n --\u003e gzip size: 462.92 KB\n --\u003e brotli size: 375.26 KB\n-----------------------------\n```\n\n\u003e **NOTE:** The \"main bundle\" file is naively chosen by largest file size, it\n\u003e doesn't use a dependency graph. This script is minimal by design, check out\n\u003e [`webpack-bundle-analyzer`](https://github.com/webpack-contrib/webpack-bundle-analyzer)\n\u003e for a more complex bundle size tool.\n\nFlags are provided to change the default options. For example, you can specify a\nfiletype for the largest bundle size (default is \"js\"):\n\n```sh\nbuild-sizes dist --filetype=css\n```\n\nThe argument parsing logic is very simple and requires the equals sign (`=`)\nbetween the flag and its value. Additionally, short flags cannot be grouped\ntogether into a single argument. Here are a couple examples of what will and\nwon't work:\n\n```sh\n# These two are incorrect\nbuild-sizes dist -lb           # -l and -b flags combined into a single argument\nbuild-sizes dist --decimals 4  # space between --decimals flag and its value\n\n# This one is correct\nbuild-sizes dist -l -b --decimals=4\n```\n\nThe `-h` or `--help` flag will log usage information to the console, copy/pasted\nhere for convenience:\n\n\u003cdetails\u003e\n  \u003csummary\u003eUsage info\u003c/summary\u003e\n\n### Arguments\n\n**path [required]**\n\n- Path to the build directory\n\n### Options\n\n**-l, --loader [boolean]**\n\n- Show a loading animation while determining the build size\n\n**-b, --binary [boolean]**\n\n- Convert bytes to human readable format in base 2 instead of base 10\n\n**-d, --decimals**\n\n- Number of decimal places for rounding bytes to a human readable format\n  (default is 2)\n\n**-f, --filetype**\n\n- Filetype of the main bundle (default is js)\n\n**-o, --outfile**\n\n- Path to a file for saving build sizes as CSV data\n\n**-p, --path [required]**\n\n- Path to the build directory (also available as argument)\n\n### Examples\n\n- Simplest usage with sane defaults\n\n  ```sh\n  build-sizes dist\n  ```\n\n- Size of the largest css file with tweaked number formatting\n\n  ```sh\n  build-sizes dist --filetype=css --binary --decimals=1\n  ```\n\n- Same as above, but use a flag for path when it's not the first argument\n\n  ```sh\n  build-sizes -f=css -b -d=1 -p=dist\n  ```\n\n- Save the build sizes to a csv\n\n  ```sh\n  build-sizes dist --outfile=data/build-sizes.csv\n  ```\n\n\u003c/details\u003e\n\n\u003cbr\u003e\n\n### Running from an npm script\n\nPro tip: you can view the sizes after every build by adding a `postbuild` npm\nscript:\n\n```diff\n \"scripts\": {\n    \"start\": \"react-scripts start\",\n    \"build\": \"react-scripts build\",\n+   \"postbuild\": \"build-sizes build\",\n    \"test\": \"react-scripts test\",\n    \"eject\": \"react-scripts eject\"\n  },\n...\n```\n\nThe sizes will be logged to the console after running `npm run build`.\n\n\u003cbr\u003e\n\n## Using the functions\n\nThe package also exports functions,\n[documented here](https://benelan.github.io/build-sizes/global.html). They are\navailable as both CommonJS and ECMAScript modules. Check out the\n[CLI code](https://github.com/benelan/build-sizes/blob/master/src/cli.js) for a\nsimple usage example that logs build sizes to the console. Here is another usage\nexample that saves your project's build sizes, version, and a timestamp to a CSV\nfile.\n\n```js\nimport { appendFile, readFile, writeFile } from \"fs/promises\";\nimport { getBuildSizes } from \"build-sizes\";\n\nconst ARGUMENT_ERROR = `Two required arguments (in order):\n    - path to the build directory\n    - path of the output csv file`;\n\n(async () =\u003e {\n  try {\n    const [build, outfile] = process.argv.splice(2);\n    if (!build || !outfile) throw new Error(ARGUMENT_ERROR);\n\n    const sizes = await getBuildSizes(build);\n    const version = JSON.parse(await readFile(\"package.json\", \"utf8\")).version;\n\n    // convert build-sizes output into csv header and row\n    const header = [\"Version\", \"Timestamp\", ...Object.keys(sizes)]\n      .join(\",\")\n      .concat(\"\\n\");\n\n    const row = [version, Date.now(), ...Object.values(sizes)]\n      .join(\",\")\n      .concat(\"\\n\");\n\n    try {\n      // write csv header if outfile doesn't exist\n      await writeFile(outfile, header, { flag: \"wx\" });\n    } catch (err) {\n      // don't throw error if outfile does exists\n      if (err.code !== \"EEXIST\") {\n        throw new Error(err);\n      }\n    }\n    // append build size info to csv\n    await appendFile(outfile, row);\n  } catch (err) {\n    console.error(err);\n    process.exit(1);\n  }\n})();\n```\n\nYou can use the example by providing the paths to the build directory and output\nCSV file:\n\n```sh\nnode save.js dist sizes.csv\n```\n\nYou could even add it as a `postpublish` script to keep track of your build\nsizes for each release! As a matter of fact, scratch that I'm adding it to the\npackage 🚀\n\nUse the `-o` or `--outfile` flag to specify the CSV file's location:\n\n```diff\n \"scripts\": {\n    \"publish\": \"... \u0026\u0026 npm publish\",\n+   \"postpublish\": \"build-sizes dist -o=sizes.csv\",\n    ...\n```\n\nThe `saveBuildSizes` function is also exported, so you can use it in your\nscripts!\n\n\u003e **Note:** The save script requires the current working directory to contain\n\u003e `package.json` so it can grab the project's version number. I recommend using\n\u003e an npm script like the snippet above, which allows you to run the script from\n\u003e any directory in the project.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenelan%2Fbuild-sizes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbenelan%2Fbuild-sizes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenelan%2Fbuild-sizes/lists"}