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

Getting wmlandscape to run not in a browser #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mlakewood
Copy link

I've been working to get the wmlandscape library to work on the
commandline. The window object doesnt exist in node. I've tried to make sure that this doesnt break anything,
and the variable declarations are a bit funky to make sure we get a const for isDarwin. Im a bit of a newcomer to node, so open to any and all feedback.

I've been working to get the wmlandscape library to work on the
commandline. This line was the only issue in that `window` doesnt exist
in node.
const isDarwin = /Mac|iPod|iPhone|iPad/.test(window.navigator.platform);
let onMac = null;

if (typeof window === 'undefined') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in JS undefined is a value and not a string.

image

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I looked into this, and you are correct that undefined is a value not a string... But SURPRISE! typeof returns undefined as a string... which is totally wierd, but here we are.

[] :~/projects/wm-cli mlakewood$ node
Welcome to Node.js v16.4.2.
Type ".help" for more information.
> typeof window
'undefined'
> typeof window === 'undefined' ? 1 : 0
1
> typeof window === 'undefined' ? "its a string" : "its something else"
'its a string'

So I think this code is correct.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, I misread the check. It does do what it says

@ChristianDavis
Copy link
Contributor

You can simplify this check by just replacing "window" with globalThis, which works in both environments. Here's some info: https://javascript.info/global-object

let onMac;
if (globalThis.navigator) {
 	onMac = /Mac|iPod|iPhone|iPad/.test(globalThis.navigator.platform);
} else {
	onMac = process.platform === 'darwin';
}
const isDarwin = onMac;

But your check will also work if you fix the undefined issue.

The browser is also acting as the rendering engine in this case, so if you're planning on rendering images with Node you'll need to experiment with node-canvas or a similar library

@mlakewood
Copy link
Author

With regards to the globalThis object, I attempted to use it, however it looks like there are the following drawbacks

  1. "platform" doesnt exist in this API. which means this code path would need to be changed. And looking through the browser compatibility for https://developer.mozilla.org/en-US/docs/Web/API/NavigatorUAData/platform (which is the recommended replacement) its not supported on FF or IE. Which seems, not great?
  2. Eslint had some issues with using it, as its not declared as a global variable. I was able to work around this however, but marking it as a global.

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.

2 participants