Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
"node": {
"import": {
"types": "./types/transformers.d.ts",
"default": "./dist/transformers.mjs"
"default": "./dist/transformers.node.mjs"
},
"require": {
"types": "./types/transformers.d.ts",
"default": "./dist/transformers.cjs"
"default": "./dist/transformers.node.cjs"
}
},
"default": {
"types": "./types/transformers.d.ts",
"default": "./dist/transformers.js"
"default": "./dist/transformers.web.js"
}
},
"scripts": {
Expand Down
27 changes: 15 additions & 12 deletions src/backends/onnx.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ let ONNX;
const ORT_SYMBOL = Symbol.for('onnxruntime');

if (ORT_SYMBOL in globalThis) {
// If the JS runtime exposes their own ONNX runtime, use it
ONNX = globalThis[ORT_SYMBOL];
// If the JS runtime exposes their own ONNX runtime, use it
ONNX = globalThis[ORT_SYMBOL];

} else if (apis.IS_NODE_ENV) {
ONNX = ONNX_NODE.default ?? ONNX_NODE;
Expand Down Expand Up @@ -175,23 +175,26 @@ const ONNX_ENV = ONNX?.env;
if (ONNX_ENV?.wasm) {
// Initialize wasm backend with suitable default settings.

// (Optional) Set path to wasm files. This is needed when running in a web worker.
// https://onnxruntime.ai/docs/api/js/interfaces/Env.WebAssemblyFlags.html#wasmPaths
// We use remote wasm files by default to make it easier for newer users.
// In practice, users should probably self-host the necessary .wasm files.
ONNX_ENV.wasm.wasmPaths = `https://cdn.jsdelivr.net/npm/@huggingface/transformers@${env.version}/dist/`;
// (Optional) Set path to wasm files. This will override the default path search behavior of onnxruntime-web.

// Override the wasm search path if:
// 1. We are not in a service worker.
// 2. ONNX Runtime Web version is defined.
// 3. The wasmPaths are not already set.
//
// @ts-ignore Cannot find name 'ServiceWorkerGlobalScope'.ts(2304)
if (!(typeof ServiceWorkerGlobalScope !== 'undefined' && self instanceof ServiceWorkerGlobalScope)
&& typeof ONNX?.versions?.web === 'string'
&& !ONNX_ENV.wasm.wasmPaths) {
ONNX_ENV.wasm.wasmPaths = `https://cdn.jsdelivr.net/npm/onnxruntime-web@${ONNX.versions.web}/dist/`;
}

// TODO: Add support for loading WASM files from cached buffer when we upgrade to [email protected]
// https://github.com/microsoft/onnxruntime/pull/21534

// Users may wish to proxy the WASM backend to prevent the UI from freezing,
// However, this is not necessary when using WebGPU, so we default to false.
ONNX_ENV.wasm.proxy = false;

// https://developer.mozilla.org/en-US/docs/Web/API/crossOriginIsolated
if (typeof crossOriginIsolated === 'undefined' || !crossOriginIsolated) {
ONNX_ENV.wasm.numThreads = 1;
}
}

if (ONNX_ENV?.webgpu) {
Expand Down
51 changes: 24 additions & 27 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,13 @@ class PostBuildPlugin {
const file = path.join(dist, ORT_BUNDLE_FILE);
if (fs.existsSync(file)) fs.unlinkSync(file);
}

// 2. Copy unbundled JSEP file
{
const src = path.join(__dirname, 'node_modules/onnxruntime-web/dist', ORT_JSEP_FILE);
const dest = path.join(dist, ORT_JSEP_FILE);
fs.copyFileSync(src, dest);
}

// 3. Replace strings in certain files
{
const files = ['transformers.js', 'transformers.min.js'];
for (const file of files) {
const filePath = path.join(dist, file);
let content = fs.readFileSync(filePath, 'utf8');
content = content.replace(
// Replace all instances of `new URL("./", import.meta.url)` with `new URL(import.meta.url)`,
// as it causes several issues with build tools and bundlers.
//
// See the following issues for more information:
// - https://github.com/huggingface/transformers.js/issues/911
// - https://github.com/huggingface/transformers.js/issues/984
// - https://github.com/huggingface/transformers.js/issues/980
// - https://github.com/huggingface/transformers.js/issues/1021
// - https://github.com/huggingface/transformers.js/issues/1026
new RegExp('new URL\\(["\']\\.\\\/["\'],\\s*import\\.meta\\.url\\)', 'gm'),
"new URL(import.meta.url)",
);
fs.writeFileSync(filePath, content, 'utf8');
}
}
});
}
}
Expand Down Expand Up @@ -98,7 +75,7 @@ function buildConfig({
type,
},
assetModuleFilename: "[name][ext]",
chunkFormat: "module",
chunkFormat: false,
},
optimization: {
minimize: true,
Expand Down Expand Up @@ -157,28 +134,48 @@ const NODE_IGNORE_MODULES = ["onnxruntime-web"];
// NOTE: This is necessary for both type="module" and type="commonjs",
// and will be ignored when building for web (only used for node/deno)
const NODE_EXTERNAL_MODULES = [
"onnxruntime-common",
"onnxruntime-node",
"sharp",
"fs",
"path",
"url",
];

// Do not bundle onnxruntime-node when packaging for the web.
const WEB_IGNORE_MODULES = ["onnxruntime-node"];

// Do not bundle the following modules with webpack (mark as external)
const WEB_EXTERNAL_MODULES = [
"onnxruntime-common",
"onnxruntime-web",
];

// Web-only build
const WEB_BUILD = buildConfig({
name: ".web",
type: "module",
ignoreModules: WEB_IGNORE_MODULES,
externalModules: WEB_EXTERNAL_MODULES,
});

// Web-only build, bundled with onnxruntime-web
const BUNDLE_BUILD = buildConfig({
type: "module",
plugins: [new PostBuildPlugin()],
});

// Node-compatible builds
const NODE_BUILDS = [
buildConfig({
name: ".node",
suffix: ".mjs",
type: "module",
ignoreModules: NODE_IGNORE_MODULES,
externalModules: NODE_EXTERNAL_MODULES,
}),
buildConfig({
name: ".node",
suffix: ".cjs",
type: "commonjs",
ignoreModules: NODE_IGNORE_MODULES,
Expand All @@ -188,6 +185,6 @@ const NODE_BUILDS = [

// When running with `webpack serve`, only build the web target.
const BUILDS = process.env.WEBPACK_SERVE
? [WEB_BUILD]
: [WEB_BUILD, ...NODE_BUILDS];
? [BUNDLE_BUILD]
: [BUNDLE_BUILD, WEB_BUILD, ...NODE_BUILDS];
export default BUILDS;