Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: #8 window is undefined error #106

Merged
merged 5 commits into from
Jul 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions generator/js_generator.cc
Original file line number Diff line number Diff line change
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