1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
diff --git a/package.json b/package.json
index b5c3e86..16b9b0e 100644
--- a/package.json
+++ b/package.json
@@ -26,7 +26,9 @@
"format:check": "prettier --check .",
"typegen": "tsc --build",
"dev": "webpack serve --no-client-overlay",
- "build": "webpack && npm run typegen",
+ "build:dev": "webpack --mode development",
+ "build:prod": "webpack --mode production",
+ "build": "npm run build:prod && npm run typegen",
"test": "node --experimental-vm-modules --expose-gc node_modules/jest/bin/jest.js --verbose",
"readme": "python ./docs/scripts/build_readme.py",
"docs-api": "node ./docs/scripts/generate.js",
diff --git a/src/backends/onnx.js b/src/backends/onnx.js
index a64f9d1..4557f20 100644
--- a/src/backends/onnx.js
+++ b/src/backends/onnx.js
@@ -20,7 +20,7 @@ import { env, apis } from '../env.js';
// NOTE: Import order matters here. We need to import `onnxruntime-node` before `onnxruntime-web`.
// In either case, we select the default export if it exists, otherwise we use the named export.
-import * as ONNX_NODE from 'onnxruntime-node';
+const ONNX_NODE = null;
import * as ONNX_WEB from 'onnxruntime-web';
export { Tensor } from 'onnxruntime-common';
@@ -59,7 +59,8 @@ 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];
-
+ supportedDevices.push(...ONNX.supportedDevices);
+ defaultDevices = ONNX.defaultDevices;
} else if (apis.IS_NODE_ENV) {
ONNX = ONNX_NODE.default ?? ONNX_NODE;
diff --git a/src/utils/tensor.js b/src/utils/tensor.js
index 357aba9..85c6d46 100644
--- a/src/utils/tensor.js
+++ b/src/utils/tensor.js
@@ -89,9 +89,13 @@ export class Tensor {
this.ort_tensor = /** @type {ONNXTensor} */ (args[0]);
} else {
// Create new tensor
- this.ort_tensor = new ONNXTensor(
+ const sym = Symbol.for("onnxruntime");
+ const ctor = sym in globalThis
+ ? globalThis.Tensor
+ : ONNXTensor;
+
+ this.ort_tensor = new ctor(
/** @type {DataType} */(args[0]),
- // @ts-expect-error ts(2769) Type 'number' is not assignable to type 'bigint'.
/** @type {Exclude<import('./maths.js').AnyTypedArray, Uint8ClampedArray>} */(args[1]),
args[2],
);
diff --git a/webpack.config.js b/webpack.config.js
index 09611f1..742da97 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -14,10 +14,11 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
class PostBuildPlugin {
apply(compiler) {
- compiler.hooks.done.tap('PostBuildPlugin', () => {
- const dist = path.join(__dirname, 'dist');
- const ORT_JSEP_FILE = 'ort-wasm-simd-threaded.jsep.mjs';
- const ORT_BUNDLE_FILE = 'ort.bundle.min.mjs';
+ compiler.hooks.done.tap("PostBuildPlugin", () => {
+ const dist = path.join(__dirname, "dist");
+ const ORT_JSEP_FILE = "ort-wasm-simd-threaded.jsep.mjs";
+ const isProd = compiler.options.mode === "production";
+ const ORT_BUNDLE_FILE = isProd ? "ort.webgpu.mjs" : "ort.webgpu-dev.mjs"; // Dynamic filename
// 1. Remove unnecessary files
{
@@ -36,155 +37,62 @@ class PostBuildPlugin {
}
/**
- * Helper function to create webpack configurations.
- * @param {Object} options Options for creating a webpack target.
- * @param {string} options.name Name of output file.
- * @param {string} options.suffix Suffix of output file.
- * @param {string} options.type Type of library.
- * @param {string} options.ignoreModules The list of modules to ignore.
- * @param {string} options.externalModules The list of modules to set as external.
- * @param {Object[]} options.plugins List of plugins to use.
- * @returns {import('webpack').Configuration} One webpack target.
+ * Generate Webpack configuration dynamically based on the environment.
*/
-function buildConfig({
- name = "",
- suffix = ".js",
- type = "module", // 'module' | 'commonjs'
- ignoreModules = [],
- externalModules = [],
- plugins = [],
-} = {}) {
- const outputModule = type === "module";
-
- const alias = Object.fromEntries(
- ignoreModules.map((module) => [module, false]),
- );
-
- /** @type {import('webpack').Configuration} */
- const config = {
- mode: "development",
- devtool: "source-map",
- entry: {
- [`transformers${name}`]: "./src/transformers.js",
- [`transformers${name}.min`]: "./src/transformers.js",
- },
- output: {
- filename: `[name]${suffix}`,
- path: path.join(__dirname, "dist"),
- library: {
- type,
- },
- assetModuleFilename: "[name][ext]",
- chunkFormat: false,
- },
- optimization: {
- minimize: true,
- minimizer: [
- new TerserPlugin({
- test: new RegExp(`\\.min\\${suffix}$`),
-
- // Do not bundle with comments.
- // See https://webpack.js.org/plugins/terser-webpack-plugin/#remove-comments for more information.
- terserOptions: {
- output: {
- comments: false,
- },
- },
- extractComments: false,
- }),
- ],
+export default (env, argv) => {
+ const isProd = argv.mode === "production";
+
+ return {
+ mode: isProd ? "production" : "development",
+ devtool: false, // No source maps for smaller file size
+ entry: {
+ transformers: "./src/transformers.js",
+ },
+ output: {
+ filename: isProd ? "transformers.min.js" : "transformers.dev.js", // Different filenames
+ path: path.join(__dirname, "dist"),
+ library: {
+ type: "module",
+ },
+ assetModuleFilename: "[name][ext]",
+ chunkFormat: false,
+ },
+ optimization: {
+ minimize: isProd,
+ minimizer: isProd
+ ? [
+ new TerserPlugin({
+ test: /\.js$/,
+ terserOptions: {
+ output: {
+ comments: false,
+ },
+ },
+ extractComments: false,
+ }),
+ ]
+ : [],
+ },
+ experiments: {
+ outputModule: true,
+ },
+ resolve: {
+ alias: {
+ "onnxruntime-web": false,
+ },
+ },
+ externals: {
+ "onnxruntime-web": isProd
+ ? "chrome://global/content/ml/ort.webgpu.mjs"
+ : "chrome://global/content/ml/ort.webgpu-dev.mjs",
},
- experiments: {
- outputModule,
+ module: {
+ parser: {
+ javascript: {
+ importMeta: false,
+ },
+ },
},
- resolve: { alias },
-
- externals: externalModules,
-
- // Development server
- devServer: {
- static: {
- directory: __dirname,
- },
- port: 8080,
- },
- plugins,
+ plugins: [new PostBuildPlugin()],
};
-
- if (outputModule) {
- config.module = {
- parser: {
- javascript: {
- importMeta: false,
- },
- },
- };
- } else {
- config.externalsType = "commonjs";
- }
-
- return config;
-}
-
-// Do not bundle onnxruntime-web when packaging for Node.js.
-// Instead, we use the native library (onnxruntime-node).
-const NODE_IGNORE_MODULES = ["onnxruntime-web"];
-
-// Do not bundle the following modules with webpack (mark as external)
-// 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,
- externalModules: NODE_EXTERNAL_MODULES,
- }),
-];
-
-// When running with `webpack serve`, only build the web target.
-const BUILDS = process.env.WEBPACK_SERVE
- ? [BUNDLE_BUILD]
- : [BUNDLE_BUILD, WEB_BUILD, ...NODE_BUILDS];
-export default BUILDS;
+};
|