diff --git a/vite.config.ts b/vite.config.ts index 678a86fab..2abba3dca 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -42,10 +42,12 @@ export const vitePluginsConfig = ({ }), react(), wasm(), - nodePolyfills({ - // Enables the 'events' module, which is required by the matrix-js-sdk - include: ["events"], - }), + exceptMediaPipeWasm( + nodePolyfills({ + // Enables the 'events' module, which is required by the matrix-js-sdk + include: ["events"], + }), + ), svgrPlugin({ svgrOptions: { // This enables ref forwarding on SVGR components, which is needed, for @@ -176,3 +178,37 @@ export default ({ }, }; }; + +/** + * Keeps a plugin's transform off MediaPipe's WASM loader. The loader is a + * classic script that sets `self.ModuleFactory`; transformed into a module it + * sets nothing, and the segmenter fails with "ModuleFactory not set". + */ +function exceptMediaPipeWasm(plugins: PluginOption): PluginOption { + const isLoader = (id: string): boolean => + id.includes("tasks-vision") && id.includes("wasm"); + return (Array.isArray(plugins) ? plugins : [plugins]).map((plugin) => { + if ( + !plugin || + typeof plugin !== "object" || + !("transform" in plugin) || + typeof plugin.transform !== "function" + ) + return plugin; + const transform = plugin.transform; + return { + ...plugin, + transform(this: unknown, code: string, id: string, options: unknown) { + if (isLoader(id)) return null; + return ( + transform as ( + this: unknown, + c: string, + i: string, + o: unknown, + ) => unknown + ).call(this, code, id, options); + }, + } as PluginOption; + }); +}