Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/core/has.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,5 @@ add('dom-pointer-events', () => has('host-browser') && global.onpointerdown !==
add('build-elide', false);

add('test', false);

add('global-this', () => typeof global.globalThis !== 'undefined');
3 changes: 3 additions & 0 deletions src/shim/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const globalObject: any = (function(): any {
// the only reliable means to get the global object is
// `Function('return this')()`
// However, this causes CSP violations in Chrome apps.
if (typeof globalThis !== 'undefined') {
return globalThis;
}
if (typeof self !== 'undefined') {
return self;
}
Expand Down
6 changes: 6 additions & 0 deletions src/shim/globalThis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import global from './global';
import has from '../core/has';

if (!has('global-this')) {
global.globalThis = global;
}
1 change: 1 addition & 0 deletions tests/shim/unit/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import './util/wrapper';
import './AbortController';
import './array';
import './global';
import './globalThis';
import './iterator';
import './main';
import './Map';
Expand Down
11 changes: 11 additions & 0 deletions tests/shim/unit/globalThis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import globalObj from '../../../src/shim/global';
import '../../../src/shim/globalThis';

const { registerSuite } = intern.getInterface('object');
const { assert } = intern.getPlugin('chai');

registerSuite('globalThis', {
'ensures that the global object is available as `globalThis`'() {
assert.strictEqual(globalObj, globalThis);
}
});