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

Issue loading the .riv file when configured using webpack. #298

Open
vigsub opened this issue Mar 17, 2023 · 6 comments
Open

Issue loading the .riv file when configured using webpack. #298

vigsub opened this issue Mar 17, 2023 · 6 comments

Comments

@vigsub
Copy link

vigsub commented Mar 17, 2023

I'm using webpack 5 for bundling and trying to load a rive file into my React app. I'm getting the following error.

Screenshot 2023-03-18 at 12 03 46 AM

These are the rules in the webpack config file.

module.exports = {
  experiments: {
    asyncWebAssembly: true
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.scss$/,
        use: ['style-loader', 'css-loader', 'sass-loader']
      },
      {
        test: /\.(js|jsx)$/,
        use: ['babel-loader']
      },
      {
        test: /\.(ts|tsx)$/,
        exclude: /node_modules/,
        use: ['babel-loader']
      },
      {
        test: /\.(gif|jpg|png|woff|ttf|eot)$/,
        loader: 'file-loader'
      },
      {
        test: /\.svg$/,
        loader: 'svg-react-loader'
      },
      {
        test: /\.wasm$/,
        type: 'javascript/auto',
        use: ['url-loader']
      }
    ]
  }
};

As suggested in one of the resolved issues in this repo, I'm using the following approach.

function TestRive({ asset, className, animation, fit, alignment }) {
  const canvas = useRef(null);
  const animationContainer = useRef(null);
  const rive = useRef(null);

  // Start the animation
  useEffect(() => {
    rive.current = new Rive({
      src:'https://cdn.rive.app/animations/vehicles.riv',
      canvas: canvas.current,
      layout: new Layout({ fit: 'cover', alignment: 'center' }),
      autoplay: true
    });
    return () => rive.current.stop();
  }, [asset, fit, alignment]);

  return (
    <div ref={animationContainer} className={className}>
      <canvas ref={canvas} width="500" height="500" />
    </div>
  );
}

Also tried multiple ways of loading the rive component.

function TestRive() {
  const { rive, RiveComponent } = useRive({
    src: 'https://cdn.rive.app/animations/vehicles.riv',
    autoplay: true
  });
  return (
    <div>
      <RiveComponent />
    </div>
  );
}

P.S: It is working fine in any sample app created with create-react-app. But in the app where I use webpack to manually configure, it's not working as expected and throws the binding error message. What are the loaders I'm missing? Or is it something else that I'm missing?

@vigsub
Copy link
Author

vigsub commented Mar 24, 2023

@zplata Can you help with this? I'm kinda stuck here..

@zplata
Copy link
Contributor

zplata commented Mar 31, 2023

Hi @vigsub - The error comes from Emscripten which does the binding of underlying C++ code to JS. In this case, there's an underlying function that thinks it's taking in an enum and you're passing string values in your Layout options. You'll want to import Fit and Alignment as named imports and use Fit.Cover and Alignment.Center.

import {Fit, Alignment} from '@rive-app/canvas';

// or

import {Fit, Alignment} from '@rive-app/react-canvas'

@zplata
Copy link
Contributor

zplata commented Apr 17, 2023

@vigsub wonder if this helped solve your issue?

@vigsub
Copy link
Author

vigsub commented Apr 18, 2023

Oops. Sorry.. Missed this thread. Will check this and respond. Thanks :)

@vigsub
Copy link
Author

vigsub commented Apr 18, 2023

Nope.. Not working :( .. If providing Fit.Cover and Alignment is a problem, in the second example mentioned, I don't use them. Still it's not rendering. @zplata I suppose this has something to do with the loaders in webpack?? Please assist..

@zplata
Copy link
Contributor

zplata commented Apr 18, 2023

@vigsub sorry to hear - I don't imagine it should be an issue with Webpack as you're not trying to import WASM or Rive files in your JS/TS files, but not to say that it couldn't be the issue! In case I'm missing context here, do you have a repro project that you can post here so that we can debug further and run in isolation?

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

2 participants