Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove global-this-polyfill, and replace it with a simpler alternative, similer to what we already rely on in lib/promise-polyfill.js. Then, with this available internally, move our es6-map.js back into the regular build (thus effectively reverting 259f9af) where we can access it as a regular exported value for internal use, including the use of globalThis for accessing the native Map when available. This resolves the canundrum we had before, where either `typeof Map` is done within the bundle and it gets renamed by Rollup, or we do it outside the bundle and it always finds undefined due to still being within the outer IIFE (which was at the same time a good thing as we don't want to leak this export to outside the QUnit scope). The fix is obvious in retrospect, which is to access it via globalThis. It seems we can consolidate the export.js handling for Web Workers with the globalThis fallback (for SpiderMonkey), so let's do that.
- Loading branch information
Showing
6 changed files
with
92 additions
and
122 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,98 @@ | ||
import globalThis from "../lib/global-this-polyfill"; | ||
// We don't use global-this-polyfill [1], because it modifies | ||
// the globals scope by default. QUnit must not affect the host context | ||
// as developers may test their project may be such a polyfill, and/or | ||
// they may intentionally test their project with and without certain | ||
// polyfills and we must not affect that. It also uses an obscure | ||
// mechanism that seems to sometimes causes a runtime error in older | ||
// browsers (specifically Safari and IE versions that support | ||
// Object.defineProperty but then report _T_ as undefined). | ||
// [1] https://github.com/ungap/global-this/blob/v0.4.4/esm/index.js | ||
// | ||
// Another way is `Function('return this')()`, but doing so relies | ||
// on eval which will cause a CSP error on some servers. | ||
// | ||
// Instead, simply check the four options that already exist | ||
// in all supported environments. | ||
function getGlobalThis() { | ||
if ( typeof globalThis !== "undefined" ) { | ||
|
||
export const window = globalThis.window; | ||
export const self = globalThis.self; | ||
export const console = globalThis.console; | ||
export const setTimeout = globalThis.setTimeout; | ||
export const clearTimeout = globalThis.clearTimeout; | ||
// For SpiderMonkey, modern browsers, and recent Node.js | ||
// eslint-disable-next-line no-undef | ||
return globalThis; | ||
} | ||
if ( typeof self !== "undefined" ) { | ||
|
||
// For web workers | ||
// eslint-disable-next-line no-undef | ||
return self; | ||
} | ||
if ( typeof window !== "undefined" ) { | ||
|
||
// For document context in browsers | ||
return window; | ||
} | ||
if ( typeof global !== "undefined" ) { | ||
|
||
// For Node.js | ||
// eslint-disable-next-line no-undef | ||
return global; | ||
} | ||
throw new Error( "Unable to locate global object" ); | ||
} | ||
|
||
|
||
// This avoids a simple `export const` assignment as that would lead Rollup | ||
// to change getGlobalThis and use the same (generated) variable name there. | ||
const g = getGlobalThis(); | ||
export { g as globalThis }; | ||
export const window = g.window; | ||
export const console = g.console; | ||
export const setTimeout = g.setTimeout; | ||
export const clearTimeout = g.clearTimeout; | ||
|
||
export const document = window && window.document; | ||
export const navigator = window && window.navigator; | ||
|
||
export const localSessionStorage = ( function() { | ||
const x = "qunit-test-string"; | ||
try { | ||
globalThis.sessionStorage.setItem( x, x ); | ||
globalThis.sessionStorage.removeItem( x ); | ||
return globalThis.sessionStorage; | ||
g.sessionStorage.setItem( x, x ); | ||
g.sessionStorage.removeItem( x ); | ||
return g.sessionStorage; | ||
} catch ( e ) { | ||
return undefined; | ||
} | ||
}() ); | ||
|
||
// Basic fallback for ES6 Map | ||
// Support: IE 9-10, Safari 7, PhantomJS | ||
export const StringMap = typeof g.Map === "function" ? g.Map : function StringMap() { | ||
var store = Object.create( null ); | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
this.get = function( strKey ) { | ||
return store[ strKey ]; | ||
}; | ||
this.set = function( strKey, val ) { | ||
if ( !hasOwn.call( store, strKey ) ) { | ||
this.size++; | ||
} | ||
store[ strKey ] = val; | ||
return this; | ||
}; | ||
this.delete = function( strKey ) { | ||
if ( hasOwn.call( store, strKey ) ) { | ||
delete store[ strKey ]; | ||
this.size--; | ||
} | ||
}; | ||
this.forEach = function( callback ) { | ||
for ( var strKey in store ) { | ||
callback( store[ strKey ], strKey ); | ||
} | ||
}; | ||
this.clear = function() { | ||
store = Object.create( null ); | ||
this.size = 0; | ||
}; | ||
this.size = 0; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters