From 4c76a8e59d0a4144b414f7dfeed6c0b80465e293 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Tue, 11 Jul 2023 13:40:07 -0700 Subject: [PATCH] fix eval on new doc script --- core/gather/driver/execution-context.js | 35 +++++++++++++++---------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/core/gather/driver/execution-context.js b/core/gather/driver/execution-context.js index 6faef0f415b0..e4a2a7faf6ee 100644 --- a/core/gather/driver/execution-context.js +++ b/core/gather/driver/execution-context.js @@ -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=} 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 @@ -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} @@ -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};