@jsenv/file-size-impact 14.1.1 → 14.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/filesize.mjs +6 -6
- package/package.json +5 -5
- package/readme.md +29 -29
- package/src/brotliTransformation.js +7 -7
- package/src/generateFileSizeReport.js +58 -58
- package/src/gzipTransformation.js +7 -7
- package/src/internal/applyTrackingConfig.js +29 -29
- package/src/internal/compareTwoFileSizeReports.js +56 -52
- package/src/internal/formatComment.js +64 -58
- package/src/internal/formatFileSizeReportForLog.js +20 -19
- package/src/internal/formatSize.js +7 -7
- package/src/internal/format_percentage.js +3 -3
- package/src/internal/helper.js +5 -5
- package/src/internal/jsenvCommentParameters.js +38 -38
- package/src/internal/manifest.js +18 -18
- package/src/internal/orderBySizeImpact.js +15 -15
- package/src/internal/renderImpactTable.js +63 -63
- package/src/internal/size_map.js +28 -28
- package/src/jsenvTrackingConfig.js +1 -1
- package/src/main.js +7 -7
- package/src/rawTransformation.js +2 -2
- package/src/report_file_size_impact_in_github_pr.js +17 -17
package/bin/filesize.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import { pathToFileURL } from "node:url"
|
|
4
|
-
import { generateFileSizeReport } from "@jsenv/file-size-impact"
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { generateFileSizeReport } from "@jsenv/file-size-impact";
|
|
5
5
|
|
|
6
|
-
const cwdUrl = `${pathToFileURL(process.cwd())}
|
|
7
|
-
const argv = process.argv.slice(2)
|
|
8
|
-
const dirname = argv[0] || "dist"
|
|
6
|
+
const cwdUrl = `${pathToFileURL(process.cwd())}/`;
|
|
7
|
+
const argv = process.argv.slice(2);
|
|
8
|
+
const dirname = argv[0] || "dist";
|
|
9
9
|
|
|
10
10
|
await generateFileSizeReport({
|
|
11
11
|
log: true,
|
|
@@ -16,4 +16,4 @@ await generateFileSizeReport({
|
|
|
16
16
|
[`${dirname}/**/*.map`]: false,
|
|
17
17
|
},
|
|
18
18
|
},
|
|
19
|
-
})
|
|
19
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/file-size-impact",
|
|
3
|
-
"version": "14.1.
|
|
3
|
+
"version": "14.1.3",
|
|
4
4
|
"description": "Add files size impact into pull requests",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -35,9 +35,9 @@
|
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@jsenv/dynamic-import-worker": "1.1.0",
|
|
38
|
-
"@jsenv/filesystem": "4.2
|
|
39
|
-
"@jsenv/github-pull-request-impact": "1.7.
|
|
40
|
-
"@jsenv/log": "3.
|
|
41
|
-
"@jsenv/urls": "2.
|
|
38
|
+
"@jsenv/filesystem": "4.3.2",
|
|
39
|
+
"@jsenv/github-pull-request-impact": "1.7.3",
|
|
40
|
+
"@jsenv/log": "3.4.1",
|
|
41
|
+
"@jsenv/urls": "2.2.1"
|
|
42
42
|
}
|
|
43
43
|
}
|
package/readme.md
CHANGED
|
@@ -17,8 +17,8 @@ Screenshot + explanation on pull request comment.
|
|
|
17
17
|
| ------------------------------ | ----------------------------------------------------------------------------------------------------------------------------- |
|
|
18
18
|
| "remaining files (+4.71%)" | There is a group of files named "remaining files" and pull request has an overall impact of _+4.71%_ on these files. |
|
|
19
19
|
| "21.6 kB (+4.11 kB / +23.55%)" | The size after merge is _21.6 kB_. Pull request adds _4.11 kB_ representing an increase of _23.55%_ of the size before merge. |
|
|
20
|
-
| "_Unmodified (4)_" | Sum files in
|
|
21
|
-
| _Total (5)_ | Sum files in
|
|
20
|
+
| "_Unmodified (4)_" | Sum of files in that group that are not impacted by the pull request. |
|
|
21
|
+
| _Total (5)_ | Sum of files in that group. |
|
|
22
22
|
|
|
23
23
|
# Installation
|
|
24
24
|
|
|
@@ -31,7 +31,7 @@ npm install --save-dev @jsenv/file-size-impact
|
|
|
31
31
|
_file_size.mjs_
|
|
32
32
|
|
|
33
33
|
```js
|
|
34
|
-
import { generateFileSizeReport } from "@jsenv/file-size-impact"
|
|
34
|
+
import { generateFileSizeReport } from "@jsenv/file-size-impact";
|
|
35
35
|
|
|
36
36
|
export const fileSizeReport = await generateFileSizeReport({
|
|
37
37
|
log: process.argv.includes("--log"),
|
|
@@ -42,7 +42,7 @@ export const fileSizeReport = await generateFileSizeReport({
|
|
|
42
42
|
"./dist/**/*.map": false,
|
|
43
43
|
},
|
|
44
44
|
},
|
|
45
|
-
})
|
|
45
|
+
});
|
|
46
46
|
```
|
|
47
47
|
|
|
48
48
|
At this stage, you could generate a file size report on your machine with the following command.
|
|
@@ -85,7 +85,7 @@ jobs:
|
|
|
85
85
|
- name: Setup npm
|
|
86
86
|
run: npm install
|
|
87
87
|
- name: Report file size impact
|
|
88
|
-
run: node ./
|
|
88
|
+
run: node ./report_file_size_impact.mjs
|
|
89
89
|
env:
|
|
90
90
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
91
91
|
```
|
|
@@ -103,13 +103,13 @@ _report_file_size_impact.mjs_
|
|
|
103
103
|
import {
|
|
104
104
|
reportFileSizeImpactInGitHubPullRequest,
|
|
105
105
|
readGitHubWorkflowEnv,
|
|
106
|
-
} from "@jsenv/file-size-impact"
|
|
106
|
+
} from "@jsenv/file-size-impact";
|
|
107
107
|
|
|
108
108
|
await reportFileSizeImpactInGitHubPullRequest({
|
|
109
109
|
...readGitHubWorkflowEnv(),
|
|
110
110
|
buildCommand: "npm run dist",
|
|
111
111
|
fileSizeReportUrl: new URL("./file_size.mjs#fileSizeReport", import.meta.url),
|
|
112
|
-
})
|
|
112
|
+
});
|
|
113
113
|
```
|
|
114
114
|
|
|
115
115
|
**Notes**:
|
|
@@ -179,7 +179,7 @@ In order to analyse the impact of a pull request on file size the following step
|
|
|
179
179
|
_generateFileSizeReport_ is an async function scanning filesystem to compute a list of file sizes and return these infos into an object.
|
|
180
180
|
|
|
181
181
|
```js
|
|
182
|
-
import { generateFileSizeReport, raw, gzip } from "@jsenv/file-size-impact"
|
|
182
|
+
import { generateFileSizeReport, raw, gzip } from "@jsenv/file-size-impact";
|
|
183
183
|
|
|
184
184
|
const fileSizeReport = await generateFileSizeReport({
|
|
185
185
|
rootDirectoryUrl: new URL("./", import.meta.url),
|
|
@@ -189,7 +189,7 @@ const fileSizeReport = await generateFileSizeReport({
|
|
|
189
189
|
},
|
|
190
190
|
},
|
|
191
191
|
transformations: { raw, gzip },
|
|
192
|
-
})
|
|
192
|
+
});
|
|
193
193
|
```
|
|
194
194
|
|
|
195
195
|
## trackingConfig
|
|
@@ -202,7 +202,7 @@ _trackingConfig_ values are objects associating a pattern to a value. This objec
|
|
|
202
202
|
For example you can create two groups named _"critical files"_ and _"remaining files"_ like this:
|
|
203
203
|
|
|
204
204
|
```js
|
|
205
|
-
import { generateFileSizeReport } from "@jsenv/file-size-impact"
|
|
205
|
+
import { generateFileSizeReport } from "@jsenv/file-size-impact";
|
|
206
206
|
|
|
207
207
|
await generateFileSizeReport({
|
|
208
208
|
trackingConfig: {
|
|
@@ -217,7 +217,7 @@ await generateFileSizeReport({
|
|
|
217
217
|
"./dist/main.css": false,
|
|
218
218
|
},
|
|
219
219
|
},
|
|
220
|
-
})
|
|
220
|
+
});
|
|
221
221
|
```
|
|
222
222
|
|
|
223
223
|

|
|
@@ -234,11 +234,11 @@ import {
|
|
|
234
234
|
raw,
|
|
235
235
|
gzip,
|
|
236
236
|
brotli,
|
|
237
|
-
} from "@jsenv/file-size-impact"
|
|
237
|
+
} from "@jsenv/file-size-impact";
|
|
238
238
|
|
|
239
239
|
await generateFileSizeReport({
|
|
240
240
|
transformations: { raw, gzip, brotli },
|
|
241
|
-
})
|
|
241
|
+
});
|
|
242
242
|
```
|
|
243
243
|
|
|
244
244
|

|
|
@@ -248,7 +248,7 @@ _raw_, _gzip_ and _brotli_ compression can be enabled this way.
|
|
|
248
248
|
It's also possible to control compression level.
|
|
249
249
|
|
|
250
250
|
```js
|
|
251
|
-
import { generateFileSizeReport, raw, gzip } from "@jsenv/file-size-impact"
|
|
251
|
+
import { generateFileSizeReport, raw, gzip } from "@jsenv/file-size-impact";
|
|
252
252
|
|
|
253
253
|
await generateFileSizeReport({
|
|
254
254
|
transformations: {
|
|
@@ -256,7 +256,7 @@ await generateFileSizeReport({
|
|
|
256
256
|
gzip7: (buffer) => gzip(buffer, { level: 7 }),
|
|
257
257
|
gzip9: (buffer) => gzip(buffer, { level: 9 }),
|
|
258
258
|
},
|
|
259
|
-
})
|
|
259
|
+
});
|
|
260
260
|
```
|
|
261
261
|
|
|
262
262
|
Finally _transformations_ can be used to add custom _transformations_.
|
|
@@ -267,14 +267,14 @@ import {
|
|
|
267
267
|
raw,
|
|
268
268
|
gzip,
|
|
269
269
|
brotli,
|
|
270
|
-
} from "@jsenv/file-size-impact"
|
|
270
|
+
} from "@jsenv/file-size-impact";
|
|
271
271
|
|
|
272
272
|
await generateFileSizeReport({
|
|
273
273
|
transformations: {
|
|
274
274
|
raw,
|
|
275
275
|
trim: (buffer) => String(buffer).trim(),
|
|
276
276
|
},
|
|
277
|
-
})
|
|
277
|
+
});
|
|
278
278
|
```
|
|
279
279
|
|
|
280
280
|
## manifestConfig
|
|
@@ -284,25 +284,25 @@ _manifestConfig_ parameter is an object used to configure the location of an opt
|
|
|
284
284
|
This parameter reuses the shape of [trackingConfig](#trackingConfig) (associating pattern + value).
|
|
285
285
|
|
|
286
286
|
```js
|
|
287
|
-
import { reportFileSizeImpactInGitHubPullRequest } from "@jsenv/file-size-impact"
|
|
287
|
+
import { reportFileSizeImpactInGitHubPullRequest } from "@jsenv/file-size-impact";
|
|
288
288
|
|
|
289
289
|
await reportFileSizeImpactInGitHubPullRequest({
|
|
290
290
|
manifestConfig: {
|
|
291
291
|
"./dist/**/manifest.json": true,
|
|
292
292
|
},
|
|
293
|
-
})
|
|
293
|
+
});
|
|
294
294
|
```
|
|
295
295
|
|
|
296
296
|
You can disable manifest files handling by passing `null`.
|
|
297
297
|
|
|
298
298
|
```js
|
|
299
|
-
import { reportFileSizeImpactInGitHubPullRequest } from "@jsenv/file-size-impact"
|
|
299
|
+
import { reportFileSizeImpactInGitHubPullRequest } from "@jsenv/file-size-impact";
|
|
300
300
|
|
|
301
301
|
await reportFileSizeImpactInGitHubPullRequest({
|
|
302
302
|
manifestConfig: {
|
|
303
303
|
"./dist/**/manifest.json": null,
|
|
304
304
|
},
|
|
305
|
-
})
|
|
305
|
+
});
|
|
306
306
|
```
|
|
307
307
|
|
|
308
308
|
In that case _manifest.json_ will be handled as a regular file.
|
|
@@ -315,7 +315,7 @@ _reportFileSizeImpactInGitHubPullRequest_ is an async function that will analyse
|
|
|
315
315
|
import {
|
|
316
316
|
reportFileSizeImpactInGitHubPullRequest,
|
|
317
317
|
raw,
|
|
318
|
-
} from "@jsenv/file-size-impact"
|
|
318
|
+
} from "@jsenv/file-size-impact";
|
|
319
319
|
|
|
320
320
|
await reportFileSizeImpactInGitHubPullRequest({
|
|
321
321
|
logLevel: "info",
|
|
@@ -330,7 +330,7 @@ await reportFileSizeImpactInGitHubPullRequest({
|
|
|
330
330
|
buildCommand: "npm run build",
|
|
331
331
|
fileSizeReportUrl: new URL("./file_size.mjs#fileSizeReport", import.meta.url),
|
|
332
332
|
filesOrdering: "size_impact",
|
|
333
|
-
})
|
|
333
|
+
});
|
|
334
334
|
```
|
|
335
335
|
|
|
336
336
|
## logLevel
|
|
@@ -375,14 +375,14 @@ This parameter is returned by [readGitHubWorkflowEnv](#readGitHubWorkflowEnv) me
|
|
|
375
375
|
Inside an other workflow, you can pass your own _runLink_. As in the example below where it is assumed that script is runned by jenkins.
|
|
376
376
|
|
|
377
377
|
```js
|
|
378
|
-
import { reportFileSizeImpactInGitHubPullRequest } from "@jsenv/file-size-impact"
|
|
378
|
+
import { reportFileSizeImpactInGitHubPullRequest } from "@jsenv/file-size-impact";
|
|
379
379
|
|
|
380
380
|
await reportFileSizeImpactInGitHubPullRequest({
|
|
381
381
|
runLink: {
|
|
382
382
|
url: process.env.BUILD_URL,
|
|
383
383
|
text: `${process.env.JOB_NAME}#${process.env.BUILD_ID}`,
|
|
384
384
|
},
|
|
385
|
-
})
|
|
385
|
+
});
|
|
386
386
|
```
|
|
387
387
|
|
|
388
388
|
# commitInGeneratedByInfo
|
|
@@ -399,13 +399,13 @@ _readGitHubWorkflowEnv_ is a function meant to be runned inside a GitHub workflo
|
|
|
399
399
|
import {
|
|
400
400
|
reportFileSizeImpactInGitHubPullRequest,
|
|
401
401
|
readGitHubWorkflowEnv,
|
|
402
|
-
} from "@jsenv/file-size-impact"
|
|
402
|
+
} from "@jsenv/file-size-impact";
|
|
403
403
|
|
|
404
|
-
const gitHubWorkflowEnv = readGitHubWorkflowEnv()
|
|
404
|
+
const gitHubWorkflowEnv = readGitHubWorkflowEnv();
|
|
405
405
|
|
|
406
406
|
await reportFileSizeImpactInGitHubPullRequest({
|
|
407
407
|
...gitHubWorkflowEnv,
|
|
408
|
-
})
|
|
408
|
+
});
|
|
409
409
|
```
|
|
410
410
|
|
|
411
411
|
_gitHubWorkflowEnv_ object looks like this:
|
|
@@ -421,7 +421,7 @@ const gitHubWorkflowEnv = {
|
|
|
421
421
|
url: "https://github.com/jsenv/repository-name/actions/runs/34",
|
|
422
422
|
text: "workflow-name#34",
|
|
423
423
|
},
|
|
424
|
-
}
|
|
424
|
+
};
|
|
425
425
|
```
|
|
426
426
|
|
|
427
427
|
# File with dynamic names
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { brotliCompress, constants } from "node:zlib"
|
|
1
|
+
import { brotliCompress, constants } from "node:zlib";
|
|
2
2
|
|
|
3
|
-
export const name = "brotli"
|
|
3
|
+
export const name = "brotli";
|
|
4
4
|
|
|
5
5
|
export const transform = (
|
|
6
6
|
value,
|
|
@@ -23,11 +23,11 @@ export const transform = (
|
|
|
23
23
|
},
|
|
24
24
|
(error, buffer) => {
|
|
25
25
|
if (error) {
|
|
26
|
-
reject(error)
|
|
26
|
+
reject(error);
|
|
27
27
|
} else {
|
|
28
|
-
resolve(buffer)
|
|
28
|
+
resolve(buffer);
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
|
-
)
|
|
32
|
-
})
|
|
33
|
-
}
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
};
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { fileURLToPath } from "node:url"
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
2
|
import {
|
|
3
3
|
assertAndNormalizeDirectoryUrl,
|
|
4
4
|
bufferToEtag,
|
|
5
5
|
readFile,
|
|
6
|
-
} from "@jsenv/filesystem"
|
|
7
|
-
import { createDetailedMessage, createLogger } from "@jsenv/log"
|
|
6
|
+
} from "@jsenv/filesystem";
|
|
7
|
+
import { createDetailedMessage, createLogger } from "@jsenv/log";
|
|
8
8
|
|
|
9
|
-
import { transform as rawTransform } from "./rawTransformation.js"
|
|
10
|
-
import { jsenvTrackingConfig } from "./jsenvTrackingConfig.js"
|
|
11
|
-
import { applyTrackingConfig } from "./internal/applyTrackingConfig.js"
|
|
12
|
-
import { formatFileSizeReportForLog } from "./internal/formatFileSizeReportForLog.js"
|
|
9
|
+
import { transform as rawTransform } from "./rawTransformation.js";
|
|
10
|
+
import { jsenvTrackingConfig } from "./jsenvTrackingConfig.js";
|
|
11
|
+
import { applyTrackingConfig } from "./internal/applyTrackingConfig.js";
|
|
12
|
+
import { formatFileSizeReportForLog } from "./internal/formatFileSizeReportForLog.js";
|
|
13
13
|
|
|
14
14
|
export const generateFileSizeReport = async ({
|
|
15
15
|
cancellationToken,
|
|
@@ -22,30 +22,30 @@ export const generateFileSizeReport = async ({
|
|
|
22
22
|
},
|
|
23
23
|
transformations = { raw: rawTransform },
|
|
24
24
|
}) => {
|
|
25
|
-
rootDirectoryUrl = assertAndNormalizeDirectoryUrl(rootDirectoryUrl)
|
|
25
|
+
rootDirectoryUrl = assertAndNormalizeDirectoryUrl(rootDirectoryUrl);
|
|
26
26
|
|
|
27
|
-
const logger = createLogger({ logLevel })
|
|
27
|
+
const logger = createLogger({ logLevel });
|
|
28
28
|
|
|
29
|
-
const trackingNames = Object.keys(trackingConfig)
|
|
29
|
+
const trackingNames = Object.keys(trackingConfig);
|
|
30
30
|
if (trackingNames.length === 0) {
|
|
31
|
-
logger.warn(`trackingConfig is empty`)
|
|
31
|
+
logger.warn(`trackingConfig is empty`);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
const groupTrackingResults = await applyTrackingConfig(trackingConfig, {
|
|
35
35
|
cancellationToken,
|
|
36
36
|
rootDirectoryUrl,
|
|
37
37
|
manifestConfig,
|
|
38
|
-
})
|
|
39
|
-
const groupNames = Object.keys(groupTrackingResults)
|
|
40
|
-
const groups = {}
|
|
38
|
+
});
|
|
39
|
+
const groupNames = Object.keys(groupTrackingResults);
|
|
40
|
+
const groups = {};
|
|
41
41
|
|
|
42
42
|
// ensure keys order is the same as trackingConfig (despite Promise.all below)
|
|
43
43
|
groupNames.forEach((groupName) => {
|
|
44
|
-
groups[groupName] = null
|
|
45
|
-
})
|
|
44
|
+
groups[groupName] = null;
|
|
45
|
+
});
|
|
46
46
|
await Promise.all(
|
|
47
47
|
groupNames.map(async (groupName) => {
|
|
48
|
-
const groupTrackingResult = groupTrackingResults[groupName]
|
|
48
|
+
const groupTrackingResult = groupTrackingResults[groupName];
|
|
49
49
|
const groupReport = await groupTrackingResultToGroupReport(
|
|
50
50
|
groupTrackingResult,
|
|
51
51
|
{
|
|
@@ -54,40 +54,40 @@ export const generateFileSizeReport = async ({
|
|
|
54
54
|
tracking: trackingConfig[groupName],
|
|
55
55
|
transformations,
|
|
56
56
|
},
|
|
57
|
-
)
|
|
58
|
-
groups[groupName] = groupReport
|
|
57
|
+
);
|
|
58
|
+
groups[groupName] = groupReport;
|
|
59
59
|
}),
|
|
60
|
-
)
|
|
60
|
+
);
|
|
61
61
|
|
|
62
62
|
const fileSizeReport = {
|
|
63
63
|
transformationKeys: Object.keys(transformations),
|
|
64
64
|
groups,
|
|
65
|
-
}
|
|
65
|
+
};
|
|
66
66
|
|
|
67
67
|
if (log) {
|
|
68
|
-
logger.info(formatFileSizeReportForLog(fileSizeReport))
|
|
68
|
+
logger.info(formatFileSizeReportForLog(fileSizeReport));
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
return fileSizeReport
|
|
72
|
-
}
|
|
71
|
+
return fileSizeReport;
|
|
72
|
+
};
|
|
73
73
|
|
|
74
74
|
const groupTrackingResultToGroupReport = async (
|
|
75
75
|
groupTrackingResult,
|
|
76
76
|
{ logger, rootDirectoryUrl, tracking, transformations },
|
|
77
77
|
) => {
|
|
78
|
-
const manifestMap = {}
|
|
79
|
-
const { manifestMetaMap } = groupTrackingResult
|
|
80
|
-
const manifestRelativeUrls = Object.keys(manifestMetaMap)
|
|
78
|
+
const manifestMap = {};
|
|
79
|
+
const { manifestMetaMap } = groupTrackingResult;
|
|
80
|
+
const manifestRelativeUrls = Object.keys(manifestMetaMap);
|
|
81
81
|
await Promise.all(
|
|
82
82
|
manifestRelativeUrls.map(async (manifestRelativeUrl) => {
|
|
83
83
|
const manifestFileUrl = new URL(manifestRelativeUrl, rootDirectoryUrl)
|
|
84
|
-
.href
|
|
84
|
+
.href;
|
|
85
85
|
const manifestFileContent = await readFile(manifestFileUrl, {
|
|
86
86
|
as: "string",
|
|
87
|
-
})
|
|
88
|
-
let manifest
|
|
87
|
+
});
|
|
88
|
+
let manifest;
|
|
89
89
|
try {
|
|
90
|
-
manifest = JSON.parse(manifestFileContent)
|
|
90
|
+
manifest = JSON.parse(manifestFileContent);
|
|
91
91
|
} catch (e) {
|
|
92
92
|
if (e.name === "SyntaxError") {
|
|
93
93
|
logger.error(
|
|
@@ -98,55 +98,55 @@ const groupTrackingResultToGroupReport = async (
|
|
|
98
98
|
"manifest file": fileURLToPath(manifestFileUrl),
|
|
99
99
|
},
|
|
100
100
|
),
|
|
101
|
-
)
|
|
102
|
-
return
|
|
101
|
+
);
|
|
102
|
+
return;
|
|
103
103
|
}
|
|
104
|
-
throw e
|
|
104
|
+
throw e;
|
|
105
105
|
}
|
|
106
|
-
manifestMap[manifestRelativeUrl] = manifest
|
|
106
|
+
manifestMap[manifestRelativeUrl] = manifest;
|
|
107
107
|
}),
|
|
108
|
-
)
|
|
108
|
+
);
|
|
109
109
|
|
|
110
|
-
const fileMap = {}
|
|
111
|
-
const { trackedMetaMap } = groupTrackingResult
|
|
112
|
-
const trackedRelativeUrls = Object.keys(trackedMetaMap)
|
|
110
|
+
const fileMap = {};
|
|
111
|
+
const { trackedMetaMap } = groupTrackingResult;
|
|
112
|
+
const trackedRelativeUrls = Object.keys(trackedMetaMap);
|
|
113
113
|
// we use reduce and not Promise.all() because transformation can be expensive (gzip, brotli)
|
|
114
114
|
// so we won't benefit from concurrency (it might even make things worse)
|
|
115
115
|
await trackedRelativeUrls.reduce(async (previous, fileRelativeUrl) => {
|
|
116
|
-
await previous
|
|
117
|
-
const fileUrl = new URL(fileRelativeUrl, rootDirectoryUrl).href
|
|
118
|
-
const fileBuffer = await readFile(fileUrl, { as: "buffer" })
|
|
116
|
+
await previous;
|
|
117
|
+
const fileUrl = new URL(fileRelativeUrl, rootDirectoryUrl).href;
|
|
118
|
+
const fileBuffer = await readFile(fileUrl, { as: "buffer" });
|
|
119
119
|
const sizeMap = await getFileSizeMap(fileBuffer, {
|
|
120
120
|
transformations,
|
|
121
121
|
logger,
|
|
122
122
|
fileUrl,
|
|
123
|
-
})
|
|
124
|
-
const hash = bufferToEtag(fileBuffer)
|
|
123
|
+
});
|
|
124
|
+
const hash = bufferToEtag(fileBuffer);
|
|
125
125
|
fileMap[fileRelativeUrl] = {
|
|
126
126
|
sizeMap,
|
|
127
127
|
hash,
|
|
128
128
|
meta: trackedMetaMap[fileRelativeUrl],
|
|
129
|
-
}
|
|
130
|
-
}, Promise.resolve())
|
|
129
|
+
};
|
|
130
|
+
}, Promise.resolve());
|
|
131
131
|
|
|
132
132
|
return {
|
|
133
133
|
tracking,
|
|
134
134
|
manifestMap,
|
|
135
135
|
fileMap,
|
|
136
|
-
}
|
|
137
|
-
}
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
138
|
|
|
139
139
|
const getFileSizeMap = async (
|
|
140
140
|
fileBuffer,
|
|
141
141
|
{ transformations, logger, fileUrl },
|
|
142
142
|
) => {
|
|
143
|
-
const sizeMap = {}
|
|
143
|
+
const sizeMap = {};
|
|
144
144
|
await Object.keys(transformations).reduce(async (previous, sizeName) => {
|
|
145
|
-
await previous
|
|
146
|
-
const transform = transformations[sizeName]
|
|
145
|
+
await previous;
|
|
146
|
+
const transform = transformations[sizeName];
|
|
147
147
|
try {
|
|
148
|
-
const transformResult = await transform(fileBuffer)
|
|
149
|
-
sizeMap[sizeName] = Buffer.from(transformResult).length
|
|
148
|
+
const transformResult = await transform(fileBuffer);
|
|
149
|
+
sizeMap[sizeName] = Buffer.from(transformResult).length;
|
|
150
150
|
} catch (e) {
|
|
151
151
|
logger.error(
|
|
152
152
|
createDetailedMessage(
|
|
@@ -155,9 +155,9 @@ const getFileSizeMap = async (
|
|
|
155
155
|
"error stack": e.stack,
|
|
156
156
|
},
|
|
157
157
|
),
|
|
158
|
-
)
|
|
159
|
-
sizeMap[sizeName] = "error"
|
|
158
|
+
);
|
|
159
|
+
sizeMap[sizeName] = "error";
|
|
160
160
|
}
|
|
161
|
-
}, Promise.resolve())
|
|
162
|
-
return sizeMap
|
|
163
|
-
}
|
|
161
|
+
}, Promise.resolve());
|
|
162
|
+
return sizeMap;
|
|
163
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { gzip, constants } from "node:zlib"
|
|
1
|
+
import { gzip, constants } from "node:zlib";
|
|
2
2
|
|
|
3
|
-
export const name = "gzip"
|
|
3
|
+
export const name = "gzip";
|
|
4
4
|
|
|
5
5
|
export const transform = (
|
|
6
6
|
value,
|
|
@@ -9,10 +9,10 @@ export const transform = (
|
|
|
9
9
|
return new Promise((resolve, reject) => {
|
|
10
10
|
gzip(value, { level, ...rest }, (error, buffer) => {
|
|
11
11
|
if (error) {
|
|
12
|
-
reject(error)
|
|
12
|
+
reject(error);
|
|
13
13
|
} else {
|
|
14
|
-
resolve(buffer)
|
|
14
|
+
resolve(buffer);
|
|
15
15
|
}
|
|
16
|
-
})
|
|
17
|
-
})
|
|
18
|
-
}
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
};
|
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
import { fileURLToPath } from "node:url"
|
|
2
|
-
import { collectDirectoryMatchReport } from "@jsenv/filesystem"
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { collectDirectoryMatchReport } from "@jsenv/filesystem";
|
|
3
3
|
|
|
4
4
|
export const applyTrackingConfig = async (
|
|
5
5
|
trackingConfig,
|
|
6
6
|
{ rootDirectoryUrl, manifestConfig },
|
|
7
7
|
) => {
|
|
8
|
-
const result = {}
|
|
9
|
-
const trackingNames = Object.keys(trackingConfig)
|
|
8
|
+
const result = {};
|
|
9
|
+
const trackingNames = Object.keys(trackingConfig);
|
|
10
10
|
// ensure keys order is the same as trackingConfig (despite Promise.all below)
|
|
11
11
|
trackingNames.forEach((trackingName) => {
|
|
12
|
-
result[trackingName] = null
|
|
13
|
-
})
|
|
12
|
+
result[trackingName] = null;
|
|
13
|
+
});
|
|
14
14
|
await Promise.all(
|
|
15
15
|
trackingNames.map(async (trackingName) => {
|
|
16
|
-
const tracking = trackingConfig[trackingName]
|
|
16
|
+
const tracking = trackingConfig[trackingName];
|
|
17
17
|
const groupTrackingResult = await applyTracking(tracking, {
|
|
18
18
|
rootDirectoryUrl,
|
|
19
19
|
manifestConfig,
|
|
20
|
-
})
|
|
21
|
-
result[trackingName] = groupTrackingResult
|
|
20
|
+
});
|
|
21
|
+
result[trackingName] = groupTrackingResult;
|
|
22
22
|
}),
|
|
23
|
-
)
|
|
24
|
-
return result
|
|
25
|
-
}
|
|
23
|
+
);
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
26
|
|
|
27
27
|
const applyTracking = async (
|
|
28
28
|
tracking,
|
|
29
29
|
{ rootDirectoryUrl, manifestConfig },
|
|
30
30
|
) => {
|
|
31
|
-
let directoryMatchReport
|
|
31
|
+
let directoryMatchReport;
|
|
32
32
|
try {
|
|
33
33
|
directoryMatchReport = await collectDirectoryMatchReport({
|
|
34
34
|
directoryUrl: rootDirectoryUrl,
|
|
@@ -37,35 +37,35 @@ const applyTracking = async (
|
|
|
37
37
|
...(manifestConfig ? { manifest: manifestConfig } : {}),
|
|
38
38
|
},
|
|
39
39
|
predicate: (meta) => Boolean(meta.track) || Boolean(meta.manifest),
|
|
40
|
-
})
|
|
40
|
+
});
|
|
41
41
|
} catch (e) {
|
|
42
|
-
const directoryPath = fileURLToPath(rootDirectoryUrl)
|
|
42
|
+
const directoryPath = fileURLToPath(rootDirectoryUrl);
|
|
43
43
|
if (e.code === "ENOENT" && e.path === directoryPath) {
|
|
44
|
-
console.warn(`${directoryPath} does not exists`)
|
|
45
|
-
return []
|
|
44
|
+
console.warn(`${directoryPath} does not exists`);
|
|
45
|
+
return [];
|
|
46
46
|
}
|
|
47
|
-
throw e
|
|
47
|
+
throw e;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
const { matchingArray, ignoredArray } = directoryMatchReport
|
|
50
|
+
const { matchingArray, ignoredArray } = directoryMatchReport;
|
|
51
51
|
|
|
52
|
-
const trackedMetaMap = {}
|
|
53
|
-
const ignoredMetaMap = {}
|
|
54
|
-
const manifestMetaMap = {}
|
|
52
|
+
const trackedMetaMap = {};
|
|
53
|
+
const ignoredMetaMap = {};
|
|
54
|
+
const manifestMetaMap = {};
|
|
55
55
|
ignoredArray.forEach(({ relativeUrl }) => {
|
|
56
|
-
ignoredMetaMap[relativeUrl] = null
|
|
57
|
-
})
|
|
56
|
+
ignoredMetaMap[relativeUrl] = null;
|
|
57
|
+
});
|
|
58
58
|
matchingArray.forEach(({ relativeUrl, meta }) => {
|
|
59
59
|
if (meta.manifest) {
|
|
60
|
-
manifestMetaMap[relativeUrl] = meta.manifest
|
|
60
|
+
manifestMetaMap[relativeUrl] = meta.manifest;
|
|
61
61
|
} else {
|
|
62
|
-
trackedMetaMap[relativeUrl] = meta.track
|
|
62
|
+
trackedMetaMap[relativeUrl] = meta.track;
|
|
63
63
|
}
|
|
64
|
-
})
|
|
64
|
+
});
|
|
65
65
|
|
|
66
66
|
return {
|
|
67
67
|
trackedMetaMap,
|
|
68
68
|
ignoredMetaMap,
|
|
69
69
|
manifestMetaMap,
|
|
70
|
-
}
|
|
71
|
-
}
|
|
70
|
+
};
|
|
71
|
+
};
|