Skip to content

fix package export #1161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 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 Expand Up @@ -57,7 +57,7 @@
"dependencies": {
"@huggingface/jinja": "^0.3.3",
"onnxruntime-node": "1.20.1",
"onnxruntime-web": "1.21.0-dev.20250206-d981b153d3",
"onnxruntime-web": "1.22.0-dev.20250306-aafa8d170a",
"sharp": "^0.33.5"
},
"devDependencies": {
Expand Down
23 changes: 11 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,22 @@ 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.
// By default, we only do this if we are not in a service worker and the wasmPaths are not already set.
if (
// @ts-ignore Cannot find name 'ServiceWorkerGlobalScope'.ts(2304)
!(typeof ServiceWorkerGlobalScope !== 'undefined' && self instanceof ServiceWorkerGlobalScope)
&& !ONNX_ENV.wasm.wasmPaths
) {
ONNX_ENV.wasm.wasmPaths = `https://cdn.jsdelivr.net/npm/@huggingface/transformers@${env.version}/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;