@node-cli/bundlesize 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Arno Versini
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # Node CLI Bundle Size package
2
+
3
+ ![npm](https://img.shields.io/npm/v/@node-cli/bundlesize?label=version&logo=npm)
4
+
5
+ > Bundlesize is a simple CLI tool that checks file(s) size and report if limits have been reached.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ > npm install --dev @node-cli/bundlesize
11
+ ```
12
+
13
+ ## Examples
14
+
15
+ Assuming there is a `bundlesize.config.js` file in the root of the project with the following content:
16
+
17
+ ```js
18
+ export default [
19
+ {
20
+ path: "dist/some-bundle.js",
21
+ limit: "10 kB",
22
+ },
23
+ ];
24
+ ```
25
+
26
+ ### Print the results at the command line
27
+
28
+ ```json
29
+ "scripts": {
30
+ "stats": "bundlesize -c bundlesize.config.js"
31
+ }
32
+ ```
33
+
34
+ ### Print the results in a file
35
+
36
+ ```json
37
+ "scripts": {
38
+ "stats": "bundlesize -c bundlesize.config.js -o stats.json"
39
+ }
40
+ ```
41
+
42
+ ### Print the results in a file but do not fail if the limit is reached
43
+
44
+ ```json
45
+ "scripts": {
46
+ "stats": "bundlesize -c bundlesize.config.js -o stats.json -s"
47
+ }
48
+ ```
49
+
50
+ ### Add a prefix to the results
51
+
52
+ ```json
53
+ "scripts": {
54
+ "stats": "bundlesize -c bundlesize.config.js -o stats.json -p 'My prefix'"
55
+ }
56
+ ```
57
+
58
+ ### Use the current package version as a prefix
59
+
60
+ ```json
61
+ "scripts": {
62
+ "stats": "bundlesize -c bundlesize.config.js -o stats.json -p \"$npm_package_version\""
63
+ }
64
+ ```
65
+
66
+ ## Get help
67
+
68
+ ```sh
69
+ > bundlesize --help
70
+ ```
71
+
72
+ ## License
73
+
74
+ MIT © Arno Versini
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env node
2
+ import { Logger } from "@node-cli/logger";
3
+ import bytes from "bytes";
4
+ import { config } from "./parse.js";
5
+ import fs from "fs-extra";
6
+ import { gzipSizeFromFileSync } from "gzip-size";
7
+ import path from "node:path";
8
+ import { statSync } from "node:fs";
9
+ const STDOUT = "stdout";
10
+ const CWD = process.cwd();
11
+ const flags = config.flags;
12
+ const configurationFile = path.join(CWD, flags.configuration);
13
+ const outputFile = flags.output === "" || flags.output === undefined ? STDOUT : path.join(CWD, flags.output);
14
+ const prefix = flags.prefix || Date.now().toString();
15
+ const resultsMap = new Map();
16
+ const log = new Logger({
17
+ boring: flags.boring
18
+ });
19
+ let failed = false;
20
+ if (flags.configuration === "") {
21
+ log.error("Please provide a configuration file");
22
+ process.exit(0);
23
+ }
24
+ if (fs.existsSync(configurationFile) === false) {
25
+ log.error("Invalid or missing configuration file!");
26
+ process.exit(0);
27
+ }
28
+ try {
29
+ const configuration = await import(configurationFile).then((m)=>m.default);
30
+ for (const artifact of configuration){
31
+ const file = path.join(path.dirname(configurationFile), artifact.path);
32
+ const fileSize = statSync(file).size;
33
+ const fileSizeGzip = gzipSizeFromFileSync(file);
34
+ const passed = fileSizeGzip < bytes(artifact.limit);
35
+ if (passed === false) {
36
+ failed = true;
37
+ }
38
+ resultsMap.set(artifact.path, {
39
+ path: file,
40
+ fileSize,
41
+ fileSizeGzip,
42
+ limit: artifact.limit,
43
+ passed
44
+ });
45
+ }
46
+ const results = [
47
+ ...resultsMap.values()
48
+ ];
49
+ let existingResults = {};
50
+ if (outputFile !== STDOUT) {
51
+ try {
52
+ existingResults = fs.readJsonSync(outputFile);
53
+ } catch {
54
+ // nothing to declare officer
55
+ }
56
+ }
57
+ existingResults[prefix] = results;
58
+ if (outputFile !== STDOUT) {
59
+ fs.outputJsonSync(outputFile, existingResults, {
60
+ spaces: 2
61
+ });
62
+ }
63
+ if (outputFile === STDOUT) {
64
+ log.info(`Configuration: ${flags.configuration}`);
65
+ log.info(`Output: ${outputFile}`);
66
+ log.info(`Output prefix: ${prefix}`);
67
+ log.log(`\n${JSON.stringify(results, undefined, 2)}`);
68
+ }
69
+ } catch (error) {
70
+ log.error(error);
71
+ process.exit(0);
72
+ }
73
+ if (failed && flags.silent === false) {
74
+ process.exit(1);
75
+ }
76
+
77
+ //# sourceMappingURL=bundlesize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/bundlesize.ts"],"sourcesContent":["#!/usr/bin/env node\n\nimport { Logger } from \"@node-cli/logger\";\nimport bytes from \"bytes\";\nimport { config } from \"./parse.js\";\nimport fs from \"fs-extra\";\nimport { gzipSizeFromFileSync } from \"gzip-size\";\nimport path from \"node:path\";\nimport { statSync } from \"node:fs\";\n\nconst STDOUT = \"stdout\";\nconst CWD = process.cwd();\n\nconst flags = config.flags;\nconst configurationFile = path.join(CWD, flags.configuration);\nconst outputFile =\n\tflags.output === \"\" || flags.output === undefined\n\t\t? STDOUT\n\t\t: path.join(CWD, flags.output);\nconst prefix = flags.prefix || Date.now().toString();\nconst resultsMap = new Map();\nconst log = new Logger({\n\tboring: flags.boring,\n});\n\nlet failed = false;\n\nif (flags.configuration === \"\") {\n\tlog.error(\"Please provide a configuration file\");\n\tprocess.exit(0);\n}\n\nif (fs.existsSync(configurationFile) === false) {\n\tlog.error(\"Invalid or missing configuration file!\");\n\tprocess.exit(0);\n}\n\ntry {\n\tconst configuration = await import(configurationFile).then((m) => m.default);\n\n\tfor (const artifact of configuration) {\n\t\tconst file = path.join(path.dirname(configurationFile), artifact.path);\n\t\tconst fileSize = statSync(file).size;\n\t\tconst fileSizeGzip = gzipSizeFromFileSync(file);\n\t\tconst passed = fileSizeGzip < bytes(artifact.limit);\n\n\t\tif (passed === false) {\n\t\t\tfailed = true;\n\t\t}\n\n\t\tresultsMap.set(artifact.path, {\n\t\t\tpath: file,\n\t\t\tfileSize,\n\t\t\tfileSizeGzip,\n\t\t\tlimit: artifact.limit,\n\t\t\tpassed,\n\t\t});\n\t}\n\n\tconst results = [...resultsMap.values()];\n\tlet existingResults = {};\n\tif (outputFile !== STDOUT) {\n\t\ttry {\n\t\t\texistingResults = fs.readJsonSync(outputFile);\n\t\t} catch {\n\t\t\t// nothing to declare officer\n\t\t}\n\t}\n\texistingResults[prefix] = results;\n\tif (outputFile !== STDOUT) {\n\t\tfs.outputJsonSync(outputFile, existingResults, { spaces: 2 });\n\t}\n\n\tif (outputFile === STDOUT) {\n\t\tlog.info(`Configuration: ${flags.configuration}`);\n\t\tlog.info(`Output: ${outputFile}`);\n\t\tlog.info(`Output prefix: ${prefix}`);\n\t\tlog.log(`\\n${JSON.stringify(results, undefined, 2)}`);\n\t}\n} catch (error) {\n\tlog.error(error);\n\tprocess.exit(0);\n}\n\nif (failed && flags.silent === false) {\n\tprocess.exit(1);\n}\n"],"names":["Logger","bytes","config","fs","gzipSizeFromFileSync","path","statSync","STDOUT","CWD","process","cwd","flags","configurationFile","join","configuration","outputFile","output","undefined","prefix","Date","now","toString","resultsMap","Map","log","boring","failed","error","exit","existsSync","then","m","default","artifact","file","dirname","fileSize","size","fileSizeGzip","passed","limit","set","results","values","existingResults","readJsonSync","outputJsonSync","spaces","info","JSON","stringify","silent"],"mappings":";AAEA,SAASA,MAAM,QAAQ,mBAAmB;AAC1C,OAAOC,WAAW,QAAQ;AAC1B,SAASC,MAAM,QAAQ,aAAa;AACpC,OAAOC,QAAQ,WAAW;AAC1B,SAASC,oBAAoB,QAAQ,YAAY;AACjD,OAAOC,UAAU,YAAY;AAC7B,SAASC,QAAQ,QAAQ,UAAU;AAEnC,MAAMC,SAAS;AACf,MAAMC,MAAMC,QAAQC,GAAG;AAEvB,MAAMC,QAAQT,OAAOS,KAAK;AAC1B,MAAMC,oBAAoBP,KAAKQ,IAAI,CAACL,KAAKG,MAAMG,aAAa;AAC5D,MAAMC,aACLJ,MAAMK,MAAM,KAAK,MAAML,MAAMK,MAAM,KAAKC,YACrCV,SACAF,KAAKQ,IAAI,CAACL,KAAKG,MAAMK,MAAM;AAC/B,MAAME,SAASP,MAAMO,MAAM,IAAIC,KAAKC,GAAG,GAAGC,QAAQ;AAClD,MAAMC,aAAa,IAAIC;AACvB,MAAMC,MAAM,IAAIxB,OAAO;IACtByB,QAAQd,MAAMc,MAAM;AACrB;AAEA,IAAIC,SAAS;AAEb,IAAIf,MAAMG,aAAa,KAAK,IAAI;IAC/BU,IAAIG,KAAK,CAAC;IACVlB,QAAQmB,IAAI,CAAC;AACd;AAEA,IAAIzB,GAAG0B,UAAU,CAACjB,uBAAuB,OAAO;IAC/CY,IAAIG,KAAK,CAAC;IACVlB,QAAQmB,IAAI,CAAC;AACd;AAEA,IAAI;IACH,MAAMd,gBAAgB,MAAM,MAAM,CAACF,mBAAmBkB,IAAI,CAAC,CAACC,IAAMA,EAAEC,OAAO;IAE3E,KAAK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@@ -0,0 +1,5 @@
1
+ export declare const defaultFlags: {
2
+ boring: boolean;
3
+ configuration: string;
4
+ silent: boolean;
5
+ };
@@ -0,0 +1,7 @@
1
+ export const defaultFlags = {
2
+ boring: false,
3
+ configuration: "",
4
+ silent: false
5
+ };
6
+
7
+ //# sourceMappingURL=defaults.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/defaults.ts"],"sourcesContent":["export const defaultFlags = {\n\tboring: false,\n\tconfiguration: \"\",\n\tsilent: false,\n};\n"],"names":["defaultFlags","boring","configuration","silent"],"mappings":"AAAA,OAAO,MAAMA,eAAe;IAC3BC,QAAQ;IACRC,eAAe;IACfC,QAAQ;AACT,EAAE"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
4
+
5
+ export * from "./bundlesize";
6
+ export * from "./defaults";
7
+ export * from "./parse";
@@ -0,0 +1,15 @@
1
+ export type Flags = {
2
+ boring?: boolean;
3
+ help?: boolean;
4
+ version?: boolean;
5
+ configuration?: string;
6
+ output?: string;
7
+ prefix?: string;
8
+ silent?: boolean;
9
+ };
10
+ export type Configuration = {
11
+ flags?: Flags;
12
+ usage?: boolean;
13
+ showHelp?: () => void;
14
+ };
15
+ export declare const config: Configuration;
package/dist/parse.js ADDED
@@ -0,0 +1,49 @@
1
+ import { defaultFlags } from "./defaults.js";
2
+ import { parser } from "@node-cli/parser";
3
+ /* istanbul ignore next */ export const config = parser({
4
+ meta: import.meta,
5
+ examples: [],
6
+ flags: {
7
+ configuration: {
8
+ shortFlag: "c",
9
+ description: "Specify a configuration file",
10
+ type: "string"
11
+ },
12
+ output: {
13
+ shortFlag: "o",
14
+ description: "Specify the output file",
15
+ type: "string"
16
+ },
17
+ prefix: {
18
+ shortFlag: "p",
19
+ description: "Specify a prefix to use in the output file",
20
+ type: "string"
21
+ },
22
+ silent: {
23
+ shortFlag: "s",
24
+ default: defaultFlags.silent,
25
+ description: "Do not exit in error when a limit is exceeded",
26
+ type: "boolean"
27
+ },
28
+ boring: {
29
+ shortFlag: "b",
30
+ default: defaultFlags.boring,
31
+ description: "Do not use color output",
32
+ type: "boolean"
33
+ },
34
+ help: {
35
+ shortFlag: "h",
36
+ description: "Display help instructions",
37
+ type: "boolean"
38
+ },
39
+ version: {
40
+ shortFlag: "v",
41
+ description: "Output the current version",
42
+ type: "boolean"
43
+ }
44
+ },
45
+ usage: true,
46
+ defaultFlags
47
+ });
48
+
49
+ //# sourceMappingURL=parse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/parse.ts"],"sourcesContent":["import { defaultFlags } from \"./defaults.js\";\nimport { parser } from \"@node-cli/parser\";\n\nexport type Flags = {\n\tboring?: boolean;\n\thelp?: boolean;\n\tversion?: boolean;\n\tconfiguration?: string;\n\toutput?: string;\n\tprefix?: string;\n\tsilent?: boolean;\n};\n\nexport type Configuration = {\n\tflags?: Flags;\n\tusage?: boolean;\n\tshowHelp?: () => void;\n};\n\n/* istanbul ignore next */\nexport const config: Configuration = parser({\n\tmeta: import.meta,\n\texamples: [],\n\tflags: {\n\t\tconfiguration: {\n\t\t\tshortFlag: \"c\",\n\t\t\tdescription: \"Specify a configuration file\",\n\t\t\ttype: \"string\",\n\t\t},\n\t\toutput: {\n\t\t\tshortFlag: \"o\",\n\t\t\tdescription: \"Specify the output file\",\n\t\t\ttype: \"string\",\n\t\t},\n\t\tprefix: {\n\t\t\tshortFlag: \"p\",\n\t\t\tdescription: \"Specify a prefix to use in the output file\",\n\t\t\ttype: \"string\",\n\t\t},\n\t\tsilent: {\n\t\t\tshortFlag: \"s\",\n\t\t\tdefault: defaultFlags.silent,\n\t\t\tdescription: \"Do not exit in error when a limit is exceeded\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\tboring: {\n\t\t\tshortFlag: \"b\",\n\t\t\tdefault: defaultFlags.boring,\n\t\t\tdescription: \"Do not use color output\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\thelp: {\n\t\t\tshortFlag: \"h\",\n\t\t\tdescription: \"Display help instructions\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t\tversion: {\n\t\t\tshortFlag: \"v\",\n\t\t\tdescription: \"Output the current version\",\n\t\t\ttype: \"boolean\",\n\t\t},\n\t},\n\tusage: true,\n\tdefaultFlags,\n});\n"],"names":["defaultFlags","parser","config","meta","examples","flags","configuration","shortFlag","description","type","output","prefix","silent","default","boring","help","version","usage"],"mappings":"AAAA,SAASA,YAAY,QAAQ,gBAAgB;AAC7C,SAASC,MAAM,QAAQ,mBAAmB;AAkB1C,wBAAwB,GACxB,OAAO,MAAMC,SAAwBD,OAAO;IAC3CE,MAAM;IACNC,UAAU,EAAE;IACZC,OAAO;QACNC,eAAe;YACdC,WAAW;YACXC,aAAa;YACbC,MAAM;QACP;QACAC,QAAQ;YACPH,WAAW;YACXC,aAAa;YACbC,MAAM;QACP;QACAE,QAAQ;YACPJ,WAAW;YACXC,aAAa;YACbC,MAAM;QACP;QACAG,QAAQ;YACPL,WAAW;YACXM,SAASb,aAAaY,MAAM;YAC5BJ,aAAa;YACbC,MAAM;QACP;QACAK,QAAQ;YACPP,WAAW;YACXM,SAASb,aAAac,MAAM;YAC5BN,aAAa;YACbC,MAAM;QACP;QACAM,MAAM;YACLR,WAAW;YACXC,aAAa;YACbC,MAAM;QACP;QACAO,SAAS;YACRT,WAAW;YACXC,aAAa;YACbC,MAAM;QACP;IACD;IACAQ,OAAO;IACPjB;AACD,GAAG"}
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@node-cli/bundlesize",
3
+ "version": "0.0.0",
4
+ "license": "MIT",
5
+ "author": "Arno Versini",
6
+ "description": "Simple CLI tool that checks file(s) size and report if limits have been reached",
7
+ "type": "module",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": "./dist/bundlesize.js",
10
+ "bin": "dist/bundlesize.js",
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "node": ">=18",
15
+ "scripts": {
16
+ "build": "yarn run clean && yarn run build:types && yarn run build:js && yarn run build:barrel",
17
+ "build:barrel": "barrelsby --delete --directory dist --pattern \"**/*.d.ts\" --name \"index.d\"",
18
+ "build:js": "swc --source-maps --out-dir dist src",
19
+ "build:types": "tsc",
20
+ "clean": "rimraf dist types coverage",
21
+ "lint": "prettier --write \"src/*.ts\" && eslint --fix \"src/*.ts\"",
22
+ "stats": "bundlesize -c bundlesize.config.js",
23
+ "watch": "swc --watch --out-dir dist src"
24
+ },
25
+ "dependencies": {
26
+ "@node-cli/logger": ">=1.2.0",
27
+ "@node-cli/parser": ">=2.2.1",
28
+ "bytes": "3.1.2",
29
+ "fs-extra": "11.2.0",
30
+ "gzip-size": "7.0.0"
31
+ },
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "gitHead": "128145d5b6fe5e3f142343cb494466700d85071c"
36
+ }