Skip to content

Commit

Permalink
feat: global polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 11, 2021
1 parent 20c129f commit f57dd75
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/runtime/polyfill/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import _global from './globalThis'

try {
const _defineOpts: PropertyDescriptor = { enumerable: false, value: _global }
Object.defineProperties(_global, {
self: _defineOpts,
window: _defineOpts,
global: _defineOpts,
})
} catch (_err) { }

export default _global as typeof globalThis;
2 changes: 1 addition & 1 deletion src/runtime/polyfill/globalThis.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ref: https://mathiasbynens.be/notes/globalthis
const getGlobal = function () {
function getGlobal() {
if (typeof globalThis !== 'undefined') { return globalThis }
if (typeof self !== 'undefined') { return self }
if (typeof window !== 'undefined') { return window }
Expand Down
3 changes: 3 additions & 0 deletions test/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import '../src/runtime/polyfill/global'

console.log(window)

0 comments on commit f57dd75

Please sign in to comment.