Skip to content

Commit 66e32a7

Browse files
committed
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 ae675c6 commit 66e32a7

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
@@ -139,7 +139,7 @@ var _scriptDir = import.meta.url;
139139
var _scriptDir = (typeof document != 'undefined' && document.currentScript) ? document.currentScript.src : undefined;
140140

141141
if (ENVIRONMENT_IS_WORKER) {
142-
_scriptDir = self.location.href;
142+
_scriptDir = self.location == null ? '' : self.location.href;
143143
}
144144
#if ENVIRONMENT_MAY_BE_NODE
145145
else if (ENVIRONMENT_IS_NODE) {
@@ -362,7 +362,7 @@ if (ENVIRONMENT_IS_SHELL) {
362362
#if ENVIRONMENT_MAY_BE_WEB || ENVIRONMENT_MAY_BE_WORKER
363363
if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
364364
if (ENVIRONMENT_IS_WORKER) { // Check worker, not web, since window could be polyfilled
365-
scriptDirectory = self.location.href;
365+
scriptDirectory = self.location == null ? '' : self.location.href;
366366
} else if (typeof document != 'undefined' && document.currentScript) { // web
367367
scriptDirectory = document.currentScript.src;
368368
}

0 commit comments

Comments
 (0)