Closed Lee-ERP closed 6 months ago
Thanks a lot for reporting this and many thanks for the easy to reproduce test case.
As you already figured out the problem is caused by the unexpected export
statement. The import statements are split (https://github.com/chrisguttandin/standardized-audio-context/blob/master/src/helpers/split-import-statements.ts) when loading an AudioWorklet
and the rest of the file is wrapped in a function to patch the AudioWorkletProcessor
. The function which does that doesn't expect an export
statement.
I'm not sure if it's a good idea to handle the export
statement. I'm not even sure if it would be possible to do it in a proper way.
The test case can be fixed by removing the export
statement. Is there a specific reason for adding it?
https://stackblitz.com/edit/vitejs-vite-hjr5as?file=src%2Fworklet.ts
You're right, removing export
does appear to work, and I guess it's not required because I'm not importing the class to my consuming file, I'm importing a reference to the file. That's what you get for blindly sticking to conventions! (in my defence, I wrote my first line of TypeScript about a 1 week ago).
Thank you for spending the time to look at it and share a fix, I can't believe I spent 2 days on that.
And thank you even more for sharing this library. Ignoring this (very self-created) issue it took no more than an hour to go from only working on Chrome, to working on everything I tested it on.
That's great. Thanks for letting me know.
Trying to change our application from native AudioContext to this library and the
addModule
function fails with :I suspect this has something to do with the way Vite wraps up the import statement, not being compatible with the way in which this library wraps the workers.
At this line: https://github.com/chrisguttandin/standardized-audio-context/blob/master/src/factories/add-audio-worklet-module.ts#L221 the
wrappedSource
variable containsAnd the
url
variable is"blob:http://localhost:5174/1ecba6a7-f38a-4f15-8971-dd4ddce38fb7"
But i'm out of my depth on this one so i'm hoping someone familiar with how this library works will know why this doesn't work, and maybe how to fix it.
My fallback solution is to try and transpile the worker module separately to the rest of the app and import it via plain URL, but if imports with Vite could be made to work, that would be better.
I have created a minimal reproduction case here, open console log and click a button. Note how the native one doesn't throw an error.
https://stackblitz.com/edit/vitejs-vite-wxnz95?file=src%2Fworklet.ts