-
Notifications
You must be signed in to change notification settings - Fork 135
Support Webpack and fixes #558
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
Changes from 3 commits
93c8945
1daa163
2f2fe19
240ff96
3e8eac6
2474fb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -24,9 +24,6 @@ export type AudioProps = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| speaking?: SpeakingProps; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Unfortunately, we need to use a Vite-exclusive import for now. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import RenderWorklet from "./render-worklet?worker&url"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Downloads audio from a track and emits it to an AudioContext. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // The user is responsible for hooking up audio to speakers, an analyzer, etc. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export class Audio { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -94,7 +91,15 @@ export class Audio { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| effect.spawn(async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Register the AudioWorklet processor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await context.audioWorklet.addModule(RenderWorklet); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Hacky workaround to support Webpack and Vite: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // https://github.com/webpack/webpack/issues/11543#issuecomment-2045809214 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { register } = navigator.serviceWorker; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // @ts-ignore hack to make webpack believe that it is registering a worker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| navigator.serviceWorker.register = (url: URL) => context.audioWorklet.addModule(url); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await navigator.serviceWorker.register(new URL("./render-worklet", import.meta.url)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| navigator.serviceWorker.register = register; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Hacky workaround to support Webpack and Vite: | |
| // https://github.com/webpack/webpack/issues/11543#issuecomment-2045809214 | |
| const { register } = navigator.serviceWorker; | |
| // @ts-ignore hack to make webpack believe that it is registering a worker | |
| navigator.serviceWorker.register = (url: URL) => context.audioWorklet.addModule(url); | |
| await navigator.serviceWorker.register(new URL("./render-worklet", import.meta.url)); | |
| navigator.serviceWorker.register = register; | |
| // Hacky workaround to support Webpack and Vite: | |
| // https://github.com/webpack/webpack/issues/11543#issuecomment-2045809214 | |
| const sw: any = (navigator as any).serviceWorker; | |
| const workletUrl = new URL("./render-worklet", import.meta.url); | |
| if (!sw || typeof sw.register !== "function") { | |
| // Fallback: no Service Worker support—load directly. | |
| await context.audioWorklet.addModule(workletUrl); | |
| } else { | |
| const originalRegister = sw.register.bind(sw); | |
| // @ts-expect-error: Intentionally masquerading as ServiceWorker.register for bundlers. | |
| sw.register = (url: URL) => context.audioWorklet.addModule(url); | |
| try { | |
| await sw.register(workletUrl); | |
| } finally { | |
| sw.register = originalRegister; | |
| } | |
| } |
🤖 Prompt for AI Agents
In js/hang/src/watch/audio/index.ts around lines 95 to 103, the code
monkey-patches navigator.serviceWorker.register without checking
navigator.serviceWorker exists and without a try/finally to guarantee
restoration; change it to first guard that navigator.serviceWorker and
navigator.serviceWorker.register are defined, save the original register, then
perform the temporary assignment and await addModule inside a try block, and
always restore the original register in a finally block so the global is never
left patched even if addModule throws or navigator.serviceWorker is absent.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,9 +9,10 @@ | |
| "concurrently": "^9.1.2", | ||
| "cpy-cli": "^5.0.0", | ||
| "knip": "^5.60.2", | ||
| "prettier": "^3.6.2", | ||
| "rimraf": "^6.0.1", | ||
| "typescript": "^5.8.3", | ||
| "tsx": "^4.20.1" | ||
| "tsx": "^4.20.1", | ||
| "typescript": "^5.8.3" | ||
| }, | ||
| "packageManager": "[email protected]" | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same global override concerns: add guard + try/finally and prefer @ts-expect-error
Mirror the safer pattern here to prevent leaking the patched
registerand to handle no-SW environments gracefully.Apply this diff:
📝 Committable suggestion
🤖 Prompt for AI Agents