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 with roslibjs and Vite #548

Open
kavidey opened this issue May 19, 2022 · 11 comments
Open

Issue with roslibjs and Vite #548

kavidey opened this issue May 19, 2022 · 11 comments
Labels

Comments

@kavidey
Copy link

kavidey commented May 19, 2022

I am using roslibjs with a project that uses Vite as the build system and am running into an issue when running the project in development.

  • Library Version: 1.3.0
  • ROS Version: Galactic
  • Platform / OS: Ubuntu 20.04
  • Vite Version: 2.9.9

Steps To Reproduce
Minimum reproducible project here: https://github.com/kavidey/vite-sample-roslib
Run npm run dev to get the error, or npm run build then npm run preview to see the project working properly.

Expected Behavior
In both dev and preview mode, importing roslibjs should work without any errors.

Actual Behavior
When running the code in dev mode, I get the error: ReferenceError: Can't find variable: Buffer.

That error traces back to core/Ros.js requiring ws. I think that the ws shim may not be working correctly when vite is running in dev mode and roslibjs thinks that its running in node so it tries to require ws, though I'm not sure.

@kavidey kavidey added the bug label May 19, 2022
kavidey added a commit to redshiftrobotics/blueshift_ros that referenced this issue May 19, 2022
@MatthijsBurgh
Copy link
Contributor

I have really no clue. I also have to say this is a bit out of the support scope. I have to say I have never experienced any issue with webpack-dev-server.

When you are able to provide a solution that requires to be implemented in the library, I am open to that. But I am not able to help you finding that solution.

@desstiony
Copy link

desstiony commented Jun 10, 2022

Just replace import 'roslib/build/roslib' with import 'roslib/build/roslib'

@MatthijsBurgh
Copy link
Contributor

@desstiony you suggest to replace it with the same....

@desstiony
Copy link

Sorry, I made a typo. you should replace "import * as ROSLIB from 'roslib'" with 'roslib/build/roslib' in main.js file.

@MatthijsBurgh
Copy link
Contributor

So you mean replace import * as ROSLIB from 'roslib' with import * as ROSLIB from 'roslib/builld/roslib'?

@desstiony
Copy link

Just import 'roslib/build/roslib', you can try it.

@MatthijsBurgh
Copy link
Contributor

I am not using Vite. It wasn't clear to me. So I thought it might not be clear to other people.

@desstiony
Copy link

I have tried it in the codespace of this repository hope it can help you. @kavidey

@kavidey
Copy link
Author

kavidey commented Jun 10, 2022

Thank you @desstiony. This is a great fix for now!

Because we're importing the fully compiled browser version, it creates a global window.ROSLIB object instead of putting it into a local variable. This means we don't get tree shaking or quite as clean syntax (SSR also for those code segments also has to be disabled because that build of ROSLIB cannot run on the server)

I added a shim file to simplify the import syntax, so now it looks like import ROSLIB from './roslib.shim'; and we don't need to look at the global variable. Changing the file extension of the shim to .ts also gives typescript support.

I think if possible it would be great to try and find a solution that doesn't require using the precompiled version of roslib, but this works great for now!

@Interpause
Copy link

Interpause commented Jul 23, 2022

The issue with the import shim failing is likely due to vitejs/vite#7576, which is waiting on PR vitejs/vite#8709.

A workaround is possible by using Vite's resolve.alias to mimick the shims below:

roslibjs/package.json

Lines 8 to 14 in 7aac4c5

"browser": {
"./src/RosLibNode.js": "./src/RosLib.js",
"canvas": "./src/util/shim/canvas.js",
"ws": "./src/util/shim/WebSocket.js",
"@xmldom/xmldom": "./src/util/shim/@xmldom/xmldom.js",
"./src/util/decompressPng.js": "./src/util/shim/decompressPng.js"
},

In vite.config.js:

{
  // ...
  resolve: {
    alias: {
      './src/RosLibNode.js': './src/RosLib.js',
      canvas: './canvas',
      ws: '../util/shim/WebSocket',
      '@xmldom/xmldom': '../util/shim/@xmldom/xmldom',
      '../util/decompressPng': '../util/shim/decompressPng',
    }
  }
}

Note the above could have undesireable side effects if you so happen to also import those paths.

Alternatively, why not import the shims directly then use the below trick to either import the NodeJS dependencies or browser dependencies?

module.exports = typeof window !== 'undefined' ? window.WebSocket : WebSocket;

k-aguete pushed a commit to k-aguete/roslibjs that referenced this issue Oct 21, 2022
Bumps [karma](https://github.com/karma-runner/karma) from 6.3.17 to 6.4.0.
- [Release notes](https://github.com/karma-runner/karma/releases)
- [Changelog](https://github.com/karma-runner/karma/blob/master/CHANGELOG.md)
- [Commits](karma-runner/karma@v6.3.17...v6.4.0)

---
updated-dependencies:
- dependency-name: karma
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
@loran-luo
Copy link

Just replace import 'roslib/build/roslib' with import 'roslib/build/roslib'只需将 import 'roslib/build/roslib' 替换为 import 'roslib/build/roslib'

Is there any difference?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants