Source code

Revision control

Copy as Markdown

Other Tools

diff --git a/package.json b/package.json
index c7584c6..aca0441 100644
--- a/package.json
+++ b/package.json
@@ -30,7 +30,7 @@
"scripts": {
"format": "prettier --write .",
"format:check": "prettier --check .",
- "typegen": "tsc ./src/transformers.js --allowJs --declaration --emitDeclarationOnly --declarationMap --outDir types",
+ "typegen": "tsc",
"dev": "webpack serve --no-client-overlay",
"build": "webpack && npm run typegen",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --verbose",
@@ -62,6 +62,7 @@
"dependencies": {
"@huggingface/jinja": "^0.3.2",
+ "@types/node": "^22.5.5",
"onnxruntime-node": "1.20.1",
"onnxruntime-web": "1.20.1",
"sharp": "^0.33.5"
@@ -74,7 +75,7 @@
"jest-environment-node": "^30.0.0-alpha.6",
"jsdoc-to-markdown": "^8.0.1",
"prettier": "3.3.3",
- "typescript": "^5.2.2",
+ "typescript": "^5.6.2",
"wavefile": "^11.0.0",
"webpack": "^5.80.0",
"webpack-cli": "^5.0.2",
diff --git a/src/backends/onnx.js b/src/backends/onnx.js
index 26d771b..7731bfd 100644
--- a/src/backends/onnx.js
+++ b/src/backends/onnx.js
@@ -18,15 +18,8 @@
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';
-
-// Use subpath-imports to ensure Node.js and browser interoperability.
-// for more information.
-// @ts-ignore
import * as ONNX_WEB from '#onnxruntime-webgpu';
+const ONNX_NODE = null;
export { Tensor } from 'onnxruntime-common';
diff --git a/tsconfig.json b/tsconfig.json
new file mode 100644
index 0000000..8892b18
--- /dev/null
+++ b/tsconfig.json
@@ -0,0 +1,13 @@
+{
+ "files": [
+ "./src/transformers.js"
+ ],
+ "compilerOptions": {
+ "allowJs": true,
+ "declaration": true,
+ "emitDeclarationOnly": true,
+ "declarationMap": true,
+ "outDir": "types",
+ "types": [] // Or specify only the types you need
+ }
+}
diff --git a/webpack.config.js b/webpack.config.js
index 2182492..ef2540c 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -4,113 +4,106 @@ import path from "path";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
-/**
- * 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.
- * @returns {import('webpack').Configuration} One webpack target.
- */
-function buildConfig({
- name = "",
- suffix = ".js",
- type = "module", // 'module' | 'commonjs'
- ignoreModules = [],
- externalModules = [],
-} = {}) {
- const outputModule = type === "module";
-
- const alias = Object.fromEntries(
- ignoreModules.map((module) => {
- return [module, false];
- }),
- );
-
- /** @type {import('webpack').Configuration} */
- const config = {
+/** @type {import('webpack').Configuration[]} */
+const configs = [
+ {
mode: 'development',
devtool: 'source-map',
entry: {
- [`transformers${name}`]: './src/transformers.js',
- [`transformers${name}.min`]: './src/transformers.js',
+ 'transformers.min': './src/transformers.js',
},
output: {
- filename: `[name]${suffix}`,
+ filename: '[name].js',
path: path.join(__dirname, 'dist'),
library: {
- type,
+ type: 'module',
},
assetModuleFilename: '[name][ext]',
chunkFormat: 'module',
},
optimization: {
minimize: true,
- minimizer: [new TerserPlugin({
- test: new RegExp(`\\.min\\${suffix}$`),
- extractComments: false,
- })],
+ minimizer: [
+ new TerserPlugin({
+ test: /\.min\.js$/,
+ extractComments: false,
+ }),
+ ],
},
experiments: {
- outputModule,
+ outputModule: true,
+ },
+ resolve: {
+ alias: {
+ '#onnxruntime-webgpu': false,
+ },
+ },
+ externals: {
+ '#onnxruntime-webgpu': 'chrome://global/content/ml/ort.webgpu.mjs',
},
- resolve: { alias },
-
- externals: externalModules,
-
- // Development server
devServer: {
static: {
directory: __dirname,
},
port: 8080,
},
- };
-
- if (outputModule) {
- config.module = {
+ 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", "onnxruntime-web/webgpu"];
-
-// 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-node", "sharp", "fs", "path", "url"];
-
-
-export default [
- // Web-only build
- buildConfig({
- type: "module",
- }),
-
- // Node-compatible builds
- buildConfig({
- suffix: ".mjs",
- type: "module",
- ignoreModules: NODE_IGNORE_MODULES,
- externalModules: NODE_EXTERNAL_MODULES,
- }),
- buildConfig({
- suffix: ".cjs",
- type: "commonjs",
- ignoreModules: NODE_IGNORE_MODULES,
- externalModules: NODE_EXTERNAL_MODULES,
- }),
+ importMeta: false,
+ },
+ },
+ },
+ },
+ {
+ mode: 'development',
+ devtool: 'source-map',
+ entry: {
+ transformers: './src/transformers.js',
+ },
+ output: {
+ filename: '[name].js',
+ path: path.join(__dirname, 'dist'),
+ library: {
+ type: 'module',
+ },
+ assetModuleFilename: '[name][ext]',
+ chunkFormat: 'module',
+ },
+ optimization: {
+ minimize: true,
+ minimizer: [
+ new TerserPlugin({
+ test: /\.min\.js$/,
+ extractComments: false,
+ }),
+ ],
+ },
+ experiments: {
+ outputModule: true,
+ },
+ resolve: {
+ alias: {
+ '#onnxruntime-webgpu': false,
+ },
+ },
+ externals: {
+ '#onnxruntime-webgpu': 'chrome://global/content/ml/ort.webgpu-dev.mjs',
+ },
+ devServer: {
+ static: {
+ directory: __dirname,
+ },
+ port: 8080,
+ },
+ module: {
+ parser: {
+ javascript: {
+ importMeta: false,
+ },
+ },
+ },
+ },
];
+
+export default configs;