-
-
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
luaconf.js: ReferenceError: process is not defined #129
Comments
So this is a bit tricky, we need to be able to pass FENGARICONF at webpack build time. |
Ah okay, I see. Never mind then. |
Wait no.... I just realised that in webpack we set it to However that's just for fengari-web's use case. For anyone actually using |
I'm pretty new to webpack, so this might be a silly idea, but the webpack config file is just JS code, right? That means we could read the FENGARICONF in the config file and build appropriate definitions for the With these patches to fengari and fengari-web, this works just fine: diff --git a/webpack.config.js b/webpack.config.js
index babb5bc..4d1efd5 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -2,6 +2,20 @@
const webpack = require('webpack');
+function fengariconfDefine() {
+ let define = {
+ "process.env.FENGARICONF": "void 0",
+ "typeof process": JSON.stringify("undefined")
+ };
+
+ if (process.env.FENGARICONF) {
+ let fengariconf = JSON.parse(process.env.FENGARICONF);
+ define["__FENGARICONF__"] = JSON.stringify(fengariconf);
+ }
+
+ return new webpack.DefinePlugin(define);
+}
+
module.exports = [
{
/*
@@ -33,10 +47,7 @@ module.exports = [
]
},
plugins: [
- new webpack.DefinePlugin({
- "process.env.FENGARICONF": "void 0",
- "typeof process": JSON.stringify("undefined")
- })
+ fengariconfDefine()
]
},
{
@@ -54,10 +65,7 @@ module.exports = [
devtool: 'hidden-source-map',
node: false,
plugins: [
- new webpack.DefinePlugin({
- "process.env.FENGARICONF": "void 0",
- "typeof process": JSON.stringify("undefined")
- })
+ fengariconfDefine()
]
}
]; diff --git a/src/luaconf.js b/src/luaconf.js
index 387e813..b1ced5c 100644
--- a/src/luaconf.js
+++ b/src/luaconf.js
@@ -1,6 +1,12 @@
"use strict";
-const conf = (process.env.FENGARICONF ? JSON.parse(process.env.FENGARICONF) : {});
+const conf = (typeof __FENGARICONF__ !== "undefined"
+ ? __FENGARICONF__
+ : (typeof process !== "undefined" && process.env.FENGARICONF
+ ? JSON.parse(process.env.FENGARICONF)
+ : {}
+ )
+);
const {
LUA_VERSION_MAJOR, (Admittedly, it's a bit ugly that this basically defines a new global variable Now you can run |
Doesn't that new global result in failure in strict mode? + warnings from linters? |
Linters, yes. eslint gives me an But since the read access is guarded by |
When webpack does it's thing, I think it only replaces the uses, not the |
When trying to use src/luaconf.js unmodified in a browser environment, you will get an error like "ReferenceError: process is not defined", since the global
process
is not defined in a browser environment.This can easily be fixed by checking
typeof process !== "undefined"
(as it is already done in other places of fengari):The text was updated successfully, but these errors were encountered: