diff --git a/templates/html5/output.js b/templates/html5/output.js
index ad3aac5d4..7ab97c6da 100644
--- a/templates/html5/output.js
+++ b/templates/html5/output.js
@@ -1,10 +1,7 @@
(function ($hx_exports, $global) { "use strict"; var $hx_script = (function (exports, global) { ::SOURCE_FILE::
});
-::if false::
- If `window` is undefined, it means this script is running as a web worker.
- In that case, there's no need for exports, and all we need to do is run the
- static initializers.
-::end::if(typeof window == "undefined") {
+if (typeof self !== "undefined" && self.constructor.name.includes("Worker")) {
+ // No need for exports in a worker context, just initialize statics.
$hx_script({}, $global);
} else {
$hx_exports.lime = $hx_exports.lime || {};
diff --git a/tools/platforms/HTML5Platform.hx b/tools/platforms/HTML5Platform.hx
index 644eac2ad..0f8ba61d2 100644
--- a/tools/platforms/HTML5Platform.hx
+++ b/tools/platforms/HTML5Platform.hx
@@ -161,7 +161,7 @@ class HTML5Platform extends PlatformTarget
var script = File.getContent(dependency.path);
if (!dependency.webWorker)
{
- script = 'if(typeof window != "undefined") {\n' + script + "\n}";
+ script = 'if(typeof self === "undefined" || !self.constructor.name.includes("Worker")) { $script }';
}
context.embeddedLibraries.push(script);
}