Skip to content

Commit b42aa3d

Browse files
Filter tracking (#76)
* Support filtering types for compression in tracking * Remove double build from pre-push * Pin to fork of np since it actually works * Remove double build from github action
1 parent 2adde1c commit b42aa3d

File tree

8 files changed

+53
-16
lines changed

8 files changed

+53
-16
lines changed

.github/workflows/unit-test.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ jobs:
1515
with:
1616
node-version: ${{ matrix.node-version }}
1717
- run: yarn install
18-
- run: yarn build
1918
- run: yarn test

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"url": "https://github.com/ampproject/filesize.git"
1818
},
1919
"scripts": {
20-
"pretest": "tsc -p test/tsconfig.json",
20+
"build-tests": "tsc -p test/tsconfig.json",
21+
"pretest": "yarn npm-run-all build build-tests",
2122
"test": "ava -v --timeout=2m",
2223
"clean": "rimraf dist output test/output",
2324
"build": "rollup -c; shx chmod a+x dist/filesize",
@@ -42,7 +43,7 @@
4243
"ava": "3.5.1",
4344
"husky": "4.2.5",
4445
"lint-staged": "10.1.4",
45-
"np": "6.2.1",
46+
"np": "https://github.com/pixelastic/np/tarball/c3ab2e3b053c7da0ce40a572ca1616273ac080f8",
4647
"npm-run-all": "4.1.5",
4748
"prettier": "2.0.4",
4849
"rimraf": "3.0.2",
@@ -85,7 +86,7 @@
8586
},
8687
"husky": {
8788
"hooks": {
88-
"pre-push": "yarn npm-run-all clean build test",
89+
"pre-push": "yarn npm-run-all clean test",
8990
"pre-commit": "lint-staged"
9091
}
9192
},

src/validation/Config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const CONDITIONS = [
6666
return `There is no 'filesize' configuration in '${context.packagePath}'`;
6767
}
6868

69-
const { track, ...keys } = json;
69+
const { track, trackFormat, ...keys } = json;
7070
if (Object.entries(keys).length === 0 && track === undefined) {
7171
return `There is no data inside the 'filesize' configuration in '${context.packagePath}'`;
7272
}
@@ -96,7 +96,7 @@ const CONDITIONS = [
9696
];
9797

9898
const Config: ConditionFunction = (context: Context) =>
99-
async function() {
99+
async function () {
100100
for await (const condition of CONDITIONS) {
101101
const conditionResult = await condition(context);
102102
if (conditionResult !== null) {

src/validation/File.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const bytes = require('bytes');
2323
* Format input string to a known Compression Enum Value.
2424
* @param fsValue
2525
*/
26-
function validateCompressionName(fsValue: string): Compression | null {
26+
export function validateCompressionName(fsValue: string): Compression | null {
2727
const lowerCaseValue = fsValue.toLowerCase();
2828
switch (fsValue.toLowerCase()) {
2929
case 'brotli':

src/validation/Track.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Context, ValidationResponse } from './Condition';
18-
import glob from 'fast-glob';
1917
import { resolve } from 'path';
18+
import glob from 'fast-glob';
19+
import { Context, ValidationResponse, SizeMapValue } from './Condition';
20+
import { validateCompressionName } from './File';
2021

2122
/**
2223
* Use 'fast-glob' to find files requested to track from configuration.
@@ -27,15 +28,26 @@ export async function Track(context: Context, json: any): Promise<ValidationResp
2728
if ('track' in json && Array.isArray(json.track)) {
2829
const entries: Array<string> = await glob(json.track);
2930

31+
let trackedFormats: SizeMapValue = [
32+
[null, undefined],
33+
[null, undefined],
34+
[null, undefined],
35+
];
36+
if ('trackFormat' in json && Array.isArray(json.trackFormat)) {
37+
// `trackFormats` allows you to limit the compression types for tracking
38+
const formats = json.trackFormat.map((format: any) => validateCompressionName(String(format))).filter(Boolean);
39+
trackedFormats = [
40+
[formats.includes('brotli') ? null : undefined, undefined],
41+
[formats.includes('gzip') ? null : undefined, undefined],
42+
[formats.includes('none') ? null : undefined, undefined],
43+
];
44+
}
45+
3046
// glob ensures the results are valid files.
3147
for (const entry of entries) {
3248
const path = resolve(entry);
3349

34-
context.compressed.set(path, [
35-
[null, undefined],
36-
[null, undefined],
37-
[null, undefined],
38-
]);
50+
context.compressed.set(path, trackedFormats);
3951
context.originalPaths.set(path, entry);
4052
}
4153
}

test/config-validation/fixtures/track-standalone-format/index.js

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"filesize": {
3+
"track": ["test/config-validation/fixtures/track-standalone-format/**/*.js"],
4+
"trackFormat": ["brotli"]
5+
}
6+
}

test/config-validation/track.test.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { report } from '../../src/api';
2020
import Config from '../../src/validation/Config';
2121
import { Context, SizeMapValue, SizeMap } from '../../src/validation/Condition';
2222

23-
test('including trackable items should succeed', async t => {
23+
test('including trackable items should succeed', async (t) => {
2424
const context: Context = {
2525
packagePath: 'test/config-validation/fixtures/track/package.json',
2626
projectPath: 'test/config-validation/fixtures/track',
@@ -37,7 +37,7 @@ test('including trackable items should succeed', async t => {
3737
t.is(message, null);
3838
});
3939

40-
test('trackable items uses glob to find files', async t => {
40+
test('trackable items uses glob to find files', async (t) => {
4141
const sizes: SizeMapValue = [
4242
[null, undefined], // brotli
4343
[null, undefined], // gzip
@@ -55,3 +55,22 @@ test('trackable items uses glob to find files', async t => {
5555
}
5656
t.deepEqual(results, expected);
5757
});
58+
59+
test('trackable items uses trackFormats to restrict compression types', async (t) => {
60+
const sizes: SizeMapValue = [
61+
[null, undefined], // brotli
62+
[undefined, undefined], // gzip
63+
[undefined, undefined], // none
64+
];
65+
const expected: SizeMap = new Map();
66+
expected.set(resolve('test/config-validation/fixtures/track-standalone-format/index.js'), sizes);
67+
68+
const values = report('test/config-validation/fixtures/track-standalone-format', null);
69+
let next = await values.next();
70+
let results: SizeMap | undefined = undefined;
71+
while (!next.done) {
72+
results = next.value[0];
73+
next = await values.next();
74+
}
75+
t.deepEqual(results, expected);
76+
});

0 commit comments

Comments
 (0)