Skip to content

🎥 Make videos programmatically with React

Fix browser bundling of Node-only Whisper imports

JonnyBurger321b72c96b1d
diff --git a/packages/whisper-webgpu/src/download-whisper-model.ts b/packages/whisper-webgpu/src/download-whisper-model.ts
index 413edff57a..8ba608c87e 100644
--- a/packages/whisper-webgpu/src/download-whisper-model.ts
+++ b/packages/whisper-webgpu/src/download-whisper-model.ts
@@ -1,3 +1,7 @@
+import type * as NodeCrypto from 'node:crypto';
+import type * as NodeFsPromises from 'node:fs/promises';
+import type * as NodePath from 'node:path';
+import {importNodeModule} from './import-node-module';
import {
getHostedModelId,
getModelInfo,
@@ -111,14 +115,11 @@ export const downloadWhisperModel = ({
);
} else {
// Match Transformers.js FileCache's model/file layout without loading ONNX.
- const fsName: string = 'node:fs/promises';
- const pathName: string = 'node:path';
- const cryptoName: string = 'node:crypto';
const [{mkdir, open, rename, unlink}, {join, dirname}, {randomUUID}] =
await Promise.all([
- import(/* @vite-ignore */ fsName),
- import(/* @vite-ignore */ pathName),
- import(/* @vite-ignore */ cryptoName),
+ importNodeModule<typeof NodeFsPromises>('node:fs/promises'),
+ importNodeModule<typeof NodePath>('node:path'),
+ importNodeModule<typeof NodeCrypto>('node:crypto'),
]);
if (env.cacheDir === null) {
throw new Error('Transformers.js env.cacheDir must be set.');
diff --git a/packages/whisper-webgpu/src/import-node-module.ts b/packages/whisper-webgpu/src/import-node-module.ts
new file mode 100644
index 0000000000..57f7c7d23a
--- /dev/null
+++ b/packages/whisper-webgpu/src/import-node-module.ts
@@ -0,0 +1,10 @@
+// Called only in Node.js branches. An indirect import keeps native Node modules
+// out of browser bundles without making bundlers resolve a variable import.
+export const importNodeModule = <T>(specifier: string): Promise<T> => {
+ // eslint-disable-next-line no-new-func
+ const dynamicImport = new Function(
+ 'specifier',
+ 'return import(specifier)',
+ ) as (module: string) => Promise<T>;
+ return dynamicImport(specifier);
+};
diff --git a/packages/whisper-webgpu/src/probe-node-webgpu.ts b/packages/whisper-webgpu/src/probe-node-webgpu.ts
index 02aaa2603b..b44e5bd289 100644
--- a/packages/whisper-webgpu/src/probe-node-webgpu.ts
+++ b/packages/whisper-webgpu/src/probe-node-webgpu.ts
@@ -1,4 +1,5 @@
import type * as OnnxRuntimeNode from 'onnxruntime-node';
+import {importNodeModule} from './import-node-module';
// A 130-byte multiplication model from ONNX Runtime's test fixtures.
// https://github.com/microsoft/onnxruntime/blob/main/onnxruntime/test/testdata/mul_1.onnx
@@ -19,10 +20,8 @@ const webGpuProbeModel = new Uint8Array([
export const probeNodeWebGpu = async (): Promise<void> => {
// Keep the native Node.js addon out of browser bundles.
- const packageName: string = 'onnxruntime-node';
- const {InferenceSession} = (await import(
- /* @vite-ignore */ packageName
- )) as typeof OnnxRuntimeNode;
+ const {InferenceSession} =
+ await importNodeModule<typeof OnnxRuntimeNode>('onnxruntime-node');
const session = await InferenceSession.create(webGpuProbeModel, {
executionProviders: ['webgpu'],
});