Skip to content

Commit 5447d28

Browse files
committed
Upgrade package export
1 parent 6f43f24 commit 5447d28

File tree

4 files changed

+43
-46
lines changed

4 files changed

+43
-46
lines changed

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
"node": {
1010
"import": {
1111
"types": "./types/transformers.d.ts",
12-
"default": "./dist/transformers.mjs"
12+
"default": "./dist/transformers.node.mjs"
1313
},
1414
"require": {
1515
"types": "./types/transformers.d.ts",
16-
"default": "./dist/transformers.cjs"
16+
"default": "./dist/transformers.node.cjs"
1717
}
1818
},
1919
"default": {
2020
"types": "./types/transformers.d.ts",
21-
"default": "./dist/transformers.js"
21+
"default": "./dist/transformers.web.js"
2222
}
2323
},
2424
"scripts": {
@@ -57,7 +57,7 @@
5757
"dependencies": {
5858
"@huggingface/jinja": "^0.3.2",
5959
"onnxruntime-node": "1.20.1",
60-
"onnxruntime-web": "1.21.0-dev.20250114-228dd16893",
60+
"onnxruntime-web": "1.21.0-dev.20250129-80bc1d25f0",
6161
"sharp": "^0.33.5"
6262
},
6363
"devDependencies": {

src/backends/onnx.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ let ONNX;
5757
const ORT_SYMBOL = Symbol.for('onnxruntime');
5858

5959
if (ORT_SYMBOL in globalThis) {
60-
// If the JS runtime exposes their own ONNX runtime, use it
61-
ONNX = globalThis[ORT_SYMBOL];
60+
// If the JS runtime exposes their own ONNX runtime, use it
61+
ONNX = globalThis[ORT_SYMBOL];
6262

6363
} else if (apis.IS_NODE_ENV) {
6464
ONNX = ONNX_NODE.default ?? ONNX_NODE;
@@ -175,23 +175,27 @@ const ONNX_ENV = ONNX?.env;
175175
if (ONNX_ENV?.wasm) {
176176
// Initialize wasm backend with suitable default settings.
177177

178-
// (Optional) Set path to wasm files. This is needed when running in a web worker.
179-
// https://onnxruntime.ai/docs/api/js/interfaces/Env.WebAssemblyFlags.html#wasmPaths
180-
// We use remote wasm files by default to make it easier for newer users.
181-
// In practice, users should probably self-host the necessary .wasm files.
182-
ONNX_ENV.wasm.wasmPaths = `https://cdn.jsdelivr.net/npm/@huggingface/transformers@${env.version}/dist/`;
178+
// (Optional) Set path to wasm files. This will override the default path search behavior of onnxruntime-web.
179+
// Currently, this behavior is disabled. To enable it, uncomment the following code.
180+
181+
// // Override the wasm search path if:
182+
// // 1. We are not in a service worker.
183+
// // 2. ONNX Runtime Web version is defined.
184+
// // 3. The wasmPaths are not already set.
185+
// //
186+
// // @ts-ignore Cannot find name 'ServiceWorkerGlobalScope'.ts(2304)
187+
// if (!(typeof ServiceWorkerGlobalScope !== 'undefined' && self instanceof ServiceWorkerGlobalScope)
188+
// && typeof ONNX?.versions?.web === 'string'
189+
// && !ONNX_ENV.wasm.wasmPaths) {
190+
// ONNX_ENV.wasm.wasmPaths = `https://cdn.jsdelivr.net/npm/onnxruntime-web@${ONNX.versions.web}/dist/`;
191+
// }
183192

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

187196
// Users may wish to proxy the WASM backend to prevent the UI from freezing,
188197
// However, this is not necessary when using WebGPU, so we default to false.
189198
ONNX_ENV.wasm.proxy = false;
190-
191-
// https://developer.mozilla.org/en-US/docs/Web/API/crossOriginIsolated
192-
if (typeof crossOriginIsolated === 'undefined' || !crossOriginIsolated) {
193-
ONNX_ENV.wasm.numThreads = 1;
194-
}
195199
}
196200

197201
if (ONNX_ENV?.webgpu) {

webpack.config.js

+19-26
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,13 @@ class PostBuildPlugin {
2424
const file = path.join(dist, ORT_BUNDLE_FILE);
2525
if (fs.existsSync(file)) fs.unlinkSync(file);
2626
}
27-
27+
2828
// 2. Copy unbundled JSEP file
2929
{
3030
const src = path.join(__dirname, 'node_modules/onnxruntime-web/dist', ORT_JSEP_FILE);
3131
const dest = path.join(dist, ORT_JSEP_FILE);
3232
fs.copyFileSync(src, dest);
3333
}
34-
35-
// 3. Replace strings in certain files
36-
{
37-
const files = ['transformers.js', 'transformers.min.js'];
38-
for (const file of files) {
39-
const filePath = path.join(dist, file);
40-
let content = fs.readFileSync(filePath, 'utf8');
41-
content = content.replace(
42-
// Replace all instances of `new URL("./", import.meta.url)` with `new URL(import.meta.url)`,
43-
// as it causes several issues with build tools and bundlers.
44-
//
45-
// See the following issues for more information:
46-
// - https://github.com/huggingface/transformers.js/issues/911
47-
// - https://github.com/huggingface/transformers.js/issues/984
48-
// - https://github.com/huggingface/transformers.js/issues/980
49-
// - https://github.com/huggingface/transformers.js/issues/1021
50-
// - https://github.com/huggingface/transformers.js/issues/1026
51-
new RegExp('new URL\\(["\']\\.\\\/["\'],\\s*import\\.meta\\.url\\)', 'gm'),
52-
"new URL(import.meta.url)",
53-
);
54-
fs.writeFileSync(filePath, content, 'utf8');
55-
}
56-
}
5734
});
5835
}
5936
}
@@ -98,7 +75,7 @@ function buildConfig({
9875
type,
9976
},
10077
assetModuleFilename: "[name][ext]",
101-
chunkFormat: "module",
78+
chunkFormat: false,
10279
},
10380
optimization: {
10481
minimize: true,
@@ -157,28 +134,44 @@ const NODE_IGNORE_MODULES = ["onnxruntime-web"];
157134
// NOTE: This is necessary for both type="module" and type="commonjs",
158135
// and will be ignored when building for web (only used for node/deno)
159136
const NODE_EXTERNAL_MODULES = [
137+
"onnxruntime-common",
160138
"onnxruntime-node",
161139
"sharp",
162140
"fs",
163141
"path",
164142
"url",
165143
];
166144

145+
// Do not bundle the following modules with webpack (mark as external)
146+
const WEB_EXTERNAL_MODULES = [
147+
"onnxruntime-common",
148+
"onnxruntime-web",
149+
];
150+
167151
// Web-only build
168152
const WEB_BUILD = buildConfig({
153+
name: ".web",
154+
type: "module",
155+
externalModules: WEB_EXTERNAL_MODULES,
156+
});
157+
158+
// Web-only build, bundled with onnxruntime-web
159+
const BUNDLE_BUILD = buildConfig({
169160
type: "module",
170161
plugins: [new PostBuildPlugin()],
171162
});
172163

173164
// Node-compatible builds
174165
const NODE_BUILDS = [
175166
buildConfig({
167+
name: ".node",
176168
suffix: ".mjs",
177169
type: "module",
178170
ignoreModules: NODE_IGNORE_MODULES,
179171
externalModules: NODE_EXTERNAL_MODULES,
180172
}),
181173
buildConfig({
174+
name: ".node",
182175
suffix: ".cjs",
183176
type: "commonjs",
184177
ignoreModules: NODE_IGNORE_MODULES,
@@ -189,5 +182,5 @@ const NODE_BUILDS = [
189182
// When running with `webpack serve`, only build the web target.
190183
const BUILDS = process.env.WEBPACK_SERVE
191184
? [WEB_BUILD]
192-
: [WEB_BUILD, ...NODE_BUILDS];
185+
: [WEB_BUILD, BUNDLE_BUILD, ...NODE_BUILDS];
193186
export default BUILDS;

0 commit comments

Comments
 (0)