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

Check for globalThis in UMD wrapper #3691

Merged
merged 3 commits into from Jul 23, 2020
Merged

Conversation

lukastaegert
Copy link
Member

@lukastaegert lukastaegert commented Jul 23, 2020

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

List any relevant issue numbers:
resolves #3666
resolves #3689

Description

This will add a check for globalThis to the UMD wrapper to find the global variable with higher priority than this and self. The latter are kept for compatibility with older environments of course. I briefly considered moving the check into the function argument where the this resides but decided against it to avoid performing this check in CJS and AMD environments. Thus you can still run the bundle in such an environment when none of the three global object options are available.

Note that we need to scan for globalThis with a typeof check to not throw on older environments.

Before:

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

	console.log(foo.foo);

})));

After:

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

	console.log(foo.foo);

})));

@rollup-bot
Copy link
Collaborator

rollup-bot commented Jul 23, 2020

Thank you for your contribution! ❤️

You can try out this pull request locally by installing Rollup via

npm install rollup/rollup#gh-3689-umd-globalthis

or load it into the REPL:
https://rollupjs.org/repl/?circleci=12401

@codecov
Copy link

codecov bot commented Jul 23, 2020

Codecov Report

Merging #3691 into master will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3691      +/-   ##
==========================================
+ Coverage   96.78%   96.80%   +0.01%     
==========================================
  Files         183      183              
  Lines        6319     6319              
  Branches     1843     1842       -1     
==========================================
+ Hits         6116     6117       +1     
  Misses        105      105              
+ Partials       98       97       -1     
Impacted Files Coverage Δ
src/finalisers/umd.ts 100.00% <100.00%> (+1.72%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e59dda1...ecb1201. Read the comment docs.

@Jack-Works
Copy link

Great! Thanks!

@frank-dspeed
Copy link
Contributor

LGTM +1

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 this pull request may close these issues.

Bug: UMD cannot run in strict mode Extend UMD loader to use globalThis
4 participants