Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ __pycache__
node_modules
.cache
.DS_STORE
deno.lock

# Do not track build artifacts/generated files
/dist
Expand Down
7 changes: 0 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,6 @@
"README.md",
"LICENSE"
],
"browser": {
"fs": false,
"path": false,
"url": false,
"sharp": false,
"onnxruntime-node": false
},
"publishConfig": {
"access": "public"
},
Expand Down
14 changes: 9 additions & 5 deletions src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
* @module env
*/

import fs from 'fs';
import path from 'path';
import url from 'url';
import fs from 'node:fs';
import path from 'node:path';
import url from 'node:url';

const VERSION = '3.5.2';

Expand All @@ -40,6 +40,10 @@ const IS_NODE_ENV = IS_PROCESS_AVAILABLE && process?.release?.name === 'node';
const IS_FS_AVAILABLE = !isEmpty(fs);
const IS_PATH_AVAILABLE = !isEmpty(path);

// Runtime detection
const IS_DENO_RUNTIME = typeof globalThis.Deno !== 'undefined';
const IS_BUN_RUNTIME = typeof globalThis.Bun !== 'undefined';

/**
* A read-only object containing information about the APIs available in the current environment.
*/
Expand All @@ -62,7 +66,7 @@ export const apis = Object.freeze({
/** Whether the Node.js process API is available */
IS_PROCESS_AVAILABLE,

/** Whether we are running in a Node.js environment */
/** Whether we are running in a Node.js-like environment (node, deno, bun) */
IS_NODE_ENV,

/** Whether the filesystem API is available */
Expand Down Expand Up @@ -143,7 +147,7 @@ export const env = {
useFS: IS_FS_AVAILABLE,

/////////////////// Cache settings ///////////////////
useBrowserCache: IS_WEB_CACHE_AVAILABLE,
useBrowserCache: IS_WEB_CACHE_AVAILABLE && !IS_DENO_RUNTIME,

useFSCache: IS_FS_AVAILABLE,
cacheDir: DEFAULT_CACHE_DIR,
Expand Down
3 changes: 1 addition & 2 deletions src/utils/audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import {
calculateReflectOffset, saveBlob,
} from './core.js';
import { apis } from '../env.js';
import fs from 'fs';
import { Tensor, matmul } from './tensor.js';

import fs from 'node:fs';

/**
* Helper function to read audio from a path/URL.
Expand Down
4 changes: 2 additions & 2 deletions src/utils/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* @module utils/hub
*/

import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';

import { apis, env } from '../env.js';
import { dispatchCallback } from './core.js';
Expand Down
6 changes: 3 additions & 3 deletions tests/test_utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

export async function loadAudio(url) {
// NOTE: Since the Web Audio API is not available in Node.js, we will need to use the `wavefile` library to obtain the raw audio data.
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/hub.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AutoModel, PreTrainedModel } from "../../src/models.js";

import { MAX_TEST_EXECUTION_TIME, DEFAULT_MODEL_OPTIONS } from "../init.js";
import fs from "fs";
import fs from "node:fs";

// TODO: Set cache folder to a temp directory

Expand Down
53 changes: 43 additions & 10 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
import { fileURLToPath } from "node:url";
import path from "node:path";
import fs from "node:fs";
import webpack from "webpack";
import TerserPlugin from "terser-webpack-plugin";
import { fileURLToPath } from "url";
import path from "path";
import fs from "fs";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

/**
* Plugin to strip the "node:" prefix from module requests.
*
* This is necessary to ensure both web and node builds work correctly,
* otherwise we would get an error like:
* ```
* Module build failed: UnhandledSchemeError: Reading from "node:path" is not handled by plugins (Unhandled scheme).
* Webpack supports "data:" and "file:" URIs by default.
* You may need an additional plugin to handle "node:" URIs.
* ```
*
* NOTE: We then do not need to use the `node:` prefix in the resolve.alias configuration.
*/
class StripNodePrefixPlugin extends webpack.NormalModuleReplacementPlugin {
constructor() {
super(
/^node:(.+)$/,
resource => {
resource.request = resource.request.replace(/^node:/, '');
}
);
}
}

/**
* Plugin to post-process build files. Required to solve certain issues with ESM module output.
* See https://github.com/webpack/webpack/issues/17121 for more information.
Expand Down Expand Up @@ -55,7 +80,6 @@ function buildConfig({
plugins = [],
} = {}) {
const outputModule = type === "module";

const alias = Object.fromEntries(
ignoreModules.map((module) => [module, false]),
);
Expand Down Expand Up @@ -137,13 +161,15 @@ const NODE_EXTERNAL_MODULES = [
"onnxruntime-common",
"onnxruntime-node",
"sharp",
"fs",
"path",
"url",
"node:fs",
"node:path",
"node:url",
];

// Do not bundle onnxruntime-node when packaging for the web.
const WEB_IGNORE_MODULES = ["onnxruntime-node"];
// Do not bundle node-only packages when bundling for the web.
// NOTE: We can exclude the "node:" prefix for built-in modules here,
// since we apply the `StripNodePrefixPlugin` to strip it.
const WEB_IGNORE_MODULES = ["onnxruntime-node", "sharp", "fs", "path", "url"];

// Do not bundle the following modules with webpack (mark as external)
const WEB_EXTERNAL_MODULES = [
Expand All @@ -157,12 +183,19 @@ const WEB_BUILD = buildConfig({
type: "module",
ignoreModules: WEB_IGNORE_MODULES,
externalModules: WEB_EXTERNAL_MODULES,
plugins: [
new StripNodePrefixPlugin()
]
});

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

// Node-compatible builds
Expand Down