Coder Social home page Coder Social logo

can't find variable: document about h3-js HOT 16 CLOSED

uber avatar uber commented on May 22, 2024 5
can't find variable: document

from h3-js.

Comments (16)

benjamintd avatar benjamintd commented on May 22, 2024 4

Thanks @len-art for following up! I ended up going the ugly way and using patch-package, which allows patching your node_modules on build, with the following patch:

Because we use h3-js in a webworker, we need to delete this condition to avoid a crash
due to the emscripten compilation, which bugs when the `window` exists but not `document`.

diff --git a/node_modules/h3-js/dist/browser/h3-js.js b/node_modules/h3-js/dist/browser/h3-js.js
index fa31223..f722cb4 100644
--- a/node_modules/h3-js/dist/browser/h3-js.js
+++ b/node_modules/h3-js/dist/browser/h3-js.js
@@ -24,10 +24,6 @@ var libh3 = function (libh3) {
   var readAsync;

   {
-    if (document.currentScript) {
-      scriptDirectory = document.currentScript.src;
-    }
-
     if (scriptDirectory.indexOf("blob:") !== 0) {
       scriptDirectory = scriptDirectory.substr(0, scriptDirectory.lastIndexOf("/") + 1);
     } else {

I would gladly have sent a PR to h3-js directly, but it seems that it would involve changing the docker image that runs emscripten build. I did not have time to dig further.

from h3-js.

nrabinowitz avatar nrabinowitz commented on May 22, 2024 3

There's a fork here that fixes the distro package for React Native.

I'm closing this for now, as RN support isn't in scope for h3-js at the moment.

from h3-js.

nrabinowitz avatar nrabinowitz commented on May 22, 2024 2

I haven't tried h3-js in React Native, and I'm not very familiar with the environment. The Emscripten-generated code checks for the environment using fairly simple duck-typing, and it assumes that the environment is Node, a browser, or a Web Worker. My guess here is that the React Native environment passes the "browser" test (which I think looks for the existence of window) but doesn't have document or other browser features.

I'm not sure what to recommend here, since this seems likely to be an issue with Emscripten itself. The unsatisfactory answer is that we don't currently support React Native :/.

from h3-js.

Benassou avatar Benassou commented on May 22, 2024

Hey

I have found a workaround. If I comment out the following code in node_modules/h3-js/dist/browser/h3-js.js

if (document.currentScript) {
  scriptDirectory = document.currentScript.src;
}

Module["setWindowTitle"] = function (title) {
  document.title = title;
};

everything works as expected. This does feel quite hacky so I was wondering if there was a better way to remove the document dependency? It doesn't seem to have any real functional use in the code.

from h3-js.

nrabinowitz avatar nrabinowitz commented on May 22, 2024

AFAIK, this is not used - it's part of the Emscripten boilerplate. I can see whether there's a clean way to remove this in the build, but in the meantime, it might be less hacky to polyfill document with {} at the global level (not sure how to do this in the React Native - you'd need to attach it to whatever the equivalent of the global object is in that context). That at least avoids modifying the imported lib, which could make it hard to upgrade, etc.

from h3-js.

Benassou avatar Benassou commented on May 22, 2024

OK, thanks. Right now I'm still trying out this library but I'll be sure to test your solution and report back.

from h3-js.

dakkafex avatar dakkafex commented on May 22, 2024

any update on this?

from h3-js.

Benassou avatar Benassou commented on May 22, 2024

I haven't gone into production yet so I'm still using the hack. The polyfill solution does sound promising though.

from h3-js.

tylergaw avatar tylergaw commented on May 22, 2024

I hit this today too and have a workaround that seems fine. In RN you can use global to set variables that all scripts have access to. You can set a document key on that. Here's what I'm doing in my app:

In App.js

global.document = {};

export default App = () => (...)

then in AnyFile.js import and use h3 as normal

import { h3ToGeo } from "h3-js";
const hexID = geoToH3(longitude, latitude, 7);

from h3-js.

len-art avatar len-art commented on May 22, 2024

I have the same problem on a web app. I'm trying to use h3-js in a web worker.

Uncaught ReferenceError: document is not defined

Should I open a new issue?

from h3-js.

tylergaw avatar tylergaw commented on May 22, 2024

@len-art I don't think web workers have a global document object. You could probably do the same work-around that I posted above where you create that object before you use h3. Something like:

let document = {};

// some code using h3
// the rest of your web worker code

I haven't done that, but that's all h3 needs. It doesn't need any members of document, only expects it to be there.

from h3-js.

len-art avatar len-art commented on May 22, 2024

Workers don't have a global document, yes. I tried doing what you suggested but have some problems because webpack and worker-loader probably work a bit differently than a vanilla inclustion of worker code.
But that's something I have to solve myself, it's not an H3 problem, so thanks for the help.

from h3-js.

benjamintd avatar benjamintd commented on May 22, 2024

@len-art have you figured out how to solve this issue in a webpack-compiled webworker? I am facing the same problem and was not able to solve it easily bu setting document to {}.

from h3-js.

len-art avatar len-art commented on May 22, 2024

@benjamintd sorry for responding so late: No, I haven't. I went another route and implemented something different which, in retrospect, made more sense than using H3. Regardless, I would really love it if the authors would fix this problem. There are a ton of use cases for H3 that benefit from being calculated in a separate thread.

from h3-js.

markusand avatar markusand commented on May 22, 2024

@benjamintd sorry for responding so late: No, I haven't. I went another route and implemented something different which, in retrospect, made more sense than using H3. Regardless, I would really love it if the authors would fix this problem. There are a ton of use cases for H3 that benefit from being calculated in a separate thread.

There's definitely plenty of use cases where using h3-js in web workers makes total sense. Something apparently simple like the following is blocking the execution of main thread, making the map unresponsible during 3/4 seconds (neither being able to show a spinner).

import { cellToBoundary, polygonToCells } from 'h3-js';
import { polygon as toPolygon } from '@turf/turf';
import type { Slope } from './types';

onmessage = (event: MessageEvent<{ slopes: Slope[], resolution: number }>) => {
  const { slopes, resolution } = event.data;

  const cells = slopes.flatMap(slope => {
    const { coordinates, type } = slope.feature.geometry;
    const polygons = type === 'MultiPolygon' ? coordinates : [coordinates];
    const h3Ids = polygons.flatMap(polygon => polygonToCells(polygon, resolution, true));
    return h3Ids.map(id => toPolygon([cellToBoundary(id, true)], { id }));
  });

  postMessage(cells);
};

I've tried to just fake a global document object as suggested, without success. Just for context, I'm using Vite.
Has it been any movement in this after the update to v4?

from h3-js.

nrabinowitz avatar nrabinowitz commented on May 22, 2024

Has it been any movement in this after the update to v4?

Sorry, this is still on my TODO list. It's likely solved by a more recent version of Emscripten, but I believe that's still blocked on performance regressions, making this a potentially difficult update.

from h3-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.