jakubfiala / atrament

A small JS library for beautiful drawing and handwriting on the HTML Canvas.
http://fiala.space/atrament/demo
MIT License
1.56k stars 115 forks source link

Bundling Atrament with vite shows warning: #103

Closed ak37165 closed 4 months ago

ak37165 commented 4 months ago

I am using atrament in a client side project bundled using vite. It shows warning Module "worker_threads" has been externalized for browser compatibility. Cannot access "worker_threads.Worker" in client code

VITE documentation for the warning is here: https://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility

I am not sure I understand fully but looks like some Node.js module expected to be used only on the server side is being used?

jakubfiala commented 4 months ago

@ak37165 thanks for the report! I suspect it's to do with the Rollup plugin for Web Workers: https://github.com/darionco/rollup-plugin-web-worker-loader/blob/master/src/helper/node/WorkerClass.js

it's technically not importing worker_threads but the code to import it does exist in the bundle, which is probably why Vite is complaining.

I'll try to find a way to eliminate the warning, it would actually be pretty nice if this code never entered the bundle. Does fill mode work in your application?

jakubfiala commented 4 months ago

I'm seriously considering splitting out Fill Mode into a separate package and letting the developer create their own Worker with it. Firstly, there's probably lots of apps that don't require it, and secondly, bundling it has been causing issues all the time :grimacing:

ak37165 commented 4 months ago

I am not using fill mode (and have not tried it yet). Using atrament for the smooth drawing and that works great!

Separating out seems like a good design choice. Many apps already use a worker and can integrate with that if they need to use fill mode.

jakubfiala commented 4 months ago

@ak37165 this should now be fixed in v4.4.1. I'm going to close the issue as the culprit code clearly isn't present in the dist bundles but please do reopen if the issue persists in the new version.

ak37165 commented 4 months ago

Thanks for the fast fix! The warning does not show up anymore.