Skip to content

Commit c3db2b5

Browse files
authored
Merge pull request #106 from alias-rahil/fix/issues/8
fix: #8 window is undefined error
2 parents 6ff8856 + dac3720 commit c3db2b5

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

generator/js_generator.cc

+9-2
Original file line numberDiff line numberDiff line change
@@ -3630,15 +3630,22 @@ void Generator::GenerateFile(const GeneratorOptions& options,
36303630
// set "this" inside the function to the global object. This does not work
36313631
// if we are running in strict mode ("use strict"), so we fallback to the
36323632
// following things (in order from first to last):
3633+
// - globalThis: cross-platform standard, might not be defined in older
3634+
// versions of browsers
36333635
// - window: defined in browsers
36343636
// - global: defined in most server side environments like NodeJS
36353637
// - self: defined inside Web Workers (WorkerGlobalScope)
36363638
// - Function('return this')(): this will work on most platforms, but it
36373639
// may be blocked by things like CSP.
36383640
// Function('') is almost the same as eval('')
36393641
printer->Print(
3640-
"var global = (function() { return this || window || global || self "
3641-
"|| Function('return this')(); }).call(null);\n\n");
3642+
"var global =\n"
3643+
" (typeof globalThis !== 'undefined' && globalThis) ||\n"
3644+
" (typeof window !== 'undefined' && window) ||\n"
3645+
" (typeof global !== 'undefined' && global) ||\n"
3646+
" (typeof self !== 'undefined' && self) ||\n"
3647+
" (function () { return this; }).call(null) ||\n"
3648+
" Function('return this')();\n\n");
36423649
}
36433650

36443651
for (int i = 0; i < file->dependency_count(); i++) {

0 commit comments

Comments
 (0)