Skip to content

Commit 025e8ed

Browse files
committed
wip
1 parent 16656c6 commit 025e8ed

38 files changed

+598
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
dist
55
tmp
66
/out-tsc
7+
.code-pushup
78

89
# dependencies
910
node_modules

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"eslint.validate": ["json"]
3+
}

code-pushup.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import bundleSizePlugin from './dist/packages/bundle-size';
2+
import {fileSizePlugin} from "./dist/packages/file-size";
3+
export default {
4+
plugins: [
5+
bundleSizePlugin(),
6+
fileSizePlugin()
7+
]
8+
}

jest.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { getJestProjectsAsync } from '@nx/jest';
2+
3+
export default async () => ({
4+
projects: await getJestProjectsAsync(),
5+
});

jest.preset.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const nxPreset = require('@nx/jest/preset').default;
2+
3+
module.exports = { ...nxPreset };

nx.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"options": {
4040
"buildTargetName": "build"
4141
}
42-
}
42+
},
43+
"./tooling/plugin-factory/src/index.ts"
4344
]
4445
}

packages/bundle-size/project.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
"options": {
2929
"reportsDirectory": "../../coverage/packages/bundle-size"
3030
}
31+
},
32+
"code-pushup": {
33+
"command": "npx code-pushup collect"
3134
}
3235
}
3336
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`bundleSizePlugin > should execute correctly and return valid audit outputs 1`] = `
4+
{
5+
"audits": [
6+
{
7+
"description": "Bundle size audit",
8+
"displayValue": "56.17 ms",
9+
"score": 0.5578038525269111,
10+
"slug": "bundle-size",
11+
"title": "Bundle size",
12+
"value": 56.17041533525504,
13+
},
14+
],
15+
"description": "Bundle size plugin for Code PushUp",
16+
"icon": "folder-css",
17+
"slug": "bundle-size",
18+
"title": "Bundle size",
19+
}
20+
`;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`bundleSizePlugin > should create valid bundle-size plugin configuration 1`] = `
4+
{
5+
"audits": [
6+
{
7+
"description": "Bundle size audit",
8+
"slug": "bundle-size",
9+
"title": "Bundle size",
10+
},
11+
],
12+
"description": "Bundle size plugin for Code PushUp",
13+
"icon": "folder-css",
14+
"runner": [Function],
15+
"slug": "bundle-size",
16+
"title": "Bundle size",
17+
}
18+
`;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {describe, expect, it} from "vitest";
2+
import {pluginReportSchema} from "@code-pushup/models";
3+
import bundleSizePlugin from "./plugin";
4+
import {executePlugin} from "@code-pushup/core";
5+
import {omitVariablePluginData} from "testing-utils";
6+
7+
describe('bundleSizePlugin', () => {
8+
9+
it('should execute correctly and return valid audit outputs', async () => {
10+
const pluginReport = await executePlugin(bundleSizePlugin())
11+
expect(() => pluginReportSchema.parse(pluginReport)).not.toThrow();
12+
expect(omitVariablePluginData(pluginReport)).toMatchSnapshot();
13+
});
14+
15+
});
16+

packages/bundle-size/src/lib/plugin.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
1-
import {PluginConfig} from "@code-pushup/models";
1+
import {AuditOutputs, PluginConfig, RunnerFunction} from "@code-pushup/models";
2+
import {bundleSizeRunner} from "./runner";
23

34
export default bundleSizePlugin;
45

56
export type BundleSizePluginOptions = {};
6-
export function bundleSizePlugin(): PluginConfig {
7-
console.log('bundle-size plugin');
7+
8+
export function bundleSizePlugin(options?: BundleSizePluginOptions): PluginConfig {
89
return {
910
slug: 'bundle-size',
1011
title: 'Bundle size',
1112
description: 'Bundle size plugin for Code PushUp',
1213
icon: "folder-css",
13-
runner: () => {
14-
return [
15-
{
16-
slug: 'bundle-size',
17-
score: 1,
18-
value: 1
19-
},
20-
];
21-
},
14+
runner: bundleSizeRunner(),
2215
audits: [
2316
{
2417
slug: 'bundle-size',
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {describe, expect, it} from "vitest";
2+
import {pluginConfigSchema} from "@code-pushup/models";
3+
import bundleSizePlugin from "./plugin";
4+
5+
describe('bundleSizePlugin', () => {
6+
7+
it('should create valid bundle-size plugin configuration', async () => {
8+
const bundleSizePluginConfig = bundleSizePlugin({});
9+
expect(() => pluginConfigSchema.parse(bundleSizePluginConfig)).not.toThrow();
10+
expect(bundleSizePluginConfig).toMatchSnapshot();
11+
});
12+
13+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {AuditOutputs, RunnerFunction} from "@code-pushup/models";
2+
3+
export type BundleSizeRunnerOptions = {
4+
timeout?: number
5+
};
6+
7+
export function bundleSizeRunner(options?: BundleSizeRunnerOptions): RunnerFunction {
8+
return async (): Promise<AuditOutputs> => {
9+
const {time, value} = await fakePluginLogic(options?.timeout);
10+
11+
return [{
12+
slug: 'bundle-size',
13+
score: value < 1 ? value : 1,
14+
value: time,
15+
displayValue: `${time.toFixed(2)} ms`,
16+
}];
17+
}
18+
}
19+
20+
export async function fakePluginLogic(time?: number): Promise<{ time: number, value: number }> {
21+
const timer = time ?? Math.random() * 100
22+
const value = await new Promise<number>((resolve) => setTimeout(() => {
23+
resolve(Math.random());
24+
}, timer))
25+
return { time: timer, value };
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {describe, expect, it} from "vitest";
2+
import {bundleSizeRunner} from "./runner";
3+
4+
describe('bundleSizeRunner', () => {
5+
6+
it('should have slug of "bundle-size"', async () => {
7+
const runnerFunction = bundleSizeRunner({timeout: 0});
8+
const auditOutputs = await runnerFunction(undefined);
9+
expect(auditOutputs.at(0)?.slug).toBe('bundle-size');
10+
});
11+
12+
});

testing/src/lib/testing-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {readFile, rm, writeFile} from "fs/promises";
22
import {join, relative, resolve} from "path";
3-
import {AuditReport, CoreConfig, PluginReport, Report} from "@code-pushup/models";
3+
import {AuditOutput, AuditReport, CoreConfig, PluginReport, Report} from "@code-pushup/models";
44
import {formatFiles, generateFiles} from "@nx/devkit";
55
import {ensureDirectoryExists, executeProcess, objectToCliArgs, ProcessResult, readJsonFile} from "@code-pushup/utils";
66
import {createTree} from "nx/src/generators/testing-utils/create-tree";
@@ -10,7 +10,7 @@ export function omitVariableAuditData({
1010
value,
1111
displayValue,
1212
...auditReport
13-
}: AuditReport) {
13+
}: AuditReport | AuditOutput) {
1414
return auditReport;
1515
}
1616

tooling/plugin-factory/.eslintrc.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"extends": ["../../.eslintrc.json"],
3+
"ignorePatterns": ["!**/*"],
4+
"overrides": [
5+
{
6+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
7+
"rules": {}
8+
},
9+
{
10+
"files": ["*.ts", "*.tsx"],
11+
"rules": {}
12+
},
13+
{
14+
"files": ["*.js", "*.jsx"],
15+
"rules": {}
16+
},
17+
{
18+
"files": ["*.json"],
19+
"parser": "jsonc-eslint-parser",
20+
"rules": {
21+
"@nx/dependency-checks": "error"
22+
}
23+
},
24+
{
25+
"files": ["./package.json", "./generators.json"],
26+
"parser": "jsonc-eslint-parser",
27+
"rules": {
28+
"@nx/nx-plugin-checks": "error"
29+
}
30+
}
31+
]
32+
}

tooling/plugin-factory/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# plugin-factory
2+
3+
This library was generated with [Nx](https://nx.dev).
4+
5+
## Building
6+
7+
Run `nx build plugin-factory` to build the library.
8+
9+
## Running unit tests
10+
11+
Run `nx test plugin-factory` to execute the unit tests via [Jest](https://jestjs.io).
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"generators": {
3+
"plugin": {
4+
"factory": "./src/generators/plugin/generator",
5+
"schema": "./src/generators/plugin/schema.json",
6+
"description": "plugin generator"
7+
}
8+
}
9+
}

tooling/plugin-factory/jest.config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* eslint-disable */
2+
export default {
3+
displayName: 'plugin-factory',
4+
preset: '../../jest.preset.js',
5+
transform: {
6+
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
7+
},
8+
moduleFileExtensions: ['ts', 'js', 'html'],
9+
coverageDirectory: '../../coverage/tooling/plugin-factory',
10+
};

tooling/plugin-factory/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "plugin-factory",
3+
"version": "0.0.1",
4+
"dependencies": {
5+
"@nx/devkit": "19.4.3",
6+
"tslib": "^2.3.0"
7+
},
8+
"type": "commonjs",
9+
"main": "./src/index.js",
10+
"typings": "./src/index.d.ts",
11+
"private": true,
12+
"generators": "./generators.json"
13+
}

tooling/plugin-factory/project.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "plugin-factory",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "tooling/plugin-factory/src",
5+
"projectType": "library",
6+
"tags": [],
7+
"targets": {
8+
"build": {
9+
"executor": "@nx/js:tsc",
10+
"outputs": ["{options.outputPath}"],
11+
"options": {
12+
"outputPath": "dist/tooling/plugin-factory",
13+
"main": "tooling/plugin-factory/src/index.ts",
14+
"tsConfig": "tooling/plugin-factory/tsconfig.lib.json",
15+
"assets": [
16+
"tooling/plugin-factory/*.md",
17+
{
18+
"input": "./tooling/plugin-factory/src",
19+
"glob": "**/!(*.ts)",
20+
"output": "./src"
21+
},
22+
{
23+
"input": "./tooling/plugin-factory/src",
24+
"glob": "**/*.d.ts",
25+
"output": "./src"
26+
},
27+
{
28+
"input": "./tooling/plugin-factory",
29+
"glob": "generators.json",
30+
"output": "."
31+
},
32+
{
33+
"input": "./tooling/plugin-factory",
34+
"glob": "executors.json",
35+
"output": "."
36+
}
37+
]
38+
}
39+
},
40+
"test": {
41+
"executor": "@nx/jest:jest",
42+
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
43+
"options": {
44+
"jestConfig": "tooling/plugin-factory/jest.config.ts"
45+
}
46+
}
47+
}
48+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { <%= propertyName %>Plugin } from './src';
2+
3+
export default {
4+
plugins: [
5+
<%= propertyName %>Plugin()
6+
],
7+
categories: [
8+
{
9+
slug: '<%= categoryName %>',
10+
title: '<%= categoryName %>',
11+
refs: [
12+
{
13+
slug: '<%= fileName %>-audit',
14+
plugin: '<%= fileName %>',
15+
type: 'audit',
16+
weight: 1
17+
}
18+
]
19+
}
20+
]
21+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { <%= propertyName %>Plugin } from './lib/<%= fileName %>-plugin';
2+
3+
export default <%= propertyName %>Plugin;
4+
export {<%= propertyName %>Plugin} from "./lib/<%= fileName %>-plugin";
5+
export type { <%= className %>PluginConfig } from './lib/<%= fileName %>-plugin';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {PluginConfig} from "@code-pushup/models";
2+
import {<%= propertyName %>Runner} from "./runner";
3+
4+
export default <%= propertyName %>Plugin;
5+
6+
export type <%= className %>PluginConfig = {
7+
opt1?: string;
8+
};
9+
export function <%= propertyName %>Plugin(options?: <%= className %>PluginConfig): PluginConfig {
10+
return {
11+
slug: '<%= fileName %>',
12+
title: '<%= name %>',
13+
description: '<%= name %> plugin for Code PushUp',
14+
icon: "folder",
15+
runner: <%= propertyName %>Runner(),
16+
audits: [
17+
{
18+
slug: '<%= fileName %>-audit',
19+
title: '<%= name %> Audit',
20+
description: `<%= name %> audit ${options?.opt1 ?? ''}`,
21+
},
22+
],
23+
};
24+
}

0 commit comments

Comments
 (0)