Skip to content

Commit

Permalink
refactor: use opaline as a base for the cli, and refactor to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rkr00t committed Apr 5, 2021
1 parent a9ed734 commit d6dfb09
Show file tree
Hide file tree
Showing 45 changed files with 5,066 additions and 4,114 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
node-version: [8.x, 12.x]
node-version: [12.x]

steps:
- uses: actions/checkout@v2
Expand All @@ -17,6 +17,7 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run build
- run: npm run ci:validate
- run: npm run ci:test:coverage
- name: Coveralls
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: npm install
- run: npm run build
- run: npm run ci:github-release

env:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ stats.json
.nyc_output
coverage
node_modules
dist
63 changes: 34 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,27 @@ whybundled stats.json ← stats file generated by wepback
## Usage

```sh
Usage
$ whybundled stats.json "[pattern]" [default command]
$ whybundled stats.json --ignore babel-runtime,tslib [default command]
$ whybundled stats.json --by styled-components [by command]

Default options:
[pattern] Optional pattern used to filter output to only matched modules
Note: you might need to wrap the pattern in quotes to use wildcards, e.g. "*.jsx"
--ignore Comma separated list of glob pattern to exclude modules from final output
--modulesOnly Only include modules
--filesOnly Only include files
--directOnly Only include direct dependencies
--transitiveOnly Only include transitive dependencies
--duplicatesOnly Only include modules that have duplicates in a resulting bundle
--limit Limits output of reasons and files [default: 20]

By options [--by]:
--ignore Comma separated list of glob pattern to exclude modules from final output
--limit Limits output of reasons and files [default: 20]
--only Limits output to only include modules that were included by specified module exclusively

Other options:
-v, --version Shows version.
--help Shows help.

Examples
$ whybundled stats.json --modulesOnly
USAGE
whybundled stats.json [pattern]

COMMANDS
by Shows all modules that were brought into the bundle by a particular module.

OPTIONS
--limit Limits output of reasons and files [number] [default: 20]
--filesOnly Only include files [boolean]
--modulesOnly Only include modules [boolean]
--directoryOnly Only include direct dependencies [boolean]
--transitiveOnly Only include transitive dependencies [boolean]
--duplicatesOnly Only include modules that have duplicates in a resulting bundle [boolean]
--ignore Comma separated list of glob patterns to exclude modules from final output [string]
--help Output usage information
--version Output the version number

EXAMPLES
whybundled stats.json --ignore babel-runtime,tslib
whybundled stats.json --modulesOnly
whybundled by stats.json styled-components
```

## Features
Expand Down Expand Up @@ -113,10 +107,21 @@ If module has been bundled several times from different locations `whybundled` a

### Brought by

Using `--by` flag `whybundled` shows all modules that were brought into the bundle by a particular module:
Using `by` command `whybundled` shows all modules that were brought into the bundle by a particular module:

```sh
whybundled stats.json --by styled-components
USAGE
whybundled by stats.json [pattern]

OPTIONS
--limit Limits output of reasons and files [number=20]
--only Limits output to only include modules that were included by specified module exclusively [boolean]
--ignore Comma separated list of glob patterns to exclude modules from final output [string]

EXAMPLES
whybundled by stats.json styled-components
whybundled by stats.json styled-components --ignore babel-runtime,tslib
whybundled by stats.json styled-components --only
```

![whybundled brought by](/assets/by.png)
91 changes: 0 additions & 91 deletions cli.js

This file was deleted.

63 changes: 0 additions & 63 deletions commands/by.js

This file was deleted.

78 changes: 78 additions & 0 deletions commands/by.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Reason, Module } from "../lib/analyze";
import { reporter as defaultReporter } from "../lib/reporter";
import { createProgressBar } from "../lib/console/progress-bar";

import { analyze, getStats } from "../lib";
import { vlidateStatsJson } from "../lib/validate";
import { log, invalidStatsJson } from "../lib/console/messages";
import { normalizeStats } from "../lib/normalize-stats";

/**
* Shows all modules that were brought into the bundle by a particular module.
*
* @param {string[]} $inputs
* @param {number} [limit=20] Limits output of reasons and files
* @param {boolean} only Limits output to only include modules that were included by specified module exclusively
* @param {string} ignore Comma separated list of glob patterns to exclude modules from final output
*
* @usage {cliName} by stats.json [pattern]
* @example whybundled by stats.json styled-components
* @example whybundled by stats.json styled-components --ignore babel-runtime,tslib
* @example whybundled by stats.json styled-components --only
*/
export default async function byCommand(
$inputs: string[],
limit: number = 20,
only: boolean = false,
ignore?: string
) {
const start = Date.now();
const [statsFilePath, by] = $inputs;
const updateProgressBar = createProgressBar();

const stats = normalizeStats(await getStats(statsFilePath));
if (!vlidateStatsJson(stats.modules)) {
log(invalidStatsJson(statsFilePath));
process.exit(1);
}

const ignorePatterns = ignore ? ignore.split(",") : [];
const report = analyze(stats, ignorePatterns, updateProgressBar);

const modules = only
? modulesOnlyBy(report.modules, by)
: modulesFollowingDepsChain(report.modules, by);

defaultReporter.print(modules, report.chunks, { by: by }, limit);

const timing = (Date.now() - start) / 1000;
const rounded = Math.round(timing * 100) / 100;

console.log(`🏁 Done in ${rounded}s.`);
}

const isDepsChainBy = (depsChain: Array<string>, by: string) => {
return depsChain.indexOf(by) !== -1;
};

const modulesFollowingDepsChain = (modules: Array<Module>, by: string) =>
modules.filter(
(mod) =>
mod.reasons.some(
(reason) => reason.moduleName === by || reason.clearName === by
) || (mod.depsChains || []).some((deps) => isDepsChainBy(deps, by))
);

const isSingleReasonBy = (reasons: Array<Reason>, by: string) =>
reasons.length === 1 &&
(reasons[0].moduleName === by || reasons[0].clearName === by);

const modulesOnlyBy = (modules: Array<Module>, by: string) =>
modulesFollowingDepsChain(modules, by).filter(
(mod) =>
isSingleReasonBy(mod.reasons, by) ||
((mod.depsChains || []).length &&
(mod.depsChains || []).every((depsChain) =>
isDepsChainBy(depsChain, by)
))
);
Loading

0 comments on commit d6dfb09

Please sign in to comment.