Skip to content

Commit

Permalink
feat: Added --ignore flag to exclude modules from output
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rkr00t committed Feb 28, 2018
1 parent 9f9bb51 commit 88f1046
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
3 changes: 2 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const knownFlags = [
"duplicatesOnly",
"limit",
"version",
"help"
"help",
"ignore"
];

const validateFlags = flags => {
Expand Down
13 changes: 7 additions & 6 deletions commands/by.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ const { analyze, print, getStats } = require("../lib");

module.exports = function byCommand(
statsFilePath /*: string */,
flags /*: { limit: number, by: string } */,
flags /*: { limit: number, by: string, ignore?: string } */,
pattern /*: string */
) {
const stats = getStats(statsFilePath);
const { by } = flags;
const report = analyze(stats).filter(mod => {
const ignore = flags.ignore ? flags.ignore.split(",") : [];
const report = analyze(stats, ignore).filter(mod => {
return (
mod.reasons.some(
reason => reason.moduleName === by || reason.clearName === by
) || (mod.depsChains || []).some(deps => deps.indexOf(by) !== -1)
reason =>
reason.moduleName === flags.by || reason.clearName === flags.by
) || (mod.depsChains || []).some(deps => deps.indexOf(flags.by) !== -1)
);
});
const limit /*: number */ = pattern ? 0 : flags.limit >= 0 ? flags.limit : 20;
print(report, { by }, limit);
print(report, { by: flags.by }, limit);
};
4 changes: 3 additions & 1 deletion commands/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Flags = {
directOnly?: boolean,
transitiveOnly?: boolean,
duplicatesOnly?: boolean,
ignore?: string,
by?: string
}
*/
Expand All @@ -21,7 +22,8 @@ module.exports = function defaultCommand(
pattern /*: string*/
) {
const stats = getStats(statsFilePath);
const report = analyze(stats).filter(module => {
const ignore = flags.ignore ? flags.ignore.split(",") : [];
const report = analyze(stats, ignore).filter(module => {
if (pattern && mm.isMatch(module.name, pattern)) {
return true;
} else if (pattern) {
Expand Down
7 changes: 5 additions & 2 deletions commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ const chalk = require("chalk");
module.exports = function help() {
return [
chalk.green("Usage"),
` $ whybundled stats.json [pattern] ${chalk.dim('[default command]')}`,
` $ whybundled stats.json --by styled-components ${chalk.dim('[by command]')}`,
` $ whybundled stats.json [pattern] ${chalk.dim('[default command]')}`,
` $ whybundled stats.json --ignore babel-runtime,tslib ${chalk.dim('[default command]')}`,
` $ whybundled stats.json --by styled-components ${chalk.dim('[by command]')}`,
"",
chalk.green("Default options:"),
` ${chalk.yellow("[pattern]")} Optional pattern used to filter output to only matched modules`,
` ${chalk.yellow("--ignore")} Comma separated list of glob pattern to exclude modules from final output`,
` ${chalk.yellow("--modulesOnly")} Only include modules`,
` ${chalk.yellow("--filesOnly")} Only include files`,
` ${chalk.yellow("--directOnly")} Only include direct dependencies`,
Expand All @@ -18,6 +20,7 @@ module.exports = function help() {
` ${chalk.yellow("--limit")} Limits output of reasons and files [default: 20]`,
"",
chalk.green("By options [--by]:"),
` ${chalk.yellow("--ignore")} Comma separated list of glob pattern to exclude modules from final output`,
` ${chalk.yellow("--limit")} Limits output of reasons and files [default: 20]`,
``,
chalk.green("Other options:"),
Expand Down
29 changes: 22 additions & 7 deletions lib/analyze.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/* @flow */

const path = require("path");

/*::
export type WebpackStats = {
chunks: Array<WebpackChunk>
Expand Down Expand Up @@ -65,6 +62,11 @@ export type SubReason = {
*/

const path = require("path");
const mm = require("micromatch");

const DEFAULT_IGNORE = ["multi *", "*-loader*"];

const toArray = (report /*: { [string]: Module } */) =>
Object.keys(report).map(name =>
Object.assign({}, report[name], { name: name })
Expand Down Expand Up @@ -207,7 +209,7 @@ const pickFromModules = (modules) /*: Array<PreModule> */ =>
const buildModuleDepsChains = (modules, name) => {
const module = modules[name];
return module.reasons.reduce((acc, reason) => {
if (reason.type !== "module") {
if (reason.type !== "module" || !modules[reason.clearName]) {
return acc;
}

Expand Down Expand Up @@ -239,15 +241,28 @@ const postProcessModules = modules => {
).map(chain => chain.split("|"));
}

acc[name] = module;
module.reasons = module.reasons.filter(
reason => !!modules[reason.moduleName]
);

if (module.reasons.length) {
acc[name] = module;
}
return acc;
}, {});
};

module.exports = function analyze(stats /*: WebpackStats */) {
module.exports = function analyze(
stats /*: WebpackStats */,
ignore /*: Array<string> */ = []
) {
const rawModules = flattenChunks(stats);
const modules = pickFromModules(rawModules).filter(
m => !m.name.startsWith("multi") && !m.name.match("-loader")
m =>
!mm.isMatch(
m.clearName || m.name,
[].concat(DEFAULT_IGNORE).concat(ignore)
)
);
return toArray(postProcessModules(joinModules(modules)));
};

0 comments on commit 88f1046

Please sign in to comment.