Skip to content

Commit

Permalink
core(legacy-javascript): reduce polyfills, fix core-js import in test (
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored and George Martin committed Jul 6, 2020
1 parent 6a4b9cf commit 8e6b019
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 688 deletions.
2 changes: 1 addition & 1 deletion lighthouse-cli/test/fixtures/legacy-javascript.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
wanna cracker?
<script src="/legacy-javascript.js"></script>
<script>
String.prototype.includes = function() {};
Array.prototype.findIndex = function() {};
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion lighthouse-cli/test/fixtures/legacy-javascript.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,56 @@ module.exports = [
url: 'http://localhost:10200/legacy-javascript.js',
subItems: {
items: [
{signal: 'Array.prototype.fill'},
{signal: 'Array.prototype.filter'},
{signal: 'Array.prototype.findIndex'},
{signal: 'Array.prototype.find'},
{signal: 'Array.prototype.forEach'},
{signal: 'Array.from'},
{signal: 'Array.prototype.includes'},
{signal: 'Array.isArray'},
{signal: 'Array.prototype.map'},
{signal: 'Array.of'},
{signal: 'Array.prototype.reduceRight'},
{signal: 'Array.prototype.reduce'},
{signal: 'Array.prototype.some'},
{signal: 'Date.now'},
{signal: 'Date.prototype.toISOString'},
{signal: 'Date.prototype.toJSON'},
{signal: 'Number.isInteger'},
{signal: 'Number.isSafeInteger'},
{signal: 'Number.parseInt'},
{signal: 'Object.defineProperties'},
{signal: 'Object.defineProperty'},
{signal: 'Object.entries'},
{signal: 'Object.freeze'},
{signal: 'Object.getOwnPropertyDescriptors'},
{signal: 'Object.getOwnPropertyNames'},
{signal: 'Object.getPrototypeOf'},
{signal: 'Object.isExtensible'},
{signal: 'Object.isFrozen'},
{signal: 'Object.isSealed'},
{signal: 'Object.keys'},
{signal: 'Object.preventExtensions'},
{signal: 'Object.seal'},
{signal: 'Object.setPrototypeOf'},
{signal: 'Object.values'},
{signal: 'Reflect.apply'},
{signal: 'Reflect.construct'},
{signal: 'Reflect.defineProperty'},
{signal: 'Reflect.deleteProperty'},
{signal: 'Reflect.getOwnPropertyDescriptor'},
{signal: 'Reflect.getPrototypeOf'},
{signal: 'Reflect.get'},
{signal: 'Reflect.has'},
{signal: 'Reflect.isExtensible'},
{signal: 'Reflect.ownKeys'},
{signal: 'Reflect.preventExtensions'},
{signal: 'Reflect.setPrototypeOf'},
{signal: 'String.prototype.codePointAt'},
{signal: 'String.fromCodePoint'},
{signal: 'String.raw'},
{signal: 'String.prototype.repeat'},
{signal: '@babel/plugin-transform-classes'},
{signal: '@babel/plugin-transform-regenerator'},
{signal: '@babel/plugin-transform-spread'},
Expand All @@ -31,7 +81,7 @@ module.exports = [
url: 'http://localhost:10200/legacy-javascript.html',
subItems: {
items: [
{signal: 'String.prototype.includes'},
{signal: 'Array.prototype.findIndex'},
],
},
},
Expand Down
54 changes: 11 additions & 43 deletions lighthouse-core/audits/legacy-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

/** @typedef {{name: string, expression: string}} Pattern */
/** @typedef {{name: string, line: number, column: number}} PatternMatchResult */
/** @typedef {{url: string, subItems: {type: 'subitems', items: SubItem[]}}} Item */
/** @typedef {{signal: string, location: LH.Audit.Details.SourceLocationValue}} SubItem */

const Audit = require('./audit.js');
const NetworkRecords = require('../computed/network-records.js');
Expand Down Expand Up @@ -166,15 +168,13 @@ class LegacyJavascript extends Audit {

static getPolyfillData() {
return [
/* eslint-disable max-len */
['Array.prototype.fill', 'es6.array.fill'],
['Array.prototype.filter', 'es6.array.filter'],
['Array.prototype.find', 'es6.array.find'],
['Array.prototype.findIndex', 'es6.array.find-index'],
['Array.prototype.forEach', 'es6.array.for-each'],
['Array.from', 'es6.array.from'],
['Array.isArray', 'es6.array.is-array'],
['Array.prototype.lastIndexOf', 'es6.array.last-index-of'],
['Array.prototype.map', 'es6.array.map'],
['Array.of', 'es6.array.of'],
['Array.prototype.reduce', 'es6.array.reduce'],
Expand All @@ -185,17 +185,12 @@ class LegacyJavascript extends Audit {
['Date.prototype.toJSON', 'es6.date.to-json'],
['Date.prototype.toString', 'es6.date.to-string'],
['Function.prototype.name', 'es6.function.name'],
['Map', 'es6.map'],
['Number.isInteger', 'es6.number.is-integer'],
['Number.isSafeInteger', 'es6.number.is-safe-integer'],
['Number.parseFloat', 'es6.number.parse-float'],
['Number.parseInt', 'es6.number.parse-int'],
['Object.assign', 'es6.object.assign'],
['Object.create', 'es6.object.create'],
['Object.defineProperties', 'es6.object.define-properties'],
['Object.defineProperty', 'es6.object.define-property'],
['Object.freeze', 'es6.object.freeze'],
['Object.getOwnPropertyDescriptor', 'es6.object.get-own-property-descriptor'],
['Object.getOwnPropertyNames', 'es6.object.get-own-property-names'],
['Object.getPrototypeOf', 'es6.object.get-prototype-of'],
['Object.isExtensible', 'es6.object.is-extensible'],
Expand All @@ -205,7 +200,6 @@ class LegacyJavascript extends Audit {
['Object.preventExtensions', 'es6.object.prevent-extensions'],
['Object.seal', 'es6.object.seal'],
['Object.setPrototypeOf', 'es6.object.set-prototype-of'],
['Promise', 'es6.promise'],
['Reflect.apply', 'es6.reflect.apply'],
['Reflect.construct', 'es6.reflect.construct'],
['Reflect.defineProperty', 'es6.reflect.define-property'],
Expand All @@ -217,41 +211,16 @@ class LegacyJavascript extends Audit {
['Reflect.isExtensible', 'es6.reflect.is-extensible'],
['Reflect.ownKeys', 'es6.reflect.own-keys'],
['Reflect.preventExtensions', 'es6.reflect.prevent-extensions'],
['Reflect.set', 'es6.reflect.set'],
['Reflect.setPrototypeOf', 'es6.reflect.set-prototype-of'],
['Set', 'es6.set'],
['String.prototype.codePointAt', 'es6.string.code-point-at'],
['String.prototype.endsWith', 'es6.string.ends-with'],
['String.fromCodePoint', 'es6.string.from-code-point'],
['String.prototype.includes', 'es6.string.includes'],
['String.raw', 'es6.string.raw'],
['String.prototype.repeat', 'es6.string.repeat'],
['String.prototype.startsWith', 'es6.string.starts-with'],
['String.prototype.trim', 'es6.string.trim'],
// These break the coreJs2/coreJs3 naming pattern so are set explicitly.
{name: 'ArrayBuffer', coreJs2Module: 'es6.typed.array-buffer', coreJs3Module: 'es.array-buffer.constructor'},
{name: 'DataView', coreJs2Module: 'es6.typed.data-view', coreJs3Module: 'es.data-view'},
['Float32Array', 'es6.typed.float32-array'],
['Float64Array', 'es6.typed.float64-array'],
['Int16Array', 'es6.typed.int16-array'],
['Int32Array', 'es6.typed.int32-array'],
['Int8Array', 'es6.typed.int8-array'],
['Uint16Array', 'es6.typed.uint16-array'],
['Uint32Array', 'es6.typed.uint32-array'],
['Uint8Array', 'es6.typed.uint8-array'],
['Uint8ClampedArray', 'es6.typed.uint8-clamped-array'],
['WeakMap', 'es6.weak-map'],
['WeakSet', 'es6.weak-set'],
['Array.prototype.includes', 'es7.array.includes'],
['Object.entries', 'es7.object.entries'],
['Object.getOwnPropertyDescriptors', 'es7.object.get-own-property-descriptors'],
['Object.values', 'es7.object.values'],
['String.prototype.padEnd', 'es7.string.pad-end'],
['String.prototype.padStart', 'es7.string.pad-start'],
/* eslint-enable max-len */
].map(data => {
if (!Array.isArray(data)) return data;

const [name, coreJs2Module] = data;
return {
name,
Expand Down Expand Up @@ -358,9 +327,8 @@ class LegacyJavascript extends Audit {
const networkRecords = await NetworkRecords.request(devtoolsLog, context);
const bundles = await JSBundles.request(artifacts, context);

/** @typedef {{signal: string, location: LH.Audit.Details.SourceLocationValue}} SubItem */
/** @type {Array<{url: string, subItems: LH.Audit.Details.TableSubItems}>} */
const tableRows = [];
/** @type {Item[]} */
const items = [];
let signalCount = 0;

// TODO(cjamcl): Use SourceMaps, and only pattern match if maps are not available.
Expand All @@ -372,8 +340,8 @@ class LegacyJavascript extends Audit {
const urlToMatchResults =
this.detectAcrossScripts(matcher, artifacts.ScriptElements, networkRecords, bundles);
urlToMatchResults.forEach((matches, url) => {
/** @type {typeof tableRows[number]} */
const row = {
/** @type {typeof items[number]} */
const item = {
url,
subItems: {
type: 'subitems',
Expand All @@ -393,10 +361,10 @@ class LegacyJavascript extends Audit {
urlProvider: 'network',
},
};
row.subItems.items.push(subItem);
item.subItems.items.push(subItem);
}
tableRows.push(row);
signalCount += row.subItems.items.length;
items.push(item);
signalCount += item.subItems.items.length;
});

/** @type {LH.Audit.Details.Table['headings']} */
Expand All @@ -406,11 +374,11 @@ class LegacyJavascript extends Audit {
{key: null, itemType: 'code', subHeading: {key: 'signal'}, text: ''},
/* eslint-enable max-len */
];
const details = Audit.makeTableDetails(headings, tableRows);
const details = Audit.makeTableDetails(headings, items);

// Only fail if first party code has legacy code.
const mainDocumentEntity = thirdPartyWeb.getEntity(artifacts.URL.finalUrl);
const foundSignalInFirstPartyCode = tableRows.some(row => {
const foundSignalInFirstPartyCode = items.some(row => {
return thirdPartyWeb.isFirstParty(row.url, mainDocumentEntity);
});
return {
Expand Down
2 changes: 2 additions & 0 deletions lighthouse-core/scripts/legacy-javascript/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
class MyTestClass {};
async function reg(...args) {
await 1;
for (let i = 0; i < 10; i++) await 2;
await 3;
}
const spread = [...[1,2,3], 3, 2, 1];
reg(...spread);
39 changes: 27 additions & 12 deletions lighthouse-core/scripts/legacy-javascript/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,17 @@ function makeSummary(legacyJavascriptFilename) {
let totalSignals = 0;
const variants = [];
for (const dir of glob.sync('*/*', {cwd: VARIANT_DIR})) {
/** @type {Array<{signals: string[]}>} */
/** @type {import('../../audits/legacy-javascript.js').Item[]} */
const legacyJavascriptItems = require(`${VARIANT_DIR}/${dir}/${legacyJavascriptFilename}`);
const signals = legacyJavascriptItems.reduce((acc, cur) => {
totalSignals += cur.signals.length;
return acc.concat(cur.signals);
}, /** @type {string[]} */ ([])).join(', ');
variants.push({name: dir, signals});

const signals = [];
for (const item of legacyJavascriptItems) {
for (const subItem of item.subItems.items) {
signals.push(subItem.signal);
}
}
totalSignals += signals.length;
variants.push({name: dir, signals: signals.join(', ')});
}
return {
totalSignals,
Expand Down Expand Up @@ -227,13 +231,17 @@ function makeRequireCodeForPolyfill(module) {
}

async function main() {
for (const plugin of plugins) {
const pluginGroups = [
...plugins.map(plugin => [plugin]),
['@babel/plugin-transform-regenerator', '@babel/transform-async-to-generator'],
];
for (const pluginGroup of pluginGroups) {
await createVariant({
group: 'only-plugin',
name: plugin,
name: pluginGroup.join('_'),
code: mainCode,
babelrc: {
plugins: [plugin],
plugins: pluginGroup,
},
});
}
Expand All @@ -242,11 +250,17 @@ async function main() {
removeCoreJs();
installCoreJs(coreJsVersion);

for (const esmodules of [true, false]) {
const moduleOptions = [
{esmodules: false},
// Output: https://gist.github.com/connorjclark/515d05094ffd1fc038894a77156bf226
{esmodules: true},
{esmodules: true, bugfixes: true},
];
for (const {esmodules, bugfixes} of moduleOptions) {
await createVariant({
group: `core-js-${coreJsVersion}-preset-env-esmodules`,
name: String(esmodules),
code: mainCode,
name: String(esmodules) + (bugfixes ? '_and_bugfixes' : ''),
code: `require('core-js');\n${mainCode}`,
babelrc: {
presets: [
[
Expand All @@ -255,6 +269,7 @@ async function main() {
targets: {esmodules},
useBuiltIns: 'entry',
corejs: coreJsVersion,
bugfixes,
},
],
],
Expand Down
Loading

0 comments on commit 8e6b019

Please sign in to comment.