Reputation: 799
I have UMD ES2015 module
(function (global, factory) {
(typeof exports === 'undefined' ? 'undefined' : _typeof(exports)) === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : factory(global.XmlDSigJs = global.XmlDSigJs || {});
})(this, function (exports) {
// My code here
...
After Babel compilation I've got (undefined, function (exports)
.
Is there any bable options to bock this changing?
Upvotes: 0
Views: 322
Reputation: 161517
If your code is already written as a UMD module, and thus does not use import
or export
, then you can do
"presets": [
['es2015', {modules: false}]
],
Pass modules: false
to your configured preset will tell Babel not to consider the file an "ES6 Module", which is what is triggering the behavior you are seeing.
Upvotes: 1
Reputation: 1074959
You've said this is an "ES2015 module." this
at the top-level scope of an ES2015 module is undefined
. If your code expects it to be something else, that code is incorrect.
Normally, you don't refer to the global object or create globals in modules at all; that's part of the reason we have modules, to avoid global pollution.
In those rare cases where you need to, the way you do that varies depending on the environment. In browsers, you can use window
, which is a global variable that refers to the window object, which is the JavaScript global object in browsers. In NodeJS, you can use global
, which is a global variable referring to the JavaScript global object.
But again, creating globals in a module is an anti-pattern. A module should export what it provides, not make it global.
Upvotes: 1