Skip to content

Commit d3a054b

Browse files
authored
shell.js: Handle if location object does not exist
When compiling with the options introduced in [this comment](#13190 (comment)), the `ENVIRONMENT_IS_WORKER` value is set to `true`. In Deno, the value of `self.location` is `undefined` when the `deno run` command is executed without the `--location` CLI argument. In the scenario of creating the Deno library, it is difficult to tell the library user to input the `--location` CLI argument, so I modified it to handle fallback at this level.
1 parent 4b5c4f0 commit d3a054b

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/shell.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ var _scriptDir = import.meta.url;
134134
var _scriptDir = (typeof document != 'undefined' && document.currentScript) ? document.currentScript.src : undefined;
135135

136136
if (ENVIRONMENT_IS_WORKER) {
137-
_scriptDir = self.location.href;
137+
_scriptDir = self.location?.href || '';
138138
}
139139
#if ENVIRONMENT_MAY_BE_NODE
140140
else if (ENVIRONMENT_IS_NODE) {
@@ -357,7 +357,7 @@ if (ENVIRONMENT_IS_SHELL) {
357357
#if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER
358358
if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
359359
if (ENVIRONMENT_IS_WORKER) { // Check worker, not web, since window could be polyfilled
360-
scriptDirectory = self.location.href;
360+
scriptDirectory = self.location?.href || '';
361361
} else if (typeof document != 'undefined' && document.currentScript) { // web
362362
scriptDirectory = document.currentScript.src;
363363
}

0 commit comments

Comments
 (0)