Skip to content

Commit

Permalink
fix eval on new doc script
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Jul 11, 2023
1 parent 0860d8f commit 4c76a8e
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions core/gather/driver/execution-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ class ExecutionContext {
}
}

/**
* Serializes an array of functions or strings.
*
* Also makes sure that an esbuild-bundled version of Lighthouse will
* continue to create working code to be executed within the browser.
* @param {Array<Function|string>=} deps
* @return {string}
*/
_serializeDeps(deps) {
deps = [pageFunctions.esbuildFunctionNameStubString, ...deps || []];
return deps.map(dep => {
if (typeof dep === 'function') {
return `const ${dep.name} = ${dep}`;
} else {
return dep;
}
}).join('\n');
}

/**
* Note: Prefer `evaluate` instead.
* Evaluate an expression in the context of the current page. If useIsolation is true, the expression
Expand Down Expand Up @@ -197,13 +216,7 @@ class ExecutionContext {
*/
evaluate(mainFn, options) {
const argsSerialized = ExecutionContext.serializeArguments(options.args);
const depsSerialized = (options.deps || []).map(dep => {
if (typeof dep === 'function') {
return `const ${dep.name} = ${dep}`;
} else {
return dep;
}
}).join('\n');
const depsSerialized = this._serializeDeps(options.deps);

const expression = `(() => {
${depsSerialized}
Expand All @@ -223,13 +236,7 @@ class ExecutionContext {
*/
async evaluateOnNewDocument(mainFn, options) {
const argsSerialized = ExecutionContext.serializeArguments(options.args);
const depsSerialized = (options.deps || []).map(dep => {
if (typeof dep === 'function') {
return `const ${dep.name} = ${dep}`;
} else {
return dep;
}
}).join('\n');
const depsSerialized = this._serializeDeps(options.deps);

const expression = `(() => {
${ExecutionContext._cachedNativesPreamble};
Expand Down

0 comments on commit 4c76a8e

Please sign in to comment.