Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: UMD cannot run in strict mode #3689

Closed
Jack-Works opened this issue Jul 22, 2020 · 5 comments · Fixed by #3691
Closed

Bug: UMD cannot run in strict mode #3689

Jack-Works opened this issue Jul 22, 2020 · 5 comments · Fixed by #3691

Comments

@Jack-Works
Copy link

Expected Behavior

(function (global, factory) {
	typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
	typeof define === 'function' && define.amd ? define(factory) :
	(global = global || self, global.myBundle = factory());
}(this || globalThis, (function () { 'use strict';

	var main = 1;

	return main;

})));

Actual Behavior

(function (global, factory) {
	typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
	typeof define === 'function' && define.amd ? define(factory) :
	(global = global || self, global.myBundle = factory());
}(this, (function () { 'use strict';

	var main = 1;

	return main;

})));

Related: facebook/react#18040

@Jack-Works
Copy link
Author

the global = global || self the trick only works for the browser. We need globalThis to make it work on other environments.

@lukastaegert
Copy link
Member

It works for all environments that have either this or self point to the global object. The problem is

  • on older systems, there is no globalThis so we have to move that one after other options otherwise it will throw with an Uncaught ReferenceError
  • on Node, there is no self, so we would need to move self after globalThis

No matter how we turn it, we will likely need to probe one of the options via typeof. Maybe global = global || typeof self === 'undefined' ? globalThis : self? Other suggestions?

@lukastaegert
Copy link
Member

To make it nicer, we could also move this into the first argument.

@Jack-Works
Copy link
Author

The lodash way is good enough to me.
https://github.com/lodash/lodash/pull/4347/files
Besides, I have a request: please make the globalThis at the first place.

Here is my reason: in Firefox web extension content script, globalThis does not equal to window. globalThis means the current global object (sandbox) and the window means the global object out of the sandbox.

@lukastaegert
Copy link
Member

Makes sense. I assume #3691 would work for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants