From 8b8bdf92e606bff1343b4ed4369fe5fdad566d33 Mon Sep 17 00:00:00 2001 From: Wil Wilsman Date: Fri, 12 Mar 2021 14:26:48 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Force=20@percy/dom=20globalizati?= =?UTF-8?q?on=20(#237)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐛 Force @percy/dom globalization Sometimes modules or exports are polyfilled causing the UMD bundle to not attach the DOM script to the global scope. Since this scope issue seems to keep coming up, let's always set the serialize function globally when the window object is defined. * ♻ Type check gobalThis rather than try-catch usage --- packages/dom/src/index.js | 11 +++-------- rollup.config.js | 3 +-- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/packages/dom/src/index.js b/packages/dom/src/index.js index f034a9832..c1bd918e7 100644 --- a/packages/dom/src/index.js +++ b/packages/dom/src/index.js @@ -1,14 +1,9 @@ import serialize from './serialize-dom'; /* istanbul ignore next */ -// works around instances where the context has an incorrect global scope -// https://github.com/mozilla/geckodriver/issues/1798 -try { - if (globalThis !== window) { - window.PercyDOM = { serialize }; - } -} catch (error) { - // `globalThis` is probably not defined +// works around instances where the context has an incorrect scope +if (typeof window !== 'undefined') { + window.PercyDOM = exports; } export { serialize }; diff --git a/rollup.config.js b/rollup.config.js index d0ffe75c1..a26e72942 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -45,8 +45,7 @@ const base = { ...pkg.rollup.output, intro: [ // provide the bundle with a fake process.env if needed - 'let process = {};', - 'try { process = globalThis.process || {}; } catch (e) {}', + 'const process = (typeof globalThis !== "undefined" && globalThis.process) || {};', 'process.env = process.env || {};', // signals that the package is running in a browserified bundle 'process.env.__PERCY_BROWSERIFIED__ = true;'