Skip to content

Commit

Permalink
feat: validate stats.json
Browse files Browse the repository at this point in the history
Closes: #6
  • Loading branch information
d4rkr00t committed Mar 9, 2018
1 parent 3db4b0f commit e230e80
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 4 deletions.
8 changes: 8 additions & 0 deletions commands/by.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
/* @flow */

const { analyze, print, getStats } = require("../lib");
const validate = require("../lib/validate");
const { log, invalidStatsJson } = require("../lib/messages");

module.exports = function byCommand(
statsFilePath /*: string */,
flags /*: { limit: number, by: string, ignore?: string } */,
pattern /*: string */
) {
const stats = getStats(statsFilePath);

if (!validate(stats)) {
log(invalidStatsJson(statsFilePath));
process.exit(1);
}

const ignore = flags.ignore ? flags.ignore.split(",") : [];
const report = analyze(stats, ignore).filter(mod => {
return (
Expand Down
8 changes: 8 additions & 0 deletions commands/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const mm = require("micromatch");
const { analyze, print, getStats } = require("../lib");
const validate = require("../lib/validate");
const { log, invalidStatsJson } = require("../lib/messages");

/*::
type Flags = {
Expand All @@ -22,6 +24,12 @@ module.exports = function defaultCommand(
pattern /*: string*/
) {
const stats = getStats(statsFilePath);

if (!validate(stats)) {
log(invalidStatsJson(statsFilePath));
process.exit(1);
}

const ignore = flags.ignore ? flags.ignore.split(",") : [];
const report = analyze(stats, ignore).filter(module => {
if (pattern && mm.isMatch(module.name, pattern)) {
Expand Down
13 changes: 13 additions & 0 deletions fixtures/invalid-no-reasons.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"chunks": [
{
"modules": [
{
"id": "module1",
"name": "module-name",
"size": 500
}
]
}
]
}
14 changes: 14 additions & 0 deletions fixtures/valid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"chunks": [
{
"modules": [
{
"id": "module1",
"name": "module-name",
"size": 500,
"reasons": []
}
]
}
]
}
File renamed without changes.
20 changes: 20 additions & 0 deletions lib/__tests__/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const test = require("ava");
const fixtures = require("fixturez");
const f = fixtures(__dirname);
const validate = require("../validate");
const getStats = require("../get-stats");

test(`should return false for an invalid stats file that doesn't have "reasons" for modules`, t => {
const stats = getStats(f.find("invalid-no-reasons.json"));
t.falsy(validate(stats));
});

test(`should return false for an invalid stats file that doesn't have niether chunks nor modules`, t => {
const stats = getStats(f.find("empty-stats.json"));
t.falsy(validate(stats));
});

test("should return true for a valid stats file", t => {
const stats = getStats(f.find("valid.json"));
t.truthy(validate(stats));
});
7 changes: 5 additions & 2 deletions lib/analyze.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* @flow */
/*::
export type WebpackStats = {
chunks: Array<WebpackChunk>
chunks?: Array<WebpackChunk>,
modules: Array<WebpackModule>
};
export type WebpackChunk = {
Expand Down Expand Up @@ -93,7 +94,9 @@ const getModuleType = (name /*: string */) =>
name.startsWith("multi ") ? "entry" : isNodeModules(name) ? "module" : "file";

const flattenChunks = (stats /*: WebpackStats */) /*: Array<WebpackModule> */ =>
stats.chunks.reduce((modules, chunk) => modules.concat(chunk.modules), []);
stats.chunks
? stats.chunks.reduce((modules, chunk) => modules.concat(chunk.modules), [])
: stats.modules;

const isNodeModules = (identifier /*: string */) =>
identifier.indexOf("node_modules") > -1;
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/get-stats.js → lib/get-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const path = require("path");

/*::
import type { WebpackStats } from '../analyze';
import type { WebpackStats } from './analyze';
*/

const getAbsoultePath = (filePath /*: string */) =>
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

const analyze = require("./analyze");
const print = require("./print");
const getStats = require("./utils/get-stats");
const getStats = require("./get-stats");

module.exports = { analyze, print, getStats };
32 changes: 32 additions & 0 deletions lib/messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* @flow */

const chalk = require("chalk");

function log(msg /*: Array<string> */) {
console.log(msg.join("\n"));
}

function redBadge(label /*: string*/) {
return chalk.bgRed.black(` ${label} `);
}

function errorBadge() {
return redBadge("ERROR");
}

function invalidStatsJson(file /*: string */) {
return [
chalk.red(
`${errorBadge()} Stats file ${chalk.bold(
`"${file}"`
)} doesn't contain "reasons" why modules are included...`
),
`${chalk.yellow("Whybundled")} needs "reasons" to function properly.`,
"",
`To add reason check webpack documentation about stats configuration: ${chalk.blue(
"https://webpack.js.org/configuration/stats/#stats"
)}`
];
}

module.exports = { log, invalidStatsJson };
28 changes: 28 additions & 0 deletions lib/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* @flow */

/*::
import type { WebpackStats } from "./analyze";
*/

function has(obj) {
return function(prop) {
return obj.hasOwnProperty(prop);
};
}

function isValidModule(sample) {
const isProp = has(sample);
if (!isProp("reasons")) return false;
return true;
}

module.exports = function vlidateStatsJson(stats /*: WebpackStats */) {
if (
!stats ||
((!stats.chunks || !stats.chunks[0].modules) && !stats.modules)
) {
return false;
}
const samples = (stats.modules || stats.chunks[0].modules).slice(0, 10);
return samples.some(isValidModule);
};

0 comments on commit e230e80

Please sign in to comment.