geotiffjs / geotiff.js

geotiff.js is a small library to parse TIFF files for visualization or analysis. It is written in pure JavaScript, and is usable in both the browser and node.js applications.
https://geotiffjs.github.io/
MIT License
872 stars 183 forks source link

parcelRequire is not defined when including in a Svelte Rollup application #216

Open dhatcher opened 3 years ago

dhatcher commented 3 years ago

I'm attempting to use the library in a svelte application using rollup and getting a parcelRequire error.

Here is my import statement: import * as GeoTIFF from "geotiff/dist-browser/geotiff";

Error message:

Uncaught ReferenceError: parcelRequire is not defined
parcelRequire.QVnC      geotiff.js:1
(anonymous function)    geotiff.js:1
createCommonjsModule    bundle.js:397
(anonymous function)    bundle.js:8388
(anonymous function)    bundle.js:10333

Any ideas or suggestions would be appreciated.

constantinius commented 3 years ago

Hi @dhatcher

Thanks for reporting this issue.

I think in your case, you should not directly reference the bundled library, but reference the source instead. So like that:

import * as GeoTIFF from "geotiff";

In this way rollup can do the bundling instead. Please try that and tell us if that worked.

dhatcher commented 3 years ago

I had started with that and ran into this issue in that case:

(!) Missing global variable names
Use output.globals to specify browser global variable names corresponding to external modules
events (guessing 'require$$0$1')
buffer (guessing 'buffer$1')
http (guessing 'http')
https (guessing 'https')
url (guessing 'urlMod')
fs (guessing 'fs')

Those are only warnings but attempting to launch the web site then causes a related failure/error

Uncaught ReferenceError: require$$0$1 is not defined
(anonymous function)    bundle.js:25445

Looking through the other issues led me to try the above.

dhatcher commented 3 years ago

I'm guessing that the parcelRequire issue is an artifact of using parcel-bundler Perhaps if the migration to the new/supported parcel were done it would be better.

constantinius commented 3 years ago

Regarding this:

(!) Missing global variable names
Use output.globals to specify browser global variable names corresponding to external modules
events (guessing 'require$$0$1')
buffer (guessing 'buffer$1')
http (guessing 'http')
https (guessing 'https')
url (guessing 'urlMod')
fs (guessing 'fs')

Those are all node packages, that you'd need to tell your bundler are not available and should be set to null. This must be done for every bundler unfortunately.

I don't quite know how rollup works, especially with es-6, maybe you need something like a babel integration to translate?

dhatcher commented 3 years ago

I got this to work with the dist-browser version by making a few edits to it. I mostly had to update the results of parcel-bundler to be more friendly with newer versions of JavaScript.

constantinius commented 3 years ago

You mean changes to the dist-browser? This file is generated, so changes made there will not really persist.

dhatcher commented 3 years ago

That's correct I made them to the generated file. Like I mentioned earlier I think these issues are something to do with the way parcel-bundler packages the code. Or at least I was able to use the distro once I updated those things. I started to work on switching it to use the newer parcel for packaging but didn't have time and this is at least working for my purposes for now.

If there is a better solution for rollup I'd be happy to use it. I just don't have the time/expertise to look into it further.

constantinius commented 3 years ago

I understand. I'm also not very familiar with bundlers. Maybe @PacoDu can help us out here? I think he wrote the switch to parcel.

PacoDu commented 3 years ago

Hi, like dhatcher stated, it seems a good idea to upgrade to parcel v2 which should be more suited for library bundling than parcel v1 which wasn't really the "right choice". At the time I did the switch it solved the issues I had using geotiffjs with nodejs, but it seems that it has introduced multiple issues with browser builds.

Maybe bundlers should be re-evaluated before upgrading, parcel v2 claims to be more suited for library building but the documentation is still missing, I've recently heard about microbundler (which is a wrapper around rollup) but I've never used it. Unfortunately I don't have the time right now to investigate the issue. I'll try to take a look later if it's not resolved.

PacoDu commented 3 years ago

If I recall correctly there is also a worker bundling issue that could be addressed at the same time with the bundler upgrade.

PacoDu commented 3 years ago

Regarding this specific issue, I would import Geotiff.js from source as @constantinius suggested, this way your rollup bundler should be able to bundle the library from sources and do as it pleased (but you have to deal with nodejs specific modules). The dist-browser is built for direct import in an html file like CDN imports.

Sorry for the sparse responses.

constantinius commented 3 years ago

Thanks @PacoDu, your insight is much appreciated! For the next bigger version I'd like to restructure a bit and choosing the right bundler may be on the table again. Okay, will look into parcel v2.

vrtantti commented 3 years ago

This same issue seems to replicate when used with nmp@7 workspaces. For some reason when you install geotiff in workspace root node_modules folder it gives error about not finding parcelRequire but when installing normally in application folder it works.

/repo
  package.json  # --> workspace definition
  node_modules/  # --> geotiff breaks here
  package-a/
    package.json  # geotiff required by this package
    node_modules/  # --> geotiff works if it would be installed here
  package-b/