Skip to content

Commit

Permalink
Merge pull request #106 from alias-rahil/fix/issues/8
Browse files Browse the repository at this point in the history
fix: #8 window is undefined error
  • Loading branch information
dibenede committed Jul 27, 2022
2 parents 6ff8856 + dac3720 commit c3db2b5
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions generator/js_generator.cc
Expand Up @@ -3630,15 +3630,22 @@ void Generator::GenerateFile(const GeneratorOptions& options,
// set "this" inside the function to the global object. This does not work
// if we are running in strict mode ("use strict"), so we fallback to the
// following things (in order from first to last):
// - globalThis: cross-platform standard, might not be defined in older
// versions of browsers
// - window: defined in browsers
// - global: defined in most server side environments like NodeJS
// - self: defined inside Web Workers (WorkerGlobalScope)
// - Function('return this')(): this will work on most platforms, but it
// may be blocked by things like CSP.
// Function('') is almost the same as eval('')
printer->Print(
"var global = (function() { return this || window || global || self "
"|| Function('return this')(); }).call(null);\n\n");
"var global =\n"
" (typeof globalThis !== 'undefined' && globalThis) ||\n"
" (typeof window !== 'undefined' && window) ||\n"
" (typeof global !== 'undefined' && global) ||\n"
" (typeof self !== 'undefined' && self) ||\n"
" (function () { return this; }).call(null) ||\n"
" Function('return this')();\n\n");
}

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

0 comments on commit c3db2b5

Please sign in to comment.