#How to use web workers with Vite ?

1 messages · Page 1 of 1 (latest)

crystal vale
#

Hey all,

I'm trying to decouple a unifiedjs parser (to translate md to html) in a separate web worker but I'm running into this error:

document is not defined

If I remove the unified parser everything works fine, so the issue is (probably) coming from there. I noticed they are using a package called decode-named-character-reference which, turns out, has a worker-specific export. It looks like Vite does not know - or care - for this, deciding to bundle the browser version and causing the error.

Is there a way to mark the worker file as a worker so that the imports can be resolved correctly ? Or am I doing to blocking the main thread xD ?

crystal vale
#

How to use web workers with Vite ?

#

edit: updated the post title to make it a bit more understandable xD