Resource batching breaks Closure-compiled ES6 code in Safari

When JS resources are batched in Confluence Data Center, the contents of each resource are wrapped in a try block. This block seems to trigger this bug in Safari, which means ES6 code easily breaks with ReferenceError: can't find variable: x. (This applies to the Closure-compiled code we use, but I don’t think it’s specific to Closure, it just happens that Closure tends to output functions/classes/etc as top-level declarations like this.)

image

(To be clear, the culprit here is definitely Safari. The resource batching used by Confluence just triggers the bug in Safari.)

Is there a way to disable batching for certain resources? Based on this answer, it doesn’t sound like there is, but it’s 2023 so “we have to support IE” is much less compelling than it was back then :slight_smile:

We found a workaround for this: wrapping all our scripts in IIFEs, so it ends up looking like this:

try {
  (function() {
    ... script code here ...
  })()
} catch (err) {
  ...
}

This works because it puts a function scope (which behaves correctly in Safari) in between the ES6 declarations in our code and the try block added by Confluence.