Skip to content

Commit 6b2d0f0

Browse files
authored
core: append sourceURL comment to eval code (#13754)
1 parent d5327b0 commit 6b2d0f0

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

lighthouse-core/gather/driver/execution-context.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ class ExecutionContext {
9797
.catch(${pageFunctions.wrapRuntimeEvalErrorInBrowserString})
9898
.then(resolve);
9999
});
100-
}())`,
100+
}())
101+
//# sourceURL=_lighthouse-eval.js`,
101102
includeCommandLineAPI: true,
102103
awaitPromise: true,
103104
returnByValue: true,
@@ -195,7 +196,8 @@ class ExecutionContext {
195196
${ExecutionContext._cachedNativesPreamble};
196197
${depsSerialized};
197198
(${mainFn})(${argsSerialized});
198-
})()`;
199+
})()
200+
//# sourceURL=_lighthouse-eval.js`;
199201

200202
await this._session.sendCommand('Page.addScriptToEvaluateOnNewDocument', {source: expression});
201203
}

lighthouse-core/gather/gatherers/js-usage.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ class JsUsage extends FRGatherer {
5050
const usageByScriptId = {};
5151

5252
for (const scriptUsage of this._scriptUsages) {
53-
// If `url` is blank, that means the script was anonymous (eval, new Function, onload, ...).
54-
// Or, it's because it was code Lighthouse over the protocol via `Runtime.evaluate`.
55-
// We currently don't consider coverage of anonymous scripts, and we definitely don't want
56-
// coverage of code Lighthouse ran to inspect the page, so we ignore this ScriptCoverage if
57-
// url is blank.
58-
if (scriptUsage.url === '') {
53+
// If `url` is blank, that means the script was dynamically
54+
// created (eval, new Function, onload, ...)
55+
if (scriptUsage.url === '' || scriptUsage.url === '_lighthouse-eval.js') {
56+
// We currently don't consider coverage of dynamic scripts, and we definitely don't want
57+
// coverage of code Lighthouse ran to inspect the page, so we ignore this ScriptCoverage.
58+
// Audits would work the same without this, it is only an optimization (not tracking coverage
59+
// for scripts we don't care about).
5960
continue;
6061
}
6162

lighthouse-core/gather/gatherers/scripts.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ async function runInSeriesOrParallel(values, promiseMapper, runInSeries) {
2828
}
2929
}
3030

31+
/**
32+
* Returns true if the script was created via our own calls
33+
* to Runtime.evaluate.
34+
* @param {LH.Crdp.Debugger.ScriptParsedEvent} script
35+
*/
36+
function isLighthouseRuntimeEvaluateScript(script) {
37+
// Scripts created by Runtime.evaluate that run on the main session/frame
38+
// result in an empty string for the embedderName.
39+
// Or, it means the script was dynamically created (eval, new Function, onload, ...)
40+
if (!script.embedderName) return true;
41+
42+
// Otherwise, when running our own code inside other frames, the embedderName
43+
// is set to the frame's url. In that case, we rely on the special sourceURL that
44+
// we set.
45+
return script.hasSourceURL && script.url === '_lighthouse-eval.js';
46+
}
47+
3148
/**
3249
* @fileoverview Gets JavaScript file contents.
3350
*/
@@ -68,8 +85,9 @@ class Scripts extends FRGatherer {
6885
// it also blocks scripts from the same origin but that happen to run in a different process,
6986
// like a worker.
7087
if (event.method === 'Debugger.scriptParsed' && !sessionId) {
71-
// Events without an embedderName (read: a url) are for JS that we ran over the protocol.
72-
if (event.params.embedderName) this._scriptParsedEvents.push(event.params);
88+
if (!isLighthouseRuntimeEvaluateScript(event.params)) {
89+
this._scriptParsedEvents.push(event.params);
90+
}
7391
}
7492
}
7593

lighthouse-core/test/gather/driver/execution-context-test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ const fetch = globalThis.__nativeFetch || globalThis.fetch;
241241
})
242242
.then(resolve);
243243
});
244-
}())`.trim();
244+
}())
245+
//# sourceURL=_lighthouse-eval.js`.trim();
245246
expect(trimTrailingWhitespace(expression)).toBe(trimTrailingWhitespace(expected));
246247
expect(await eval(expression)).toBe(1);
247248
});

0 commit comments

Comments
 (0)