-
-
Notifications
You must be signed in to change notification settings - Fork 65
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
Don't load Node-specific code when bundled via Webpack #166
Conversation
@@ -0,0 +1,7 @@ | |||
"use strict"; | |||
|
|||
const isBrowser = ((typeof process === "undefined") || process.browser); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't love a new source file for this. I think put it into defs.js?
|
||
let lua_writestring; | ||
let lua_writeline; | ||
if (typeof process === "undefined") { | ||
if (isBrowser) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is actually checking for the process
module (it uses process.stdout
if available in the other branch)
@@ -270,7 +274,7 @@ const ll_loadlib = function(L) { | |||
}; | |||
|
|||
const env = (function() { | |||
if (typeof process !== "undefined") { | |||
if (isNode) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to leave this one as actually checking process
?
const isBrowser = ((typeof process === "undefined") || process.browser); | ||
const isNode = !isBrowser; | ||
|
||
module.exports.isBrowser = isBrowser; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC don't use camelCase anywhere in fengari; please use snake_case for consistency.
After discussing this further on IRC, I'm closing this in favor of patching |
Reviewer Note:
I'm mostly just starting out on my Node/Webpack/JS/* journey. If there is a blatant misunderstanding of the inner workings of Node, Webpack, or anything related to them, please be gentle with me.
What is being fixed?
The issue is described nicely in #156. Basically, when being bundled through Webpack, Fengari would think that it's running in a Node context (because Webpack sets a global
process
variable, which Fengari checks for in a bunch of places.) This would result in Fengari trying to load Node-specific code, which, of course, fails in a Webpack bundle intended for the web.How is it fixed?
I've extracted the expression that determines whether Fengari is running in a node environment into a separate module and changed the various instances where Fengari checked for Node (or non-Node) environments to make use of this module. I've also smartened up the expression slightly.
At its core, the change is the following:
How is it tested?
I didn't find any tests for the existing behavior and didn't feel like adding a test for this change would provide much value, but I did confirm that this works in my own project.