Skip to content

Commit

Permalink
🐛 Force @percy/dom globalization (#237)
Browse files Browse the repository at this point in the history
* 🐛 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
  • Loading branch information
Wil Wilsman authored Mar 12, 2021
1 parent e9b68bc commit 8b8bdf9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
11 changes: 3 additions & 8 deletions packages/dom/src/index.js
Original file line number Diff line number Diff line change
@@ -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 };
Expand Down
3 changes: 1 addition & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;'
Expand Down

0 comments on commit 8b8bdf9

Please sign in to comment.