Skip to content

Commit a026a31

Browse files
authored
docs(cli): add docs for custom plugins (#312)
1 parent 848408f commit a026a31

12 files changed

Lines changed: 873 additions & 19 deletions

examples/plugins/code-pushup.config.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import {
2-
create as fileSizePlugin,
1+
import fileSizePlugin, {
32
recommendedRefs as fileSizeRecommendedRefs,
4-
} from './src/file-size/file-size.plugin';
3+
} from './src/file-size/src/file-size.plugin';
54

65
/**
76
* Run it with:
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# file-size-plugin
2+
3+
🕵️ **Code PushUp plugin for detecting changes in file size using different compressions** 📋
4+
5+
---
6+
7+
The plugin crawls the file base depending on your configuration and reports about their file size.
8+
9+
You can configure the plugin with the following options:
10+
11+
- directory to crawl
12+
- file name pattern
13+
- budget as number in bytes
14+
15+
## Getting started
16+
17+
1. If you haven't already, install [@code-pushup/cli](../cli/README.md) and create a configuration file.
18+
19+
2. Copy the [plugin source](../file-size) as is into your project
20+
21+
3. Add this plugin to the `plugins` array in your Code PushUp CLI config file (e.g. `code-pushup.config.js`).
22+
23+
Pass in the path on the directory to crawl (relative to `process.cwd()`), as well as patterns and a budget.
24+
25+
```js
26+
import fileSizePlugin from './file-size.plugin';
27+
28+
export default {
29+
// ...
30+
plugins: [
31+
// ...
32+
fileSizePlugin({
33+
directory: 'dist',
34+
patterns: /.js$/,
35+
budget: 42000,
36+
}),
37+
],
38+
};
39+
```
40+
41+
4. (Optional) Reference audits (or groups) that you wish to include in custom categories (use `npx code-pushup print-config` to list audits and groups).
42+
43+
Assign weights based on what influence each audit and group should have on the overall category score (assign weight 0 to only include it for extra info, without influencing the category score).
44+
45+
```js
46+
import fileSizePlugin, { recommendedRefs as fileSizeRecommendedRefs } from './file-size.plugin';
47+
48+
export default {
49+
// ...
50+
categories: [
51+
// ...
52+
{
53+
slug: 'performance',
54+
title: 'Performance',
55+
refs: [...fileSizeRecommendedRefs],
56+
},
57+
],
58+
};
59+
```
60+
61+
5. Run the CLI with `npx code-pushup collect` and view or upload report (refer to [CLI docs](../cli/README.md)).
62+
63+
## Audits
64+
65+
Detailed information about the audits can be found in the docs folder of the plugin.
66+
67+
The following audits are present:
68+
69+
- [file-size-unmodified](@TODO - link to docs/file-size-unmodified.audit.md)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# File Size Audit - Unmodified
2+
3+
🕵️ **An audit to check JavaScript file size in a directory. The files are not modified and takes as they are.** 📏
4+
5+
---
6+
7+
The audit evaluates the size of a file by using the `stat` function for the `fs:promises` node package.
8+
9+
You can configure the plugin with the following options:
10+
11+
- `budget` as number in bytes
12+
13+
## Details
14+
15+
The audit provides details in cases a file result is given.
16+
17+
### Issues
18+
19+
**Audit passed**
20+
A `Issue` with severity `info` is present and names to the given file.
21+
22+
```md
23+
<table>
24+
<tr>
25+
<th>Severity</th>
26+
<th>Message</th>
27+
<th>Source file</th>
28+
<th>Line(s)</th>
29+
</tr>
30+
<tr>
31+
<td>ℹ️ <i>info</i></td>
32+
<td>File file.js OK</td>
33+
<td><code>src/file.js</code></td>
34+
<td></td>
35+
</tr>
36+
</table>
37+
```
38+
39+
**Audit failed**
40+
A `Issue` with severity `error` is present and names to the given file.
41+
The file sizes of the given file, the budget as well as the size difference is mentioned in the message.
42+
43+
```md
44+
<table>
45+
<tr>
46+
<th>Severity</th>
47+
<th>Message</th>
48+
<th>Source file</th>
49+
<th>Line(s)</th>
50+
</tr>
51+
<tr>
52+
<td>🚨 <i>error</i></td>
53+
<td>File file.js is 17.31 kB bytes too big. (budget: 41.02 kB)</td>
54+
<td><code>src/file.js</code></td>
55+
<td></td>
56+
</tr>
57+
</table>
58+
```

examples/plugins/src/file-size/file-size.plugin.integration.test.ts renamed to examples/plugins/src/file-size/src/file-size.plugin.integration.test.ts

File renamed without changes.

examples/plugins/src/file-size/file-size.plugin.ts renamed to examples/plugins/src/file-size/src/file-size.plugin.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
formatBytes,
1414
pluralizeToken,
1515
toUnixPath,
16-
} from '../../../../dist/packages/utils';
16+
} from '../../../../../dist/packages/utils';
1717

1818
export type PluginOptions = {
1919
directory: string;
@@ -25,12 +25,13 @@ type RunnerOptions = PluginOptions;
2525

2626
export const pluginSlug = 'file-size';
2727

28-
const fileSizeAuditSlug = 'file-size-check';
28+
const fileSizeAuditSlug = 'file-size-unmodified';
2929
export const auditsMap = {
3030
[fileSizeAuditSlug]: {
3131
slug: fileSizeAuditSlug,
32-
title: 'File Size Audit',
33-
description: 'An audit to check JavaScript file size in a directory.',
32+
title: 'File Size Audit - Unmodified',
33+
description:
34+
'An audit to check JavaScript file size in a directory. The files are not modified and takes as they are.',
3435
},
3536
};
3637
export const audits = Object.values(auditsMap);
@@ -125,7 +126,7 @@ export function displayValue(numberOfFiles: number): string {
125126
return `${pluralizeToken('file', numberOfFiles)} oversize`;
126127
}
127128

128-
export async function fileSizeIssues(options: {
129+
export function fileSizeIssues(options: {
129130
directory: string;
130131
pattern?: string | RegExp;
131132
budget?: number;
@@ -192,3 +193,5 @@ export function assertFileSize(
192193
message: infoMessage(file, formattedSize),
193194
} satisfies Issue;
194195
}
196+
197+
export default create;

examples/plugins/src/file-size/file-size.plugin.unit.test.ts renamed to examples/plugins/src/file-size/src/file-size.plugin.unit.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ describe('runnerFunction', () => {
198198
const filesizeAuditOutputBase = {
199199
displayValue: '0 files oversize',
200200
score: 1,
201-
slug: 'file-size-check',
201+
slug: 'file-size-unmodified',
202202
value: 0,
203203
};
204204

examples/plugins/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ export {
33
create as fileSizePlugin,
44
recommendedRefs as fileSizeRecommendedRefs,
55
PluginOptions as FileSizePluginOptions,
6-
} from './file-size/file-size.plugin';
6+
} from './file-size/src/file-size.plugin';

packages/cli/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ jobs:
128128
- run: npx code-pushup autorun --upload.apiKey=${{ secrets.PORTAL_API_KEY }}
129129
```
130130
131+
## Custom Plugins
132+
133+
We provide comprehensive documentation on [how to create a custom plugin](./docs/custom-plugins.md).
134+
135+
The repository also maintains a set of plugin examples showcasing different scenarios.
136+
Each example is fully tested to give demonstrate best practices for plugin testing.
137+
138+
**Example for custom plugins:**
139+
140+
- [File Size](../../examples/plugins/src/file-size)
141+
131142
## CLI commands and options
132143
133144
### Global Options

0 commit comments

Comments
 (0)