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

Don't load Node-specific code when bundled via Webpack #166

Closed
wants to merge 1 commit into from

Conversation

hmans
Copy link

@hmans hmans commented Aug 28, 2019

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:

/* NEW */
((typeof process === "undefined") || process.browser)

/* OLD */
(typeof process === "undefined")

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.

@@ -0,0 +1,7 @@
"use strict";

const isBrowser = ((typeof process === "undefined") || process.browser);
Copy link
Member

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) {
Copy link
Member

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) {
Copy link
Member

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;
Copy link
Member

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.

@hmans
Copy link
Author

hmans commented Aug 29, 2019

After discussing this further on IRC, I'm closing this in favor of patching typeof process directly through webpack.config, as only this way will allow Webpack to properly exclude the non-needed code paths when bundling.

@hmans hmans closed this Aug 29, 2019
@hmans hmans deleted the make-fengari-work-in-webpack branch August 29, 2019 04:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants