mirror of
https://github.com/denoland/deno.git
synced 2025-01-11 08:33:43 -05:00
feat: deprecate window
global (#22057)
This commit deprecates `window` global and adds deprecation notice on each use of `window`. We decided to proceed with removal of `window` global variable in Deno 2.0. There's a lot of code in the wild that uses pattern like this: ``` if (typeof window !== "undefined) { ... } ``` to check if the code is being run in browser. However, this check passes fine in Deno and most often libraries that do this check try to access some browser API that is not available in Deno, or use DOM APIs (which are also not available in Deno). This situation has occurred multiple times already and it's unfeasible to expect the whole ecosystem to migrate to new check (and even if that happened there's a ton of code that's already shipped and won't change). The migration is straightfoward - replace all usages of `window` with `globalThis` or `self`. When Deno encounters use of `window` global it will now issue a warning, steering users towards required changes: ``` Warning ├ Use of deprecated "window" API. │ ├ This API will be removed in Deno 2.0. Make sure to upgrade to a stable API before then. │ ├ Suggestion: Use `globalThis` or `self` instead. │ ├ Suggestion: You can provide `window` in the current scope with: `const window = globalThis`. │ └ Stack trace: └─ at file:///Users/ib/dev/deno/foo.js:7:1 ``` Ref https://github.com/denoland/deno/issues/13367.
This commit is contained in:
parent
b66f5ed00e
commit
930ce20870
6 changed files with 25 additions and 13 deletions
|
@ -20,6 +20,7 @@ false
|
||||||
true
|
true
|
||||||
true
|
true
|
||||||
true
|
true
|
||||||
|
[WILDCARD]
|
||||||
true
|
true
|
||||||
false
|
false
|
||||||
false
|
false
|
||||||
|
|
2
cli/tests/testdata/run/webstorage/logger.ts
vendored
2
cli/tests/testdata/run/webstorage/logger.ts
vendored
|
@ -1 +1 @@
|
||||||
console.log(window.localStorage);
|
console.log(globalThis.localStorage);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
window.sessionStorage.setItem("hello", "deno");
|
globalThis.sessionStorage.setItem("hello", "deno");
|
||||||
|
|
||||||
console.log(window.localStorage);
|
console.log(globalThis.localStorage);
|
||||||
console.log(window.sessionStorage);
|
console.log(globalThis.sessionStorage);
|
||||||
|
|
2
cli/tests/testdata/run/webstorage/setter.ts
vendored
2
cli/tests/testdata/run/webstorage/setter.ts
vendored
|
@ -1 +1 @@
|
||||||
window.localStorage.setItem("hello", "deno");
|
globalThis.localStorage.setItem("hello", "deno");
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
import { core, primordials } from "ext:core/mod.js";
|
import { core, internals, primordials } from "ext:core/mod.js";
|
||||||
const {
|
const {
|
||||||
op_bootstrap_language,
|
op_bootstrap_language,
|
||||||
op_bootstrap_numcpus,
|
op_bootstrap_numcpus,
|
||||||
|
@ -108,7 +108,15 @@ const mainRuntimeGlobalProperties = {
|
||||||
Location: location.locationConstructorDescriptor,
|
Location: location.locationConstructorDescriptor,
|
||||||
location: location.locationDescriptor,
|
location: location.locationDescriptor,
|
||||||
Window: globalInterfaces.windowConstructorDescriptor,
|
Window: globalInterfaces.windowConstructorDescriptor,
|
||||||
window: util.getterOnly(() => globalThis),
|
window: util.getterOnly(() => {
|
||||||
|
internals.warnOnDeprecatedApi(
|
||||||
|
"window",
|
||||||
|
new Error().stack,
|
||||||
|
"Use `globalThis` or `self` instead.",
|
||||||
|
"You can provide `window` in the current scope with: `const window = globalThis`.",
|
||||||
|
);
|
||||||
|
return globalThis;
|
||||||
|
}),
|
||||||
self: util.getterOnly(() => globalThis),
|
self: util.getterOnly(() => globalThis),
|
||||||
Navigator: util.nonEnumerable(Navigator),
|
Navigator: util.nonEnumerable(Navigator),
|
||||||
navigator: util.getterOnly(() => navigator),
|
navigator: util.getterOnly(() => navigator),
|
||||||
|
|
|
@ -99,7 +99,7 @@ let globalThis_;
|
||||||
let deprecatedApiWarningDisabled = false;
|
let deprecatedApiWarningDisabled = false;
|
||||||
const ALREADY_WARNED_DEPRECATED = new SafeSet();
|
const ALREADY_WARNED_DEPRECATED = new SafeSet();
|
||||||
|
|
||||||
function warnOnDeprecatedApi(apiName, stack, suggestion) {
|
function warnOnDeprecatedApi(apiName, stack, ...suggestions) {
|
||||||
if (deprecatedApiWarningDisabled) {
|
if (deprecatedApiWarningDisabled) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -155,11 +155,14 @@ function warnOnDeprecatedApi(apiName, stack, suggestion) {
|
||||||
"%c\u251c This API will be removed in Deno 2.0. Make sure to upgrade to a stable API before then.",
|
"%c\u251c This API will be removed in Deno 2.0. Make sure to upgrade to a stable API before then.",
|
||||||
"color: yellow;",
|
"color: yellow;",
|
||||||
);
|
);
|
||||||
|
for (let i = 0; i < suggestions.length; i++) {
|
||||||
|
const suggestion = suggestions[i];
|
||||||
console.error("%c\u2502", "color: yellow;");
|
console.error("%c\u2502", "color: yellow;");
|
||||||
console.error(
|
console.error(
|
||||||
`%c\u251c Suggestion: ${suggestion}`,
|
`%c\u251c Suggestion: ${suggestion}`,
|
||||||
"color: yellow;",
|
"color: yellow;",
|
||||||
);
|
);
|
||||||
|
}
|
||||||
if (isFromRemoteDependency) {
|
if (isFromRemoteDependency) {
|
||||||
console.error("%c\u2502", "color: yellow;");
|
console.error("%c\u2502", "color: yellow;");
|
||||||
console.error(
|
console.error(
|
||||||
|
|
Loading…
Reference in a new issue