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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/finalisers/umd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function safeAccess(name: string, globalVar: string, _: string) {
const parts = name.split('.');

let acc = globalVar;
return parts.map(part => ((acc += property(part)), acc)).join(`${_}&&${_}`);
return parts.map(part => (acc += property(part))).join(`${_}&&${_}`);
}

export default function umd(
Expand Down Expand Up @@ -97,7 +97,7 @@ export default function umd(
options.compact,
`${factoryVar}(${globalDeps.join(`,${_}`)})`
)};`;
} else if (namedExportsMode) {
} else {
const module = globalDeps.shift();
factory =
`var ${noConflictExportsVar}${_}=${_}${module};${n}` +
Expand Down Expand Up @@ -129,7 +129,9 @@ export default function umd(
hasExports || (options.noConflict === true && namedExportsMode) || globalDeps.length > 0;
const globalParam = iifeNeedsGlobal ? `${globalVar},${_}` : '';
const globalArg = iifeNeedsGlobal ? `this,${_}` : '';
const iifeStart = iifeNeedsGlobal ? `(${globalVar}${_}=${_}${globalVar}${_}||${_}self,${_}` : '';
const iifeStart = iifeNeedsGlobal
? `(${globalVar}${_}=${_}typeof globalThis${_}!==${_}'undefined'${_}?${_}globalThis${_}:${_}${globalVar}${_}||${_}self,${_}`
: '';
const iifeEnd = iifeNeedsGlobal ? ')' : '';
const cjsIntro = iifeNeedsGlobal
? `${t}typeof exports${_}===${_}'object'${_}&&${_}typeof module${_}!==${_}'undefined'${_}?` +
Expand Down
2 changes: 1 addition & 1 deletion test/cli/samples/format-aliases/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('external')) :
typeof define === 'function' && define.amd ? define(['exports', 'external'], factory) :
(global = global || self, factory(global.bundle = {}, global.external));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.bundle = {}, global.external));
}(this, (function (exports, external) { 'use strict';

external = external && Object.prototype.hasOwnProperty.call(external, 'default') ? external['default'] : external;
Expand Down
2 changes: 1 addition & 1 deletion test/cli/samples/module-name/_expected.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(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());
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.myBundle = factory());
}(this, (function () { 'use strict';

var main = 42;
Expand Down
4 changes: 2 additions & 2 deletions test/cli/samples/no-conflict/_expected.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, (function () {
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, (function () {
var current = global.conflictyName;
var exports = global.conflictyName = factory();
exports.noConflict = function () { global.conflictyName = current; return exports; };
Expand All @@ -12,4 +12,4 @@

return main;

})));
})));
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.bundle = {}));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.bundle = {}));
}(this, (function (exports) { 'use strict';

var buffer = new ArrayBuffer( 8 );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.myModule = {}));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.myModule = {}));
}(this, (function (exports) { 'use strict';

exports.Foo = class Foo {};
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/assignment-to-exports/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.bundle = {}));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.bundle = {}));
}(this, (function (exports) { 'use strict';

// Unassigned export
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/form/samples/compact-named-export/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?f(exports):typeof define==='function'&&define.amd?define(['exports'],f):(g=g||self,f(g.foo={}));}(this,(function(exports){'use strict';exports.x = 42;
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?f(exports):typeof define==='function'&&define.amd?define(['exports'],f):(g=typeof globalThis!=='undefined'?globalThis:g||self,f(g.foo={}));}(this,(function(exports){'use strict';exports.x = 42;
exports.x+=1;
exports.x=exports.x+1;
exports.x++;
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/compact/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?module.exports=f(require('external')):typeof define==='function'&&define.amd?define(['external'],f):(g=g||self,g.foo=f(g.x));}(this,(function(x){'use strict';x=x&&Object.prototype.hasOwnProperty.call(x,'default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({__proto__:null,[Symbol.toStringTag]:'Module',get default(){return foo}});console.log(self);
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?module.exports=f(require('external')):typeof define==='function'&&define.amd?define(['external'],f):(g=typeof globalThis!=='undefined'?globalThis:g||self,g.foo=f(g.x));}(this,(function(x){'use strict';x=x&&Object.prototype.hasOwnProperty.call(x,'default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({__proto__:null,[Symbol.toStringTag]:'Module',get default(){return foo}});console.log(self);
function foo () {
console.log( x );
}
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/computed-properties/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.computedProperties = {}));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.computedProperties = {}));
}(this, (function (exports) { 'use strict';

var foo = 'foo';
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/conflicting-imports/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('foo'), require('bar')) :
typeof define === 'function' && define.amd ? define(['foo', 'bar'], factory) :
(global = global || self, factory(global.foo, global.bar));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.foo, global.bar));
}(this, (function (foo, bar) { 'use strict';

console.log( bar.a );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.bundle = {}));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.bundle = {}));
}(this, (function (exports) { 'use strict';

const exports$1 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('external')) :
typeof define === 'function' && define.amd ? define(['external'], factory) :
(global = global || self, global.bundle = factory(global.external));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.bundle = factory(global.external));
}(this, (function (external) { 'use strict';

external = external && Object.prototype.hasOwnProperty.call(external, 'default') ? external['default'] : external;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('external')) :
typeof define === 'function' && define.amd ? define(['exports', 'external'], factory) :
(global = global || self, factory(global.myBundle = {}, global.external));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.myBundle = {}, global.external));
}(this, (function (exports, external) { 'use strict';

class Foo extends external.Component {
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/default-export-class/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.bundle = factory());
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.bundle = factory());
}(this, (function () { 'use strict';

class main {
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/default-export-mode/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.bundle = factory());
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.bundle = factory());
}(this, (function () { 'use strict';

var main = 42;
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/define-es-modules-false/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.foo = {}));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.foo = {}));
}(this, (function (exports) { 'use strict';

const make1 = () => {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('other')) :
typeof define === 'function' && define.amd ? define(['other'], factory) :
(global = global || self, factory(global.other));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.other));
}(this, (function (other) { 'use strict';


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('other')) :
typeof define === 'function' && define.amd ? define(['other'], factory) :
(global = global || self, factory(global.other));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.other));
}(this, (function (other) { 'use strict';


Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/export-all-before-named/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('external')) :
typeof define === 'function' && define.amd ? define(['exports', 'external'], factory) :
(global = global || self, factory(global.exposedInternals = {}, global.external));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.exposedInternals = {}, global.external));
}(this, (function (exports, external) { 'use strict';

function internalFn(path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.exposedInternals = {}));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.exposedInternals = {}));
}(this, (function (exports) { 'use strict';

const a = 1;
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/export-all-multiple/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('foo'), require('bar'), require('baz')) :
typeof define === 'function' && define.amd ? define(['exports', 'foo', 'bar', 'baz'], factory) :
(global = global || self, factory(global.myBundle = {}, global.foo, global.bar, global.baz));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.myBundle = {}, global.foo, global.bar, global.baz));
}(this, (function (exports, foo, bar, baz) { 'use strict';

Object.keys(foo).forEach(function (k) {
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/export-default-2/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(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());
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.myBundle = factory());
}(this, (function () { 'use strict';

var bar = 1;
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/export-default-3/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(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());
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.myBundle = factory());
}(this, (function () { 'use strict';

var bar = 1;
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/export-default-4/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, (global.my = global.my || {}, global.my.global = global.my.global || {}, global.my.global.namespace = factory()));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, (global.my = global.my || {}, global.my.global = global.my.global || {}, global.my.global.namespace = factory()));
}(this, (function () { 'use strict';

var main = 42;
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/export-default-global/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.bundle = factory());
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.bundle = factory());
}(this, (function () { 'use strict';

var value = global;
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/export-default-import/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('x')) :
typeof define === 'function' && define.amd ? define(['exports', 'x'], factory) :
(global = global || self, factory(global.myBundle = {}, global.x));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.myBundle = {}, global.x));
}(this, (function (exports, x) { 'use strict';

x = x && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/export-default/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(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());
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.myBundle = factory());
}(this, (function () { 'use strict';

var main = 42;
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/export-globals/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.myBundle = {}));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.myBundle = {}));
}(this, (function (exports) { 'use strict';

const localIsNaN = isNaN;
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/export-live-bindings/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.iife = {}));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.iife = {}));
}(this, (function (exports) { 'use strict';

function update () {
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/export-namespace-as/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.bundle = {}));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.bundle = {}));
}(this, (function (exports) { 'use strict';

const foo = 'foo1';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.myBundle = {}));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.myBundle = {}));
}(this, (function (exports) { 'use strict';

var FOO = 'foo';
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/extend-exports/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.foo = global.foo || {}));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.foo = global.foo || {}));
}(this, (function (exports) { 'use strict';

const answer = 42;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory((global.foo = global.foo || {}, global.foo.bar = global.foo.bar || {}, global.foo.bar.baz = global.foo.bar.baz || {})));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.foo = global.foo || {}, global.foo.bar = global.foo.bar || {}, global.foo.bar.baz = global.foo.bar.baz || {})));
}(this, (function (exports) { 'use strict';

const answer = 42;
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/external-deshadowing/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('a'), require('b')) :
typeof define === 'function' && define.amd ? define(['exports', 'a', 'b'], factory) :
(global = global || self, factory(global.myBundle = {}, global.a, global.b));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.myBundle = {}, global.a, global.b));
}(this, (function (exports, a, Test$1) { 'use strict';

Test$1 = Test$1 && Object.prototype.hasOwnProperty.call(Test$1, 'default') ? Test$1['default'] : Test$1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('babel-polyfill'), require('other')) :
typeof define === 'function' && define.amd ? define(['babel-polyfill', 'other'], factory) :
(global = global || self, global.myBundle = factory(null, global.other));
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.myBundle = factory(null, global.other));
}(this, (function (babelPolyfill, other) { 'use strict';

other.x();
Expand Down