Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit09972bc

Browse files
committed
Build: add commit SHAs and last runs to comparisons
- only remove the short SHA and .dirty from version strings- automatically reset the cache on version mismatchClosegh-5329
1 parent7922384 commit09972bc

File tree

1 file changed

+66
-20
lines changed

1 file changed

+66
-20
lines changed

‎build/tasks/compare_size.mjs

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import zlib from "node:zlib";
55
import{execasnodeExec}from"node:child_process";
66
importisCleanWorkingDirfrom"./lib/isCleanWorkingDir.js";
77

8+
constVERSION=1;
9+
810
constgzip=promisify(zlib.gzip);
911
constexec=promisify(nodeExec);
1012

@@ -13,13 +15,47 @@ async function getBranchName() {
1315
returnstdout.trim();
1416
}
1517

18+
asyncfunctiongetCommitHash(){
19+
const{ stdout}=awaitexec("git rev-parse HEAD");
20+
returnstdout.trim();
21+
}
22+
23+
functiongetBranchHeader(branch,commit){
24+
letbranchHeader=branch.trim();
25+
if(commit){
26+
branchHeader=chalk.bold(branchHeader)+chalk.gray(` @${commit}`);
27+
}else{
28+
branchHeader=chalk.italic(branchHeader);
29+
}
30+
returnbranchHeader;
31+
}
32+
1633
asyncfunctiongetCache(loc){
34+
letcache;
1735
try{
1836
constcontents=awaitfs.promises.readFile(loc,"utf8");
19-
returnJSON.parse(contents);
37+
cache=JSON.parse(contents);
2038
}catch(err){
2139
return{};
2240
}
41+
42+
constlastRun=cache[" last run"];
43+
if(!lastRun||!lastRun.meta||lastRun.meta.version!==VERSION){
44+
console.log("Compare cache version mismatch. Rewriting...");
45+
return{};
46+
}
47+
returncache;
48+
}
49+
50+
functioncacheResults(results){
51+
constfiles=Object.create(null);
52+
results.forEach(function(result){
53+
files[result.filename]={
54+
raw:result.raw,
55+
gz:result.gz
56+
};
57+
});
58+
returnfiles;
2359
}
2460

2561
functionsaveCache(loc,cache){
@@ -43,6 +79,7 @@ export async function compareSize( { cache = ".sizecache.json", files } = {} ) {
4379
}
4480

4581
constbranch=awaitgetBranchName();
82+
constcommit=awaitgetCommitHash();
4683
constsizeCache=awaitgetCache(cache);
4784

4885
letrawPadLength=0;
@@ -52,9 +89,11 @@ export async function compareSize( { cache = ".sizecache.json", files } = {} ) {
5289

5390
letcontents=awaitfs.promises.readFile(filename,"utf8");
5491

55-
// Remove the banner for size comparisons.
56-
// The version string can vary widely by short SHA.
57-
contents=contents.replace(/\/\*\!jQuery[^\n]+/,"");
92+
// Remove the short SHA and .dirty from comparisons.
93+
// The short SHA so commits can be compared against each other
94+
// and .dirty to compare with the existing branch during development.
95+
constsha=/jQueryv\d+.\d+.\d+(?:-\w+)?\+(?:slim.)?([^\.]+(?:\.dirty)?)/.exec(contents)[1];
96+
contents=contents.replace(newRegExp(sha,"g"),"");
5897

5998
constsize=Buffer.byteLength(contents,"utf8");
6099
constgzippedSize=(awaitgzip(contents)).length;
@@ -67,7 +106,7 @@ export async function compareSize( { cache = ".sizecache.json", files } = {} ) {
67106
})
68107
);
69108

70-
constheader="raw".padStart(rawPadLength)+
109+
constsizeHeader="raw".padStart(rawPadLength)+
71110
"gz".padStart(gzPadLength+1)+
72111
" Filename";
73112

@@ -78,8 +117,12 @@ export async function compareSize( { cache = ".sizecache.json", files } = {} ) {
78117
});
79118

80119
constcomparisons=Object.keys(sizeCache).map(function(branch){
81-
constbranchSizes=Object.keys(sizeCache[branch]).map(function(filename){
82-
constbranchResult=sizeCache[branch][filename];
120+
constmeta=sizeCache[branch].meta||{};
121+
constcommit=meta.commit;
122+
123+
constfiles=sizeCache[branch].files;
124+
constbranchSizes=Object.keys(files).map(function(filename){
125+
constbranchResult=files[filename];
83126
constcompareResult=results.find(function(result){
84127
returnresult.filename===filename;
85128
})||{};
@@ -91,38 +134,41 @@ export async function compareSize( { cache = ".sizecache.json", files } = {} ) {
91134

92135
return[
93136
"",// New line before each branch
94-
chalk.bold(branch),
95-
header,
137+
getBranchHeader(branch,commit),
138+
sizeHeader,
96139
...branchSizes
97140
].join("\n");
98141
});
99142

100143
constoutput=[
101144
"",// Opening new line
102145
chalk.bold("Sizes"),
103-
header,
146+
sizeHeader,
104147
...sizes,
105148
...comparisons,
106149
""// Closing new line
107150
].join("\n");
108151

109152
console.log(output);
110153

154+
// Always save the last run
155+
// Save version under last run
156+
sizeCache[" last run"]={
157+
meta:{version:VERSION},
158+
files:cacheResults(results)
159+
};
160+
111161
// Only save cache for the current branch
112162
// if the working directory is clean.
113163
if(awaitisCleanWorkingDir()){
114-
sizeCache[branch]={};
115-
results.forEach(function(result){
116-
sizeCache[branch][result.filename]={
117-
raw:result.raw,
118-
gz:result.gz
119-
};
120-
});
121-
122-
awaitsaveCache(cache,sizeCache);
123-
164+
sizeCache[branch]={
165+
meta:{ commit},
166+
files:cacheResults(results)
167+
};
124168
console.log(`Saved cache for${branch}.`);
125169
}
126170

171+
awaitsaveCache(cache,sizeCache);
172+
127173
returnresults;
128174
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp