Skip to content

Commit a5322c4

Browse files
fs-eirexenova
andauthored
Fix package export (#1161)
* Upgrade package export * set wasm path * Update default wasmPaths URL * Bump versions * Set version correctly for separate PR --------- Co-authored-by: Joshua Lochner <[email protected]>
1 parent 3d6bcf4 commit a5322c4

File tree

4 files changed

+47
-51
lines changed

4 files changed

+47
-51
lines changed

package-lock.json

+8-8
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.3",
5959
"onnxruntime-node": "1.20.1",
60-
"onnxruntime-web": "1.21.0-dev.20250206-d981b153d3",
60+
"onnxruntime-web": "1.22.0-dev.20250306-aafa8d170a",
6161
"sharp": "^0.33.5"
6262
},
6363
"devDependencies": {

src/backends/onnx.js

+11-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,22 @@ 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+
// By default, we only do this if we are not in a service worker and the wasmPaths are not already set.
180+
if (
181+
// @ts-ignore Cannot find name 'ServiceWorkerGlobalScope'.ts(2304)
182+
!(typeof ServiceWorkerGlobalScope !== 'undefined' && self instanceof ServiceWorkerGlobalScope)
183+
&& !ONNX_ENV.wasm.wasmPaths
184+
) {
185+
ONNX_ENV.wasm.wasmPaths = `https://cdn.jsdelivr.net/npm/@huggingface/transformers@${env.version}/dist/`;
186+
}
183187

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

187191
// Users may wish to proxy the WASM backend to prevent the UI from freezing,
188192
// However, this is not necessary when using WebGPU, so we default to false.
189193
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-
}
195194
}
196195

197196
if (ONNX_ENV?.webgpu) {

webpack.config.js

+24-27
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,48 @@ 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 onnxruntime-node when packaging for the web.
146+
const WEB_IGNORE_MODULES = ["onnxruntime-node"];
147+
148+
// Do not bundle the following modules with webpack (mark as external)
149+
const WEB_EXTERNAL_MODULES = [
150+
"onnxruntime-common",
151+
"onnxruntime-web",
152+
];
153+
167154
// Web-only build
168155
const WEB_BUILD = buildConfig({
156+
name: ".web",
157+
type: "module",
158+
ignoreModules: WEB_IGNORE_MODULES,
159+
externalModules: WEB_EXTERNAL_MODULES,
160+
});
161+
162+
// Web-only build, bundled with onnxruntime-web
163+
const BUNDLE_BUILD = buildConfig({
169164
type: "module",
170165
plugins: [new PostBuildPlugin()],
171166
});
172167

173168
// Node-compatible builds
174169
const NODE_BUILDS = [
175170
buildConfig({
171+
name: ".node",
176172
suffix: ".mjs",
177173
type: "module",
178174
ignoreModules: NODE_IGNORE_MODULES,
179175
externalModules: NODE_EXTERNAL_MODULES,
180176
}),
181177
buildConfig({
178+
name: ".node",
182179
suffix: ".cjs",
183180
type: "commonjs",
184181
ignoreModules: NODE_IGNORE_MODULES,
@@ -188,6 +185,6 @@ const NODE_BUILDS = [
188185

189186
// When running with `webpack serve`, only build the web target.
190187
const BUILDS = process.env.WEBPACK_SERVE
191-
? [WEB_BUILD]
192-
: [WEB_BUILD, ...NODE_BUILDS];
188+
? [BUNDLE_BUILD]
189+
: [BUNDLE_BUILD, WEB_BUILD, ...NODE_BUILDS];
193190
export default BUILDS;

0 commit comments

Comments
 (0)