Fix worker import in Safari and Firefox

This commit is contained in:
Jerome Wu 2023-07-28 22:54:56 +08:00
parent f22c3076df
commit 07b39072c3
7 changed files with 25 additions and 19 deletions

2
package-lock.json generated
View File

@ -17569,7 +17569,7 @@
},
"packages/ffmpeg": {
"name": "@ffmpeg/ffmpeg",
"version": "0.12.1",
"version": "0.12.2",
"license": "MIT",
"dependencies": {
"@ffmpeg/types": "^0.12.0"

View File

@ -1,6 +1,6 @@
{
"name": "@ffmpeg/ffmpeg",
"version": "0.12.1",
"version": "0.12.2",
"description": "FFmpeg WebAssembly version for browser",
"main": "./dist/umd/ffmpeg.js",
"types": "./dist/esm/index.d.ts",
@ -17,7 +17,8 @@
"clean": "rimraf dist",
"build:esm": "tsc -p tsconfig.esm.json",
"build:umd": "webpack",
"build": "npm run clean && npm run build:esm && npm run build:umd"
"build": "npm run clean && npm run build:esm && npm run build:umd",
"prepublishOnly": "npm run build"
},
"files": [
"dist",

View File

@ -5,7 +5,7 @@ export const CORE_VERSION = "0.12.1";
export const CORE_URL = `https://unpkg.com/@ffmpeg/core@${CORE_VERSION}/dist/umd/ffmpeg-core.js`;
export enum FFMessageType {
LOAD = "load",
LOAD = "LOAD",
EXEC = "EXEC",
WRITE_FILE = "WRITE_FILE",
READ_FILE = "READ_FILE",

View File

@ -3,3 +3,6 @@ export const ERROR_NOT_LOADED = new Error(
"ffmpeg is not loaded, call `await ffmpeg.load()` first"
);
export const ERROR_TERMINATED = new Error("called FFmpeg.terminate()");
export const ERROR_IMPORT_FAILURE = new Error(
"failed to import ffmpeg-core.js"
);

View File

@ -22,13 +22,6 @@ export interface FFMessageLoadConfig {
* @defaultValue `https://unpkg.com/@ffmpeg/core-mt@${CORE_VERSION}/dist/umd/ffmpeg-core.worker.js`;
*/
workerURL?: string;
/**
* When `thread` is true, ffmpeg imports `ffmpeg-core.worker.js` and thus
* makes multi-threaded core work.
*
* @defaultValue `false`
*/
thread?: boolean;
}
export interface FFMessageExecData {

View File

@ -22,7 +22,11 @@ import type {
FileData,
} from "./types";
import { CORE_URL, FFMessageType } from "./const.js";
import { ERROR_UNKNOWN_MESSAGE_TYPE, ERROR_NOT_LOADED } from "./errors.js";
import {
ERROR_UNKNOWN_MESSAGE_TYPE,
ERROR_NOT_LOADED,
ERROR_IMPORT_FAILURE,
} from "./errors.js";
declare global {
interface WorkerGlobalScope {
@ -51,13 +55,17 @@ const load = async ({
try {
// when web worker type is `classic`.
importScripts(coreURL);
} catch (e: unknown) {
} catch {
// when web worker type is `module`.
if (e instanceof TypeError && e.toString().includes("Module scripts")) {
(self as WorkerGlobalScope).createFFmpegCore = (
(await import(coreURL)) as ImportedFFmpegCoreModuleFactory
).default;
} else throw e;
(self as WorkerGlobalScope).createFFmpegCore = (
(await import(
/* @vite-ignore */ coreURL
)) as ImportedFFmpegCoreModuleFactory
).default;
if (!(self as WorkerGlobalScope).createFFmpegCore) {
throw ERROR_IMPORT_FAILURE;
}
}
ffmpeg = await (self as WorkerGlobalScope).createFFmpegCore({

View File

@ -17,7 +17,8 @@
"clean": "rimraf dist",
"build:esm": "tsc -p tsconfig.esm.json",
"build:umd": "tsc -p tsconfig.cjs.json && webpack",
"build": "npm run clean && npm run build:esm && npm run build:umd"
"build": "npm run clean && npm run build:esm && npm run build:umd",
"prepublishOnly": "npm run build"
},
"files": [
"dist"