Skip to content

Commit f9e6cfe

Browse files
VirtualTimkripken
authored andcommitted
[ES6] Only use import.meta when strictly necessary (#8940)
A webworker ES6 module doesn't have access to 'document.currentScript'. 'import.meta' was the only way I could see to achieve that functionality. Fixes #8729. Since this appears to fix the issue for everyone who commented on #8729, and no one proposed any better ideas, this is my suggestion. It would be nice to use import.meta everywhere, and browsers seem to have good support, however the tooling appears to be lagging a bit behind.
1 parent f6ff68d commit f9e6cfe

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

emcc.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2991,7 +2991,9 @@ def modularize():
29912991
# after document.currentScript is gone, so we save it.
29922992
# (when MODULARIZE_INSTANCE, an instance is created
29932993
# immediately anyhow, like in non-modularize mode)
2994-
if shared.Settings.EXPORT_ES6:
2994+
# In EXPORT_ES6 + USE_PTHREADS the 'thread' is actually an ES6 module webworker running in strict mode,
2995+
# so doesn't have access to 'document'. In this case use 'import.meta' instead.
2996+
if shared.Settings.EXPORT_ES6 and shared.Settings.USE_PTHREADS:
29952997
script_url = "import.meta.url"
29962998
else:
29972999
script_url = "typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined"

src/shell.js

+4
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,12 @@ if (Module['ENVIRONMENT']) {
8989
#if USE_PTHREADS && (!MODULARIZE || MODULARIZE_INSTANCE)
9090
// In MODULARIZE mode _scriptDir needs to be captured already at the very top of the page immediately when the page is parsed, so it is generated there
9191
// before the page load. In non-MODULARIZE modes generate it here.
92+
#if EXPORT_ES6
93+
var _scriptDir = import.meta.url;
94+
#else
9295
var _scriptDir = (typeof document !== 'undefined' && document.currentScript) ? document.currentScript.src : undefined;
9396
#endif
97+
#endif
9498

9599
// `/` should be present at the end if `scriptDirectory` is not empty
96100
var scriptDirectory = '';

0 commit comments

Comments
 (0)