Hi @fboucquez, I will find some time to check your example later this afternoon and update this comment with what I can figure out for you.
Update 5:03pm: this problem is occurring only when the files are batched. The problem is not due to your code, but rather to a cascading JavaScript error: as soon as a single uncaught exception occurs in the batch.js file, the rest of it is not executed, which means the jira/projects/sidebar/expansion-manager
module doesn’t get defined, which means things depending upon it don’t work, which means other files stop working, and so on and so forth.
The problem is emanating from Jira’s instantiation of the moment
library. The curious thing is that moment registers itself just fine on most other pages… what’s special about this one?
It turns out the problem is due to a bad interop between Webpack, Babel Runtime (core-js), Moment, Jira, and its JS libraries.
- Your webpacked code includes the babel-runtime and babel-polyfill code – aka, core-js. The code in core-js defines
var global = // effectively window
.
- Moment.js tries to be a good citizen: if it sees that
global
exists, it exports itself to that. Otherwise, it exports itself to this
.
- The presence of core-js makes it export to
global
.
- JSLibs expects
global
to not exist, so that moment will be exported to this
.
- JSLibs fails to register the library correctly as an AMD module.
- Jira doesn’t pick up the library.
- The cascade of errors begins.
This is, in essence, a latent bug in Jira. As such, I’ve raised [JRASERVER-67644] Cascade of JavaScript errors caused if `window.global` is defined - Create and track feature requests for Atlassian products..
Of course, the bug is only caused if window.global
is defined. How is that getting on to the page? Through your inclusion of babel-polyfill
in your feature’s entrypoint.
To avoid this, I can see two options:
- Compile to es5 syntax; avoid use of the features that require the
babel-polyfill
at runtime (mainly the new primitives like Map
, Set
, Object.prototype
and Array.prototype
methods).
- Install the
transform-runtime
babel plugin, then configure it to include the stuff you need in a way that does not pollute the global scope:// .babelrc
{
"plugins": [
["transform-runtime", {
"helpers": false,
"polyfill": true,
"regenerator": false,
"moduleName": "babel-runtime"
}]
]
}
I’m aware of discussions within the Atlassian developer community about babel-polyfill
and the runtime that products like Jira provide. As more vendors try to use language features that don’t exist in Atlassian’s supported browsers, interoperability problems like this continue to crop up. It may make sense for Atlassian products to provide a common runtime, though there are tradeoffs and risks to consider. That’s a separate discussion which I’ll start elsewhere.
Hope that helps get you unstuck in the short term.
Cheers,
Daz