Skip to content

Commit

Permalink
Fix Webpack bundling of recorder worklet (#21239)
Browse files Browse the repository at this point in the history
* Fix Webpack bundling of recorder worklet
  • Loading branch information
steverep authored Jul 1, 2024
1 parent e97be57 commit d01377d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
17 changes: 12 additions & 5 deletions build-scripts/webpack.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ const createWebpackConfig = ({
resolve: {
fullySpecified: false,
},
parser: {
worker: ["*context.audioWorklet.addModule()", "..."],
},
},
{
test: /\.css$/,
Expand All @@ -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: [
Expand Down
17 changes: 9 additions & 8 deletions src/util/audio-recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ class RecorderProcessor extends AudioWorkletProcessor {
}
}

registerProcessor("recorder.worklet", RecorderProcessor);
registerProcessor("recorder-worklet", RecorderProcessor);

0 comments on commit d01377d

Please sign in to comment.