Open kavidey opened 2 years ago
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.
Just replace import 'roslib/build/roslib' with import 'roslib/build/roslib'
@desstiony you suggest to replace it with the same....
Sorry, I made a typo. you should replace "import * as ROSLIB from 'roslib'" with 'roslib/build/roslib' in main.js file.
So you mean replace import * as ROSLIB from 'roslib'
with import * as ROSLIB from 'roslib/builld/roslib'
?
Just import 'roslib/build/roslib'
, you can try it.
I am not using Vite. It wasn't clear to me. So I thought it might not be clear to other people.
I have tried it in the codespace of this repository hope it can help you. @kavidey
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!
The issue with the import shim failing is likely due to https://github.com/vitejs/vite/issues/7576, which is waiting on PR https://github.com/vitejs/vite/pull/8709.
A workaround is possible by using Vite's resolve.alias
to mimick the shims below:
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?
Just replace import 'roslib/build/roslib' with import 'roslib/build/roslib'只需将 import 'roslib/build/roslib' 替换为 import 'roslib/build/roslib'
Is there any difference?
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.
Steps To Reproduce Minimum reproducible project here: https://github.com/kavidey/vite-sample-roslib Run
npm run dev
to get the error, ornpm run build
thennpm 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
requiringws
. I think that thews
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 requirews
, though I'm not sure.