Fix Webpack bundling of recorder worklet (#21239)

* Fix Webpack bundling of recorder worklet
pull/21245/head
Steve Repsher 2024-07-01 10:31:22 -04:00 committed by GitHub
parent e97be57e3b
commit d01377da3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 14 deletions

View File

@ -74,6 +74,9 @@ const createWebpackConfig = ({
resolve: {
fullySpecified: false,
},
parser: {
worker: ["*context.audioWorklet.addModule()", "..."],
},
},
{
test: /\.css$/,
@ -92,11 +95,15 @@ const createWebpackConfig = ({
moduleIds: isProdBuild && !isStatsBuild ? "deterministic" : "named",
chunkIds: isProdBuild && !isStatsBuild ? "deterministic" : "named",
splitChunks: {
// Disable splitting for web workers with ESM output
// Imports of external chunks are broken
chunks: latestBuild
? (chunk) => !chunk.canBeInitial() && !/^.+-worker$/.test(chunk.name)
: undefined,
// Disable splitting for web workers and worklets because imports of
// external chunks are broken for:
// - ESM output: https://github.com/webpack/webpack/issues/17014
// - Worklets use `importScripts`: https://github.com/webpack/webpack/issues/11543
chunks: (chunk) =>
!chunk.canBeInitial() &&
!new RegExp(`^.+-work${latestBuild ? "(?:let|er)" : "let"}$`).test(
chunk.name
),
},
},
plugins: [

View File

@ -70,17 +70,18 @@ export class AudioRecorder {
}
private async _createContext() {
// @ts-ignore-next-line
this._context = new (window.AudioContext || window.webkitAudioContext)();
// @ts-expect-error webkitAudioContext is not recognized
const context = new (AudioContext || webkitAudioContext)();
this._stream = await navigator.mediaDevices.getUserMedia({ audio: true });
await this._context.audioWorklet.addModule(
new URL("./recorder.worklet.js", import.meta.url)
// Syntax here must match an item of `parser.worker` in Webpack config in
// order for module to be parsed and a chunk to be properly created.
await context.audioWorklet.addModule(
/* webpackChunkName: "recorder-worklet" */
new URL("./recorder-worklet.js", import.meta.url)
);
this._context = context;
this._source = this._context.createMediaStreamSource(this._stream);
this._recorder = new AudioWorkletNode(this._context, "recorder.worklet");
this._recorder = new AudioWorkletNode(this._context, "recorder-worklet");
this._recorder.port.onmessage = (e) => {
if (!this._active) {
return;

View File

@ -18,4 +18,4 @@ class RecorderProcessor extends AudioWorkletProcessor {
}
}
registerProcessor("recorder.worklet", RecorderProcessor);
registerProcessor("recorder-worklet", RecorderProcessor);