Skip to content

Commit d09dbc0

Browse files
Bart Venemanbartveneman
authored andcommitted
🐛 calculate embedded size correctly on duplicate embeds
1 parent 5cb62c3 commit d09dbc0

File tree

7 files changed

+53
-11
lines changed

7 files changed

+53
-11
lines changed

src/__fixtures__/bol-com-20190617.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
},
4343
"uniquenessRatio": 0.6666666666666666,
4444
"size": {
45-
"total": 33224,
46-
"ratio": 0.07099069454386171
45+
"total": 37768,
46+
"ratio": 0.08069999252144743
4747
},
4848
"types": {
4949
"total": 42,

src/__fixtures__/bootstrap-5.0.0.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
},
3131
"uniquenessRatio": 0.8,
3232
"size": {
33-
"total": 3473,
34-
"ratio": 0.01784136443028871
33+
"total": 4439,
34+
"ratio": 0.02280386314599815
3535
},
3636
"types": {
3737
"total": 20,

src/__fixtures__/github-20210501.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
},
2020
"uniquenessRatio": 0.7142857142857143,
2121
"size": {
22-
"total": 1198,
23-
"ratio": 0.002329905247926812
22+
"total": 2050,
23+
"ratio": 0.0039868996312604045
2424
},
2525
"types": {
2626
"total": 7,

src/__fixtures__/smashing-magazine-20190319.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
},
3737
"uniquenessRatio": 0.34375,
3838
"size": {
39-
"total": 14286,
40-
"ratio": 0.012947817197050442
39+
"total": 37316,
40+
"ratio": 0.0338205758452425
4141
},
4242
"types": {
4343
"total": 64,

src/__fixtures__/trello-20190617.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
},
1717
"uniquenessRatio": 0.5,
1818
"size": {
19-
"total": 478,
20-
"ratio": 0.0015342789370464168
19+
"total": 956,
20+
"ratio": 0.0030685578740928337
2121
},
2222
"types": {
2323
"total": 4,

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const analyze = (css) => {
4343
let totalComments = 0
4444
let commentsSize = 0
4545
const embeds = new CountableCollection()
46+
let embedSize = 0
4647
const embedTypes = {
4748
total: 0,
4849
totalUnique: 0,
@@ -275,6 +276,7 @@ const analyze = (css) => {
275276
var type = getEmbedType(embed)
276277

277278
embedTypes.total++
279+
embedSize += size
278280

279281
if (type in embedTypes.unique) {
280282
embedTypes.unique[type].count++
@@ -467,7 +469,6 @@ const analyze = (css) => {
467469
})
468470

469471
const embeddedContent = embeds.count()
470-
const embedSize = Object.keys(embeddedContent.unique).join('').length
471472

472473
const totalUniqueDeclarations = uniqueDeclarations.size
473474

src/stylesheet/stylesheet.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,47 @@ Stylesheet('measures base64 contents', () => {
138138
assert.equal(actual, expected)
139139
})
140140

141+
Stylesheet('reports embed size correctly when there are duplicates', () => {
142+
const fixture = `
143+
/* Demo from https://codepen.io/chriscoyier/pen/ZQgvyG/ */
144+
.chevron-down {
145+
background: url(data:image/svg+xml,%3Csvg%20id%3D%22Layer_1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20195.6%20107.8%22%3E%3Cpath%20fill%3D%22%23B5B5B5%22%20class%3D%22st0%22%20d%3D%22M97.8%20107.8c-2.6%200-5.1-1-7.1-2.9L2.9%2017.1C-1%2013.2-1%206.8%202.9%202.9%206.8-1%2013.2-1%2017.1%202.9l80.7%2080.7%2080.7-80.7c3.9-3.9%2010.2-3.9%2014.1%200%203.9%203.9%203.9%2010.2%200%2014.1l-87.8%2087.8c-1.9%202-4.4%203-7%203z%22%2F%3E%3C%2Fsvg%3E);
146+
}
147+
.chevron-up {
148+
background: url(data:image/svg+xml,%3Csvg%20id%3D%22Layer_1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20195.6%20107.8%22%3E%3Cpath%20fill%3D%22%23B5B5B5%22%20class%3D%22st0%22%20d%3D%22M97.8%20107.8c-2.6%200-5.1-1-7.1-2.9L2.9%2017.1C-1%2013.2-1%206.8%202.9%202.9%206.8-1%2013.2-1%2017.1%202.9l80.7%2080.7%2080.7-80.7c3.9-3.9%2010.2-3.9%2014.1%200%203.9%203.9%203.9%2010.2%200%2014.1l-87.8%2087.8c-1.9%202-4.4%203-7%203z%22%2F%3E%3C%2Fsvg%3E);
149+
transform: rotate(.5turn);
150+
}
151+
`
152+
153+
const actual = analyze(fixture).stylesheet.embeddedContent
154+
const expected = {
155+
total: 2,
156+
totalUnique: 1,
157+
unique: {
158+
// .length == 459
159+
'data:image/svg+xml,%3Csvg%20id%3D%22Layer_1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20195.6%20107.8%22%3E%3Cpath%20fill%3D%22%23B5B5B5%22%20class%3D%22st0%22%20d%3D%22M97.8%20107.8c-2.6%200-5.1-1-7.1-2.9L2.9%2017.1C-1%2013.2-1%206.8%202.9%202.9%206.8-1%2013.2-1%2017.1%202.9l80.7%2080.7%2080.7-80.7c3.9-3.9%2010.2-3.9%2014.1%200%203.9%203.9%203.9%2010.2%200%2014.1l-87.8%2087.8c-1.9%202-4.4%203-7%203z%22%2F%3E%3C%2Fsvg%3E': 2,
160+
},
161+
uniquenessRatio: 1 / 2,
162+
size: {
163+
total: 918,
164+
ratio: 0.8218442256042973,
165+
},
166+
types: {
167+
total: 2,
168+
totalUnique: 1,
169+
uniquenessRatio: 1 / 2,
170+
unique: {
171+
'image/svg+xml': {
172+
count: 2,
173+
size: 918,
174+
},
175+
},
176+
}
177+
}
178+
179+
assert.equal(actual, expected)
180+
})
181+
141182
Stylesheet('correctly extracts embed types', () => {
142183
;[
143184
['test', 'data:test;nothing-to-see-here'],

0 commit comments

Comments
 (0)