Skip to content

Commit eeaa482

Browse files
authored
Build: Restore strict mode compatibility
When bundling qunit as a library in another build, the use of `this` would cause an error strict mode due to being undefined there. Adopt [ES2020 globalThis](https://github.com/tc39/proposal-global), and polyfill it for environments that don't implement it yet. Fixes #1557. Closes #1558.
1 parent d80a986 commit eeaa482

File tree

5 files changed

+63
-22
lines changed

5 files changed

+63
-22
lines changed

lib/global-this-polyfill.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
https://github.com/ungap/global-this/blob/v0.4.4/esm/index.js
3+
4+
Copyright (c) 2020, Andrea Giammarchi, @WebReflection
5+
6+
Permission to use, copy, modify, and/or distribute this software for any
7+
purpose with or without fee is hereby granted, provided that the above
8+
copyright notice and this permission notice appear in all copies.
9+
10+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15+
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16+
PERFORMANCE OF THIS SOFTWARE.
17+
18+
-------
19+
20+
Patches for use in QUnit:
21+
22+
- 2021-02-25: Export as module only, don't change global scope as QUnit must not
23+
affect the host context (e.g. people may test their application intentionally
24+
with different or no polyfills and we must not affect that).
25+
26+
*/
27+
28+
let foundGlobalThis;
29+
30+
(function (Object) {
31+
if (typeof globalThis === "object") {
32+
foundGlobalThis = globalThis;
33+
} else {
34+
this
35+
? get()
36+
: (Object.defineProperty(Object.prototype, "_T_", {
37+
configurable: true,
38+
get: get,
39+
}),
40+
_T_);
41+
42+
function get() {
43+
foundGlobalThis = this || self;
44+
delete Object.prototype._T_;
45+
}
46+
}
47+
})(Object);
48+
49+
export default foundGlobalThis;

rollup.config.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ module.exports = {
4040
__dirname + "/src/html-reporter/es6-map.js",
4141
"utf8"
4242
).toString().trim();
43-
},
44-
45-
globals: {
46-
global: "(function() { return this; }())"
4743
}
4844
},
4945
plugins: [
@@ -57,8 +53,5 @@ module.exports = {
5753
babelHelpers: "bundled",
5854
babelrc: true
5955
} )
60-
],
61-
external: [
62-
"global"
6356
]
6457
};

src/export.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* global module, exports, define */
22
import { window, document, self } from "./globals";
3-
import global from "global";
3+
import globalThis from "../lib/global-this-polyfill";
44

55
export default function exportQUnit( QUnit ) {
66
let exportedModule = false;
@@ -54,6 +54,6 @@ export default function exportQUnit( QUnit ) {
5454
// For other environments, such as SpiderMonkey (mozjs) and other
5555
// embedded JavaScript engines
5656
if ( !exportedModule ) {
57-
global.QUnit = QUnit;
57+
globalThis.QUnit = QUnit;
5858
}
5959
}

src/globals.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import global from "global";
1+
import globalThis from "../lib/global-this-polyfill";
22

3-
export const window = global.window;
4-
export const self = global.self;
5-
export const console = global.console;
6-
export const setTimeout = global.setTimeout;
7-
export const clearTimeout = global.clearTimeout;
3+
export const window = globalThis.window;
4+
export const self = globalThis.self;
5+
export const console = globalThis.console;
6+
export const setTimeout = globalThis.setTimeout;
7+
export const clearTimeout = globalThis.clearTimeout;
88

99
export const document = window && window.document;
1010
export const navigator = window && window.navigator;
1111

1212
export const localSessionStorage = ( function() {
1313
const x = "qunit-test-string";
1414
try {
15-
global.sessionStorage.setItem( x, x );
16-
global.sessionStorage.removeItem( x );
17-
return global.sessionStorage;
15+
globalThis.sessionStorage.setItem( x, x );
16+
globalThis.sessionStorage.removeItem( x );
17+
return globalThis.sessionStorage;
1818
} catch ( e ) {
1919
return undefined;
2020
}

src/test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import global from "global";
2-
1+
import globalThis from "../lib/global-this-polyfill";
32
import { begin } from "./core";
43
import { setTimeout, clearTimeout } from "./globals";
54
import { emit } from "./events";
@@ -648,8 +647,8 @@ function saveGlobal() {
648647
config.pollution = [];
649648

650649
if ( config.noglobals ) {
651-
for ( const key in global ) {
652-
if ( hasOwn.call( global, key ) ) {
650+
for ( const key in globalThis ) {
651+
if ( hasOwn.call( globalThis, key ) ) {
653652

654653
// In Opera sometimes DOM element ids show up here, ignore them
655654
if ( /^qunit-test-output/.test( key ) ) {

0 commit comments

Comments
 (0)