@node-cli/bundlesize 1.0.0 → 2.1.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.
@@ -3,6 +3,7 @@ import { Logger } from "@node-cli/logger";
3
3
  import bytes from "bytes";
4
4
  import { config } from "./parse.js";
5
5
  import fs from "fs-extra";
6
+ import { glob } from "glob";
6
7
  import { gzipSizeFromFileSync } from "gzip-size";
7
8
  import path from "node:path";
8
9
  import { statSync } from "node:fs";
@@ -12,7 +13,7 @@ const flags = config.flags;
12
13
  const configurationFile = path.join(CWD, flags.configuration);
13
14
  const outputFile = flags.output === "" || flags.output === undefined ? STDOUT : path.join(CWD, flags.output);
14
15
  const prefix = flags.prefix || Date.now().toString();
15
- const resultsMap = new Map();
16
+ const currentResults = {};
16
17
  const log = new Logger({
17
18
  boring: flags.boring
18
19
  });
@@ -28,24 +29,32 @@ if (fs.existsSync(configurationFile) === false) {
28
29
  try {
29
30
  const configuration = await import(configurationFile).then((m)=>m.default);
30
31
  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;
32
+ const rootPath = artifact.path.startsWith("/") ? "" : path.dirname(configurationFile);
33
+ const file = path.join(rootPath, artifact.path);
34
+ const files = glob.sync(file);
35
+ if (files.length === 0) {
36
+ log.error(`File not found: ${file}`);
37
+ process.exit(flags.silent === false ? 1 : 0);
38
+ }
39
+ for (const file of files){
40
+ const fileSize = statSync(file).size;
41
+ const fileSizeGzip = gzipSizeFromFileSync(file);
42
+ const passed = fileSizeGzip < bytes(artifact.limit);
43
+ if (passed === false) {
44
+ failed = true;
45
+ }
46
+ let index = file.replace(rootPath, "");
47
+ if (!artifact.path.startsWith("/") && index.startsWith("/")) {
48
+ index = index.slice(1);
49
+ }
50
+ currentResults[index] = {
51
+ fileSize,
52
+ fileSizeGzip,
53
+ limit: artifact.limit,
54
+ passed
55
+ };
37
56
  }
38
- resultsMap.set(artifact.path, {
39
- path: file,
40
- fileSize,
41
- fileSizeGzip,
42
- limit: artifact.limit,
43
- passed
44
- });
45
57
  }
46
- const results = [
47
- ...resultsMap.values()
48
- ];
49
58
  let existingResults = {};
50
59
  if (outputFile !== STDOUT) {
51
60
  try {
@@ -54,7 +63,7 @@ try {
54
63
  // nothing to declare officer
55
64
  }
56
65
  }
57
- existingResults[prefix] = results;
66
+ existingResults[prefix] = currentResults;
58
67
  if (outputFile !== STDOUT) {
59
68
  fs.outputJsonSync(outputFile, existingResults, {
60
69
  spaces: 2
@@ -64,7 +73,7 @@ try {
64
73
  log.info(`Configuration: ${flags.configuration}`);
65
74
  log.info(`Output: ${outputFile}`);
66
75
  log.info(`Output prefix: ${prefix}`);
67
- log.log(`\n${JSON.stringify(results, undefined, 2)}`);
76
+ log.log(`\n${JSON.stringify(currentResults, undefined, 2)}`);
68
77
  }
69
78
  } catch (error) {
70
79
  log.error(error);
@@ -1 +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,WAA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B,GAAG,CAACR,SAAS5B,IAAI,EAAE;YAC7BA,MAAM6B;YACNE;YACAE;YACAE,OAAOP,SAASO,KAAK;YACrBD;QACD;IACD;IAEA,MAAMG,UAAU;WAAIpB,WAAWqB,MAAM;KAAG;IACxC,IAAIC,kBAAkB,CAAC;IACvB,IAAI7B,eAAeR,QAAQ;QAC1B,IAAI;YACHqC,kBAAkBzC,GAAG0C,YAAY,CAAC9B;QACnC,EAAE,OAAM;QACP,6BAA6B;QAC9B;IACD;IACA6B,eAAe,CAAC1B,OAAO,GAAGwB;IAC1B,IAAI3B,eAAeR,QAAQ;QAC1BJ,GAAG2C,cAAc,CAAC/B,YAAY6B,iBAAiB;YAAEG,QAAQ;QAAE;IAC5D;IAEA,IAAIhC,eAAeR,QAAQ;QAC1BiB,IAAIwB,IAAI,CAAC,CAAC,eAAe,EAAErC,MAAMG,aAAa,CAAC,CAAC;QAChDU,IAAIwB,IAAI,CAAC,CAAC,QAAQ,EAAEjC,WAAW,CAAC;QAChCS,IAAIwB,IAAI,CAAC,CAAC,eAAe,EAAE9B,OAAO,CAAC;QACnCM,IAAIA,GAAG,CAAC,CAAC,EAAE,EAAEyB,KAAKC,SAAS,CAACR,SAASzB,WAAW,GAAG,CAAC;IACrD;AACD,EAAE,OAAOU,OAAO;IACfH,IAAIG,KAAK,CAACA;IACVlB,QAAQmB,IAAI,CAAC;AACd;AAEA,IAAIF,UAAUf,MAAMwC,MAAM,KAAK,OAAO;IACrC1C,QAAQmB,IAAI,CAAC;AACd"}
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 { glob } from \"glob\";\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 currentResults = {};\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 rootPath = artifact.path.startsWith(\"/\")\n\t\t\t? \"\"\n\t\t\t: path.dirname(configurationFile);\n\t\tconst file = path.join(rootPath, artifact.path);\n\t\tconst files = glob.sync(file);\n\n\t\tif (files.length === 0) {\n\t\t\tlog.error(`File not found: ${file}`);\n\t\t\tprocess.exit(flags.silent === false ? 1 : 0);\n\t\t}\n\n\t\tfor (const file of files) {\n\t\t\tconst fileSize = statSync(file).size;\n\t\t\tconst fileSizeGzip = gzipSizeFromFileSync(file);\n\t\t\tconst passed = fileSizeGzip < bytes(artifact.limit);\n\n\t\t\tif (passed === false) {\n\t\t\t\tfailed = true;\n\t\t\t}\n\t\t\tlet index = file.replace(rootPath, \"\");\n\t\t\tif (!artifact.path.startsWith(\"/\") && index.startsWith(\"/\")) {\n\t\t\t\tindex = index.slice(1);\n\t\t\t}\n\n\t\t\tcurrentResults[index] = {\n\t\t\t\tfileSize,\n\t\t\t\tfileSizeGzip,\n\t\t\t\tlimit: artifact.limit,\n\t\t\t\tpassed,\n\t\t\t};\n\t\t}\n\t}\n\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] = currentResults;\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(currentResults, 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","glob","gzipSizeFromFileSync","path","statSync","STDOUT","CWD","process","cwd","flags","configurationFile","join","configuration","outputFile","output","undefined","prefix","Date","now","toString","currentResults","log","boring","failed","error","exit","existsSync","then","m","default","artifact","rootPath","startsWith","dirname","file","files","sync","length","silent","fileSize","size","fileSizeGzip","passed","limit","index","replace","slice","existingResults","readJsonSync","outputJsonSync","spaces","info","JSON","stringify"],"mappings":";AAEA,SAASA,MAAM,QAAQ,mBAAmB;AAC1C,OAAOC,WAAW,QAAQ;AAC1B,SAASC,MAAM,QAAQ,aAAa;AACpC,OAAOC,QAAQ,WAAW;AAC1B,SAASC,IAAI,QAAQ,OAAO;AAC5B,SAASC,oBAAoB,QAAQ,YAAY;AACjD,OAAOC,UAAU,YAAY;AAC7B,SAASC,QAAQ,QAAQ,UAAU;AAEnC,MAAMC,SAAS;AACf,MAAMC,MAAMC,QAAQC,GAAG;AAEvB,MAAMC,QAAQV,OAAOU,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,iBAAiB,CAAC;AACxB,MAAMC,MAAM,IAAIxB,OAAO;IACtBy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;IACD;IAEA,IAAIK,kBAAkB,CAAC;IACvB,IAAIlC,eAAeR,QAAQ;QAC1B,IAAI;YACH0C,kBAAkB/C,GAAGgD,YAAY,CAACnC;QACnC,EAAE,OAAM;QACP,6BAA6B;QAC9B;IACD;IACAkC,eAAe,CAAC/B,OAAO,GAAGI;IAC1B,IAAIP,eAAeR,QAAQ;QAC1BL,GAAGiD,cAAc,CAACpC,YAAYkC,iBAAiB;YAAEG,QAAQ;QAAE;IAC5D;IAEA,IAAIrC,eAAeR,QAAQ;QAC1BgB,IAAI8B,IAAI,CAAC,CAAC,eAAe,EAAE1C,MAAMG,aAAa,CAAC,CAAC;QAChDS,IAAI8B,IAAI,CAAC,CAAC,QAAQ,EAAEtC,WAAW,CAAC;QAChCQ,IAAI8B,IAAI,CAAC,CAAC,eAAe,EAAEnC,OAAO,CAAC;QACnCK,IAAIA,GAAG,CAAC,CAAC,EAAE,EAAE+B,KAAKC,SAAS,CAACjC,gBAAgBL,WAAW,GAAG,CAAC;IAC5D;AACD,EAAE,OAAOS,OAAO;IACfH,IAAIG,KAAK,CAACA;IACVjB,QAAQkB,IAAI,CAAC;AACd;AAEA,IAAIF,UAAUd,MAAM6B,MAAM,KAAK,OAAO;IACrC/B,QAAQkB,IAAI,CAAC;AACd"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@node-cli/bundlesize",
3
- "version": "1.0.0",
3
+ "version": "2.1.0",
4
4
  "license": "MIT",
5
5
  "author": "Arno Versini",
6
6
  "description": "Simple CLI tool that checks file(s) size and report if limits have been reached",
@@ -27,10 +27,11 @@
27
27
  "@node-cli/parser": ">=2.2.1",
28
28
  "bytes": "3.1.2",
29
29
  "fs-extra": "11.2.0",
30
+ "glob": "10.3.10",
30
31
  "gzip-size": "7.0.0"
31
32
  },
32
33
  "publishConfig": {
33
34
  "access": "public"
34
35
  },
35
- "gitHead": "e756a3b74a22b5668fa4ba87025eccfcc3645472"
36
+ "gitHead": "2ced1c1d13328fab153e028d9c2eca062a832e41"
36
37
  }