Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(legacy-javascript): detect es-shims polyfills #15738

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions core/audits/byte-efficiency/legacy-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@
// Object.defineProperty(String.prototype, 'startsWith'
expression += `|defineProperty\\(${object || 'window'},\\s?${qt(property)}`;

// es-shims
// no(Object,{entries:r},{entries:function
if (object) {
expression += `|\\(${object},\s*{${property}:.*},\s*{${property}`;
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
connorjclark marked this conversation as resolved.
Show resolved Hide resolved
}

// core-js
if (object) {
const objectWithoutPrototype = object.replace('.prototype', '');
Expand Down Expand Up @@ -234,6 +240,9 @@
];

for (const [name, coreJs2Module] of coreJsPolyfills) {
// es-shims follows a pattern for its packages.
// Tack it onto the corejs size estimation, as it is likely close in size.
const esShimModule = name.toLowerCase();
data.push({
name,
modules: [
Expand All @@ -243,6 +252,7 @@
.replace('es6.', 'es.')
.replace('es7.', 'es.')
.replace('typed.', 'typed-array.'),
esShimModule,
],
corejs: true,
});
Expand Down Expand Up @@ -333,8 +343,9 @@
// Skip if the pattern matching found a match for this polyfill.
if (matches.some(m => m.name === name)) continue;

const source = bundle.rawMap.sources.find(source =>
modules.some(module => source.endsWith(`${module}.js`)));
const source = bundle.rawMap.sources.find(source => modules.some(module => {
return source.endsWith(`/${module}.js`) || source.includes(`node_modules/${module}/`);
}));

Check warning on line 348 in core/audits/byte-efficiency/legacy-javascript.js

View check run for this annotation

Codecov / codecov/patch

core/audits/byte-efficiency/legacy-javascript.js#L346-L348

Added lines #L346 - L348 were not covered by tests
if (!source) continue;

const mapping = bundle.map.mappings().find(m => m.sourceURL === source);
Expand Down
20 changes: 18 additions & 2 deletions core/test/audits/byte-efficiency/legacy-javascript-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ describe('LegacyJavaScript audit', () => {
'Object.defineProperty(String.prototype, "repeat", function() {})',
'$export($export.S,"Object",{values:function values(t){return i(t)}})',
'String.raw = function() {}',
// es-shims (object.entries)
'no(Object,{entries:r},{entries:function',
'no(Array.prototype,{find:r},{find:function',
// Currently are no polyfills that declare a class. Maybe in the future.
// 'Object.defineProperty(window, \'WeakMap\', function() {})',
// 'WeakMap = function() {}',
Expand Down Expand Up @@ -255,7 +258,11 @@ describe('LegacyJavaScript audit', () => {

it('detects non-corejs modules from source maps', async () => {
const map = {
sources: ['node_modules/focus-visible/dist/focus-visible.js'],
sources: [
'node_modules/focus-visible/dist/focus-visible.js',
'node_modules/array.prototype.find/index.js',
'node_modules/object.entries/index.js',
],
mappings: 'blah',
};
const script = {
Expand All @@ -266,13 +273,22 @@ describe('LegacyJavaScript audit', () => {
const result = await getResult([script]);

expect(result.items).toHaveLength(1);
expect(result.items[0].subItems.items).toHaveLength(3);
expect(result.items[0].subItems.items).toMatchObject([
{
signal: 'focus-visible',
location: {line: 0, column: 0},
},
{
signal: 'Array.prototype.find',
location: {line: 0, column: 0},
},
{
signal: 'Object.entries',
location: {line: 0, column: 0},
},
]);
expect(result.items[0].wastedBytes).toBe(3000);
expect(result.items[0].wastedBytes).toBe(38062);
});
});

Expand Down
Loading