diff options
author | Bartek Iwańczuk <biwanczuk@gmail.com> | 2024-01-24 14:20:51 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-24 14:20:51 +0100 |
commit | 930ce2087051b4e45b2026ce7a77c14360a6993f (patch) | |
tree | b1152fcb56e7a498b904717f8a291c8720fd5eb8 /cli | |
parent | b66f5ed00e83927a976ffdbe45c2ace9641de086 (diff) |
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.
Diffstat (limited to 'cli')
-rw-r--r-- | cli/tests/testdata/npm/compare_globals/main.out | 1 | ||||
-rw-r--r-- | cli/tests/testdata/run/webstorage/logger.ts | 2 | ||||
-rw-r--r-- | cli/tests/testdata/run/webstorage/serialization.ts | 6 | ||||
-rw-r--r-- | cli/tests/testdata/run/webstorage/setter.ts | 2 |
4 files changed, 6 insertions, 5 deletions
diff --git a/cli/tests/testdata/npm/compare_globals/main.out b/cli/tests/testdata/npm/compare_globals/main.out index 0e366fae7..c59800eb4 100644 --- a/cli/tests/testdata/npm/compare_globals/main.out +++ b/cli/tests/testdata/npm/compare_globals/main.out @@ -20,6 +20,7 @@ false true true true +[WILDCARD] true false false diff --git a/cli/tests/testdata/run/webstorage/logger.ts b/cli/tests/testdata/run/webstorage/logger.ts index 3898c4445..feadd39eb 100644 --- a/cli/tests/testdata/run/webstorage/logger.ts +++ b/cli/tests/testdata/run/webstorage/logger.ts @@ -1 +1 @@ -console.log(window.localStorage); +console.log(globalThis.localStorage); diff --git a/cli/tests/testdata/run/webstorage/serialization.ts b/cli/tests/testdata/run/webstorage/serialization.ts index f3791d355..f125331bb 100644 --- a/cli/tests/testdata/run/webstorage/serialization.ts +++ b/cli/tests/testdata/run/webstorage/serialization.ts @@ -1,4 +1,4 @@ -window.sessionStorage.setItem("hello", "deno"); +globalThis.sessionStorage.setItem("hello", "deno"); -console.log(window.localStorage); -console.log(window.sessionStorage); +console.log(globalThis.localStorage); +console.log(globalThis.sessionStorage); diff --git a/cli/tests/testdata/run/webstorage/setter.ts b/cli/tests/testdata/run/webstorage/setter.ts index ec6d474f5..cf5a7bfaf 100644 --- a/cli/tests/testdata/run/webstorage/setter.ts +++ b/cli/tests/testdata/run/webstorage/setter.ts @@ -1 +1 @@ -window.localStorage.setItem("hello", "deno"); +globalThis.localStorage.setItem("hello", "deno"); |