4.2.1 • Published 10 months ago

@node-cli/bundlesize v4.2.1

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

Node CLI Bundle Size package

npm

Bundlesize is a simple CLI tool that checks file(s) size and report if limits have been reached.

Installation

> npm install --dev @node-cli/bundlesize

Configuration

A configuration file must be provided via the -c parameter.

For the size option, it must export an object named "size" which is an array of objects with the following properties:

  • path: the path to the file to check
  • limit: the limit to check against

For the report option, it must export an object named "report" which is an object with the following properties:

  • header: the header to display (optional, string)
  • footer: the footer to display (optional, function receiving a boolean indicating if the limit has been reached, and a value corresponding to the diff. It must return a string)
  • previous: the previous path to the report to compare against (required, string)
  • current: the current path to the report to compare against (required, string)
  • columns: the columns to display (optional, array of objects)

Examples

Getting stats from files

Single file

export default {
	sizes: [
		{
			path: "dist/some-bundle.js",
			limit: "10 kB",
		},
	],
};

Multiple files

export default {
	sizes: [
		{
			path: "dist/some-bundle.js",
			limit: "10 kB",
		},
		{
			path: "dist/some-other-bundle.js",
			limit: "100 kB",
		},
	],
};

With glob patterns

export default {
	sizes: [
		{
			path: "dist/**/some-bundle.js",
			limit: "10 kB",
		},
		{
			path: "dist/**/some-other-bundle-*.js",
			limit: "100 kB",
		},
		{
			path: "dist/**/extra-+([a-zA-Z0-9]).js",
			limit: "100 kB",
		},
	],
};

With a hash

The special keyword <hash> can be used to match a hash in the filename. It cannot be used in conjunction with the single star (*) glob pattern, and it cannot be used if multiple files match the pattern.

NOTE: Using <hash> is equivalent to using +([a-zA-Z0-9]) in the glob pattern. However, the result will be indexed with the hash key instead of the match key, so that subsequent scripts can use the hash value.

StatusPatternComment
OKdist/**/some-bundle-<hash>.jsIf only one file matches the pattern
Not OKdist/**/some-bundle-<hash>.jsIf multiple files match the pattern
Not OKdist/**/some-bundle-<hash>.*Cannot use <hash> with *

With a version

The special keyword <semver> can be used to match a version in the filename. It cannot be used in conjunction with the single star (*) glob pattern, and it cannot be used if multiple files match the pattern.

NOTE: Using <semver> is equivalent to using * in the glob pattern. However, the result will be indexed with the semver key instead of the match key, so that subsequent scripts can use the semver value.

StatusPatternComment
OKdist/**/some-bundle-<semver>.jsIf only one file matches the pattern
Not OKdist/**/some-bundle-<semver>.jsIf multiple files match the pattern
Not OKdist/**/some-bundle-<semver>.*Cannot use <semver> with *

Printing reports from stats

Simple report

export default {
	report: {
		prev: "stats/previous.json",
		current: "stats/current.json",
	},
};

Simple report with custom header

export default {
	report: {
		header: "## My custom header",
		prev: "stats/previous.json",
		current: "stats/current.json",
	},
};

Simple report with custom footer

export default {
	report: {
		footer: (limitReached, diff) => {
			return `## My custom footer: ${limitReached} ${diff}`;
		},
		prev: "stats/previous.json",
		current: "stats/current.json",
	},
};

Simple report with custom columns

export default {
	report: {
		columns: [
			{ status: "Status" },
			{ file: "File" },
			{ size: "Size" },
			{ limits: "Limits" },
		],
		prev: "stats/previous.json",
		current: "stats/current.json",
	},
};

Usage

Print the stats at the command line

"scripts": {
	"stats": "bundlesize -c bundlesize.config.js"
}

Print the stats in a file

"scripts": {
	"stats": "bundlesize -c bundlesize.config.js -o stats.json"
}

Print the stats in a file but do not fail if the limit is reached

"scripts": {
  "stats": "bundlesize -c bundlesize.config.js -o stats.json -s"
}

Add a prefix to the stats

"scripts": {
  "stats": "bundlesize -c bundlesize.config.js -o stats.json -p 'My prefix'"
}

Use the current package version as a prefix

"scripts": {
  "stats": "bundlesize -c bundlesize.config.js -o stats.json -p \"$npm_package_version\""
}

Compare current stats with the previous ones

"scripts": {
	"stats": "bundlesize -c bundlesize.config.js --type report"
}

Get help

> bundlesize --help

License

MIT © Arno Versini

4.1.0

1 year ago

4.2.1

10 months ago

4.2.0

11 months ago

4.1.1

11 months ago

4.0.4

1 year ago

4.0.3

1 year ago

4.0.2

1 year ago

4.0.1

1 year ago

4.0.0

1 year ago

3.1.1

1 year ago

3.1.0

1 year ago

3.0.1

1 year ago

2.1.5

1 year ago

3.0.0

1 year ago

2.1.4

1 year ago

2.1.2

1 year ago

2.1.3

1 year ago

2.1.1

1 year ago

2.1.0

1 year ago

2.0.0

1 year ago

1.0.0

1 year ago

0.0.0

1 year ago