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

URL is not defined when adding AudioWorklet in Preact preset #9606

Closed
7 tasks done
pyotrr opened this issue Aug 10, 2022 · 2 comments
Closed
7 tasks done

URL is not defined when adding AudioWorklet in Preact preset #9606

pyotrr opened this issue Aug 10, 2022 · 2 comments

Comments

@pyotrr
Copy link

pyotrr commented Aug 10, 2022

Describe the bug

I created a Preact project using yarn create vite and I'm importing my AudioWorklet class as an url using the ?url import syntax. When i try to attach that worklet to my AudioContext i get an Uncaught ReferenceError: URL is not defined in vite's file called client.ts, in this line:
const importMetaUrl = new URL(import.meta.url)
This error occurs when i call AudioContext.audioWorklet.addModule(importedWorkletUrl)
Do you know what could be causing this problem or if there's any way to successfully use worklets in this setup?
image

Reproduction

https://github.com/pyotrr/vite-audio-worklet-repro

System Info

System:
    OS: macOS 12.0.1
    CPU: (8) arm64 Apple M1
    Memory: 65.42 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v16.13.0/bin/yarn
    npm: 8.1.0 - ~/.nvm/versions/node/v16.13.0/bin/npm
  Browsers:
    Chrome: 104.0.5112.79
    Firefox: 102.0.1
    Safari: 15.1
  npmPackages:
    vite: ^3.0.0 => 3.0.5

Used Package Manager

yarn

Logs

No response

Validations

@bluwy
Copy link
Member

bluwy commented Aug 16, 2022

This is because Vite injects HMR code for Processor.ts when being imported, which imports from Vite's client.ts, which isn't compatible in an AudioWorklet environment. Since Worklets are an experimental feature, and they should ideally support common globals like URL, I think this is something we won't support for the time being.

For now you can workaround it by placing the processor JS code inside the public directory, and import the module from there.

@bluwy bluwy closed this as completed Aug 16, 2022
@pyotrr
Copy link
Author

pyotrr commented Aug 16, 2022

For now you can workaround it by placing the processor JS code inside the public directory, and import the module from there.

Understandable, thanks!

@github-actions github-actions bot locked and limited conversation to collaborators Aug 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants