Skip to content

Commit

Permalink
[js/common] allow import onnxruntime-common as ESM and CJS (microsoft…
Browse files Browse the repository at this point in the history
…#15772)

### Description
allow import onnxruntime-common as ESM and CJS.
  • Loading branch information
fs-eire committed Jun 12, 2023
1 parent 0a585c0 commit b678ba1
Show file tree
Hide file tree
Showing 24 changed files with 222 additions and 127 deletions.
1 change: 1 addition & 0 deletions common/.npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/.vscode/

build.js
webpack.config.js
typedoc.json
tsconfig.json
Expand Down
26 changes: 26 additions & 0 deletions common/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import {execSync} from 'node:child_process';
import {writeFileSync} from 'node:fs';
import {resolve, dirname} from 'node:path';
import {fileURLToPath} from 'node:url';

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

// build the following folders:
// - dist/cjs
// - dist/esm
execSync('npm run build:cjs', {shell: true, stdio: 'inherit', cwd: __dirname});
execSync('npm run build:esm', {shell: true, stdio: 'inherit', cwd: __dirname});

// generate package.json files under each of the dist folders for commonJS and ESModule
// this trick allows typescript to import this package as different module type
// see also: https://evertpot.com/universal-commonjs-esm-typescript-packages/
writeFileSync(resolve(__dirname, './dist/cjs', 'package.json'), '{"type": "commonjs"}');
writeFileSync(resolve(__dirname, './dist/esm', 'package.json'), '{"type": "module"}');

// launch webpack to generate bundles
execSync('npm run build:bundles', {shell: true, stdio: 'inherit', cwd: __dirname});
2 changes: 1 addition & 1 deletion common/lib/backend-impl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {Backend} from './backend';
import {Backend} from './backend.js';

interface BackendInfo {
backend: Backend;
Expand Down
6 changes: 3 additions & 3 deletions common/lib/backend.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {InferenceSession} from './inference-session';
import {OnnxValue} from './onnx-value';
import {InferenceSession} from './inference-session.js';
import {OnnxValue} from './onnx-value.js';

/**
* @internal
Expand Down Expand Up @@ -46,4 +46,4 @@ export interface Backend {
Promise<SessionHandler>;
}

export {registerBackend} from './backend-impl';
export {registerBackend} from './backend-impl.js';
4 changes: 2 additions & 2 deletions common/lib/env-impl.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {Env} from './env';
import {version} from './version';
import {Env} from './env.js';
import {version} from './version.js';

type LogLevelType = Env['logLevel'];

Expand Down
2 changes: 1 addition & 1 deletion common/lib/env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {env as envImpl} from './env-impl';
import {env as envImpl} from './env-impl.js';

export declare namespace Env {
export type WasmPrefixOrFilePaths = string|{
Expand Down
10 changes: 5 additions & 5 deletions common/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* @packageDocumentation
*/

export * from './backend';
export * from './env';
export * from './inference-session';
export * from './tensor';
export * from './onnx-value';
export * from './backend.js';
export * from './env.js';
export * from './inference-session.js';
export * from './tensor.js';
export * from './onnx-value.js';
10 changes: 5 additions & 5 deletions common/lib/inference-session-impl.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {SessionHandler} from './backend';
import {resolveBackend} from './backend-impl';
import {InferenceSession as InferenceSessionInterface} from './inference-session';
import {OnnxValue} from './onnx-value';
import {Tensor} from './tensor';
import {resolveBackend} from './backend-impl.js';
import {SessionHandler} from './backend.js';
import {InferenceSession as InferenceSessionInterface} from './inference-session.js';
import {OnnxValue} from './onnx-value.js';
import {Tensor} from './tensor.js';

type SessionOptions = InferenceSessionInterface.SessionOptions;
type RunOptions = InferenceSessionInterface.RunOptions;
Expand Down
4 changes: 2 additions & 2 deletions common/lib/inference-session.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {InferenceSession as InferenceSessionImpl} from './inference-session-impl';
import {OnnxValue} from './onnx-value';
import {InferenceSession as InferenceSessionImpl} from './inference-session-impl.js';
import {OnnxValue} from './onnx-value.js';

/* eslint-disable @typescript-eslint/no-redeclare */

Expand Down
2 changes: 1 addition & 1 deletion common/lib/onnx-value.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {Tensor} from './tensor';
import {Tensor} from './tensor.js';

type NonTensorType = never;

Expand Down
4 changes: 2 additions & 2 deletions common/lib/tensor-conversion-impl.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {Tensor} from './tensor';
import {TensorToDataUrlOptions, TensorToImageDataOptions} from './tensor-conversion';
import {TensorToDataUrlOptions, TensorToImageDataOptions} from './tensor-conversion.js';
import {Tensor} from './tensor.js';

/**
* implementation of Tensor.toDataURL()
Expand Down
2 changes: 1 addition & 1 deletion common/lib/tensor-conversion.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {OptionsFormat, OptionsNormalizationParameters, OptionsTensorLayout} from './tensor-factory';
import {OptionsFormat, OptionsNormalizationParameters, OptionsTensorLayout} from './tensor-factory.js';

export interface TensorToDataUrlOptions extends OptionsTensorLayout, OptionsFormat, OptionsNormalizationParameters {}

Expand Down
4 changes: 2 additions & 2 deletions common/lib/tensor-factory-impl.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {Tensor, TypedTensor} from './tensor';
import {OptionsDimensions, OptionsFormat, OptionsNormalizationParameters, OptionsTensorFormat, OptionsTensorLayout, TensorFromImageBitmapOptions, TensorFromImageDataOptions, TensorFromImageElementOptions, TensorFromUrlOptions} from './tensor-factory';
import {OptionsDimensions, OptionsFormat, OptionsNormalizationParameters, OptionsTensorFormat, OptionsTensorLayout, TensorFromImageBitmapOptions, TensorFromImageDataOptions, TensorFromImageElementOptions, TensorFromUrlOptions} from './tensor-factory.js';
import {Tensor, TypedTensor} from './tensor.js';

interface BufferToTensorOptions extends OptionsDimensions, OptionsTensorLayout, OptionsNormalizationParameters,
OptionsFormat, OptionsTensorFormat {}
Expand Down
2 changes: 1 addition & 1 deletion common/lib/tensor-factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {TypedTensor} from './tensor';
import {TypedTensor} from './tensor.js';

export type ImageFormat = 'RGB'|'RGBA'|'BGR'|'RBG';
export type ImageTensorLayout = 'NHWC'|'NCHW';
Expand Down
12 changes: 6 additions & 6 deletions common/lib/tensor-impl.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {Tensor as TensorInterface} from './tensor';
import {TensorToDataUrlOptions, TensorToImageDataOptions} from './tensor-conversion';
import {tensorToDataURL, tensorToImageData} from './tensor-conversion-impl';
import {TensorFromImageBitmapOptions, TensorFromImageDataOptions, TensorFromImageElementOptions, TensorFromUrlOptions} from './tensor-factory';
import {tensorFromImage} from './tensor-factory-impl';
import {calculateSize, tensorReshape} from './tensor-utils-impl';
import {tensorToDataURL, tensorToImageData} from './tensor-conversion-impl.js';
import {TensorToDataUrlOptions, TensorToImageDataOptions} from './tensor-conversion.js';
import {tensorFromImage} from './tensor-factory-impl.js';
import {TensorFromImageBitmapOptions, TensorFromImageDataOptions, TensorFromImageElementOptions, TensorFromUrlOptions} from './tensor-factory.js';
import {calculateSize, tensorReshape} from './tensor-utils-impl.js';
import {Tensor as TensorInterface} from './tensor.js';

type TensorType = TensorInterface.Type;
type TensorDataType = TensorInterface.DataType;
Expand Down
2 changes: 1 addition & 1 deletion common/lib/tensor-utils-impl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {Tensor} from './tensor';
import {Tensor} from './tensor.js';

/**
* calculate size from dims.
Expand Down
4 changes: 2 additions & 2 deletions common/lib/tensor-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {Tensor, TypedTensor} from './tensor';
import {ConversionUtils} from './tensor-conversion';
import {ConversionUtils} from './tensor-conversion.js';
import {Tensor, TypedTensor} from './tensor.js';

interface Properties {
/**
Expand Down
6 changes: 3 additions & 3 deletions common/lib/tensor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {TensorFactory} from './tensor-factory';
import {Tensor as TensorImpl} from './tensor-impl';
import {TypedTensorUtils} from './tensor-utils';
import {TensorFactory} from './tensor-factory.js';
import {Tensor as TensorImpl} from './tensor-impl.js';
import {TypedTensorUtils} from './tensor-utils.js';

/* eslint-disable @typescript-eslint/no-redeclare */

Expand Down
25 changes: 15 additions & 10 deletions common/package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
{
"license": "MIT",
"unpkg": "dist/ort-common.min.js",
"type": "module",
"name": "onnxruntime-common",
"version": "1.16.0",
"repository": {
"url": "https://github.com/Microsoft/onnxruntime.git",
"type": "git"
},
"author": "fs-eire",
"module": "dist/lib/index.js",
"version": "1.16.0",
"jsdelivr": "dist/ort-common.min.js",
"scripts": {
"prepare": "tsc && webpack"
"build:cjs": "npx tsc --module commonjs --outDir ./dist/cjs",
"build:esm": "npx tsc",
"build:bundles": "webpack",
"build": "node ./build.js",
"prepare": "npm run build"
},
"devDependencies": {
"typedoc": "^0.23.22"
},
"main": "dist/cjs/index.js",
"exports": {
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js"
},
"keywords": [
"ONNX",
"ONNXRuntime",
"ONNX Runtime"
],
"devDependencies": {
"typedoc": "^0.23.22"
},
"main": "dist/ort-common.node.js",
"types": "dist/lib/index.d.ts",
"description": "ONNXRuntime JavaScript API library"
}
4 changes: 3 additions & 1 deletion common/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "dist/lib",
"outDir": "./dist/esm",
"declaration": true,
"declarationMap": true,
"esModuleInterop": false,
"noUnusedParameters": true,
},
Expand Down
Loading

0 comments on commit b678ba1

Please sign in to comment.