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

SyntaxError: import not found #70

Open
behunin opened this issue Feb 6, 2021 · 7 comments
Open

SyntaxError: import not found #70

behunin opened this issue Feb 6, 2021 · 7 comments

Comments

@behunin
Copy link

behunin commented Feb 6, 2021

Chrome88: SyntaxError: The requested module '/node_modules/@silvia-odwyer/photon/photon_bg.wasm?import' does not provide an export named 'hsl'

Firefox85: SyntaxError: import not found: monochrome

I was trying the resize function.

@silvia-odwyer
Copy link
Owner

@behunin Hi Levi, could you show the code used and I'll take a look at it! Thanks 😄

@behunin
Copy link
Author

behunin commented Feb 9, 2021

    function resize() {
      
      const ctx = small.value.getContext("2d");
      ctx.drawImage(orig.value, 0, 0);
      
      try {
        import("@silvia-odwyer/photon")
          .then((photon) => {
            let img = photon.open_image(small.value, ctx);

            photon.resize(img, 50, 50, 1);
            photon.putImageData(small.value, ctx, img);
          })
          .catch((e) => {
            console.log(e);
          });
      } catch (e) {
        console.log(e);
      }
    }

This is inside a Vue 3 setup function.
small is a ref to the <canvas>
orig is a ref to the <img />

I get the same error whether I use references or getElementById()

Thank you for your time! 😃

@andilem
Copy link

andilem commented Aug 14, 2021

I am getting the same (or a similar) error when using Vite (https://vitejs.dev) as build tool.

The problem seems to come from the fact that Vite uses the following WASM import mechanism: https://vitejs.dev/guide/features.html#webassembly. But in photon_rs_bg.js, the WASM import is defined as import * as wasm from './photon_rs_bg.wasm';, which doesn't work with Vite WASM import.

I found no easy solution, even copying the whole package in my repo and editing the import only led to a new error:

Uncaught (in promise) TypeError: WebAssembly.instantiate(): Import #0 module="./photon_rs_bg.js" error: module is not an object or function

Inspection of the .wasm showed that there are indeed imports from ./photon_rs_bg.js.

Being no JS/WASM expert, I have no idea how to solve this. Is it required to build the WASM with another type, like web (https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html)?

@silvia-odwyer Could you tell us if you plan to support Vite or if you have any idea how to approach or work around this issue?

@behunin
Copy link
Author

behunin commented Aug 14, 2021

The problem comes from wasm-bindgen, the way it packages wasm causes all sorts of problems. Even when you do target web. I had to rework the whole package in order to get something working. Hopefully I will have some free time this winter to submit some PRs to wasm-bindgen.

@silvia-odwyer
Copy link
Owner

@andilem Thanks for letting me know about this! I'd recommend opening an issue in the wasm-bindgen repository, as the WASM version of photon is built using it, so it's definitely worth letting them know also, and you'll be able to get it working with Vite in the future hopefully then 👍

@nshen
Copy link

nshen commented Oct 27, 2021

HI @andilem @silvia-odwyer I made a vite build example, please check.

Demo:
http://github.nshen.net/photon-vite/

Code:
https://github.com/nshen/photon-vite

I just forked photon and republished a npm package with wasm-pack build --target web

https://www.npmjs.com/package/photon-web

with vite-plugin-wasm-pack , it works.

@silvia-odwyer
Copy link
Owner

@nshen Thanks very much for this, it'll be incredibly useful for those who wish to use Photon with vite 😄 Your help is much appreciated, so thanks again! 🎉

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

No branches or pull requests

4 participants