From 8eaa4c33e206991ba09a1d7d5138fee416c1df33 Mon Sep 17 00:00:00 2001 From: Yulong Wang Date: Mon, 3 May 2021 18:31:55 -0700 Subject: [PATCH] [js] fix library bundling and some trivial improvement (#7550) * [js] fix library bundling * fix filename in code comments --- js/common/README.md | 2 +- js/common/package.json | 11 ++- js/node/README.md | 2 +- js/node/package.json | 35 +++++---- js/web/README.md | 2 +- js/web/karma.conf.js | 2 +- js/web/package.json | 39 ++++++---- js/web/script/test-runner-cli.ts | 19 +++-- js/web/webpack.config.js | 129 +++++++++++++++++-------------- 9 files changed, 135 insertions(+), 106 deletions(-) diff --git a/js/common/README.md b/js/common/README.md index acbf790984..2f79561c32 100644 --- a/js/common/README.md +++ b/js/common/README.md @@ -10,4 +10,4 @@ This package (onnxruntime-common) is not designed for using directly. Please con ## License -License information can be found [here](../../README.md#license). +License information can be found [here](https://github.com/microsoft/onnxruntime/blob/master/README.md#license). diff --git a/js/common/package.json b/js/common/package.json index 8c46d58375..182fe5614c 100644 --- a/js/common/package.json +++ b/js/common/package.json @@ -1,12 +1,18 @@ { "license": "MIT", "name": "onnxruntime-common", + "description": "ONNXRuntime JavaScript API library", "repository": { "url": "https://github.com/Microsoft/onnxruntime.git", "type": "git" }, "author": "fs-eire", "version": "1.7.0", + "keywords": [ + "ONNX", + "ONNXRuntime", + "ONNX Runtime" + ], "scripts": { "prepare": "tsc && webpack" }, @@ -17,9 +23,8 @@ "webpack-cli": "^4.6.0" }, "main": "dist/ort-common.node.js", + "types": "./types/index.d.ts", "jsdelivr": "dist/ort-common.min.js", "unpkg": "dist/ort-common.min.js", - "module": "./lib/index.js", - "types": "./types/index.d.ts", - "description": "ONNXRuntime JavaScript library" + "module": "./lib/index.js" } diff --git a/js/node/README.md b/js/node/README.md index 62fc8b9d24..c5bec8043f 100644 --- a/js/node/README.md +++ b/js/node/README.md @@ -26,4 +26,4 @@ To use on platforms without pre-built binaries, you can build Node.js binding fr ## License -License information can be found [here](../../README.md#license). +License information can be found [here](https://github.com/microsoft/onnxruntime/blob/master/README.md#license). diff --git a/js/node/package.json b/js/node/package.json index 9e7d766cfb..4a64a8e8b4 100644 --- a/js/node/package.json +++ b/js/node/package.json @@ -1,23 +1,18 @@ { - "binary": { - "module_path": "./bin", - "host": "https://onnxruntimetestdata.blob.core.windows.net/onnxruntime-node-prebuild/", - "napi_versions": [ - 3 - ] - }, "license": "MIT", "name": "onnxruntime-node", + "description": "ONNXRuntime Node.js binding", "repository": { "url": "https://github.com/Microsoft/onnxruntime.git", "type": "git" }, "author": "fs-eire", "version": "1.7.0", - "dependencies": { - "onnxruntime-common": "file:../common", - "prebuild-install": "^6.1.2" - }, + "keywords": [ + "ONNX", + "ONNXRuntime", + "ONNX Runtime" + ], "scripts": { "build": "tsc && node ./script/build", "buildd": "tsc && node ./script/build --config=Debug", @@ -30,10 +25,10 @@ "test": "mocha ./test/test-main", "prepack": "node ./script/prepack" }, - "keywords": [ - "ONNX", - "ONNX Runtime" - ], + "dependencies": { + "onnxruntime-common": "file:../common", + "prebuild-install": "^6.1.2" + }, "devDependencies": { "@types/fs-extra": "^9.0.6", "@types/klaw-sync": "^6.0.0", @@ -56,11 +51,17 @@ "typescript": "^4.1.3" }, "main": "./lib/index.js", + "types": "./types/lib/index.d.ts", "os": [ "win32", "darwin", "linux" ], - "types": "./types/lib/index.d.ts", - "description": "ONNXRuntime Node.js binding" + "binary": { + "module_path": "./bin", + "host": "https://onnxruntimetestdata.blob.core.windows.net/onnxruntime-node-prebuild/", + "napi_versions": [ + 3 + ] + } } diff --git a/js/web/README.md b/js/web/README.md index bbce06df95..40d8fab5df 100644 --- a/js/web/README.md +++ b/js/web/README.md @@ -152,4 +152,4 @@ ONNX Runtime Web currently supports most operators in [ai.onnx](https://github.c ## License -License information can be found [here](../../README.md#license). +License information can be found [here](https://github.com/microsoft/onnxruntime/blob/master/README.md#license). diff --git a/js/web/karma.conf.js b/js/web/karma.conf.js index 21fd0edcb7..eaa07256e8 100644 --- a/js/web/karma.conf.js +++ b/js/web/karma.conf.js @@ -1,5 +1,5 @@ // Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT license. +// Licensed under the MIT License. const bundleMode = require('minimist')(process.argv)['bundle-mode'] || 'dev'; // 'dev'|'perf'|undefined; const karmaPlugins = require('minimist')(process.argv)['karma-plugins'] || undefined; diff --git a/js/web/package.json b/js/web/package.json index e7d683cf58..d5e83803b7 100644 --- a/js/web/package.json +++ b/js/web/package.json @@ -1,25 +1,24 @@ { + "license": "MIT", "name": "onnxruntime-web", - "version": "1.7.0", "description": "A Javascript library for running ONNX models on browsers", - "main": "./lib/index.js", - "browser": "./dist/ort-web.min.js", - "types": "./types/lib/index.d.ts", + "repository": { + "url": "https://github.com/Microsoft/onnxruntime.git", + "type": "git" + }, + "author": "fs-eire", + "version": "1.7.0", + "keywords": [ + "ONNX", + "ONNXRuntime", + "ONNX Runtime" + ], "scripts": { - "prepare": "tsc && node ./script/prepare-test-data", + "prepare": "tsc", "build": "node ./script/build", - "test": "node ./script/test-runner-cli", + "test": "node ./script/prepare-test-data && node ./script/test-runner-cli", "prepack": "node ./script/prepack" }, - "repository": { - "type": "git", - "url": "https://github.com/Microsoft/onnxruntime.git" - }, - "keywords": [ - "ONNX Runtime Web" - ], - "author": "fs-eire", - "license": "MIT", "dependencies": { "flatbuffers": "^1.12.0", "guid-typescript": "^1.0.9", @@ -67,5 +66,15 @@ "typescript": "^4.2.3", "webpack": "^5.30.0", "webpack-cli": "^4.6.0" + }, + "main": "dist/ort-web.node.js", + "types": "./types/lib/index.d.ts", + "jsdelivr": "dist/ort.min.js", + "unpkg": "dist/ort.min.js", + "module": "./lib/index.js", + "browser": { + "fs": false, + "path": false, + "util": false } } diff --git a/js/web/script/test-runner-cli.ts b/js/web/script/test-runner-cli.ts index c018c6fea3..a23c6c16bf 100644 --- a/js/web/script/test-runner-cli.ts +++ b/js/web/script/test-runner-cli.ts @@ -32,14 +32,18 @@ const TEST_DATA_OP_ROOT = path.join(TEST_ROOT, 'data', 'ops'); const TEST_DATA_BASE = args.env === 'node' ? TEST_ROOT : '/base/test/'; -npmlog.verbose('TestRunnerCli.Init', 'Loading whitelist...'); +let whitelist: Test.WhiteList; +const shouldLoadSuiteTestData = (args.mode === 'suite0'); +if (shouldLoadSuiteTestData) { + npmlog.verbose('TestRunnerCli.Init', 'Loading whitelist...'); -// The following is a whitelist of unittests for already implemented operators. -// Modify this list to control what node tests to run. -const jsonWithComments = fs.readFileSync(path.resolve(TEST_ROOT, './test-suite-whitelist.jsonc')).toString(); -const json = stripJsonComments(jsonWithComments, {whitespace: true}); -const whitelist = JSON.parse(json) as Test.WhiteList; -npmlog.verbose('TestRunnerCli.Init', 'Loading whitelist... DONE'); + // The following is a whitelist of unittests for already implemented operators. + // Modify this list to control what node tests to run. + const jsonWithComments = fs.readFileSync(path.resolve(TEST_ROOT, './test-suite-whitelist.jsonc')).toString(); + const json = stripJsonComments(jsonWithComments, {whitespace: true}); + whitelist = JSON.parse(json) as Test.WhiteList; + npmlog.verbose('TestRunnerCli.Init', 'Loading whitelist... DONE'); +} // The default backends and opset version lists. Those will be used in suite tests. const DEFAULT_BACKENDS: readonly TestRunnerCliArgs.Backend[] = @@ -55,7 +59,6 @@ const nodeTests = new Map(); const onnxTests = new Map(); const opTests = new Map(); -const shouldLoadSuiteTestData = (args.mode === 'suite0'); if (shouldLoadSuiteTestData) { npmlog.verbose('TestRunnerCli.Init', 'Loading test groups for suite test...'); diff --git a/js/web/webpack.config.js b/js/web/webpack.config.js index a211917d7b..0851cee8cf 100644 --- a/js/web/webpack.config.js +++ b/js/web/webpack.config.js @@ -1,90 +1,85 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + const path = require('path'); const webpack = require('webpack'); const NodePolyfillPlugin = require('node-polyfill-webpack-plugin'); const minimist = require('minimist'); -function buildAllConfig({ - suffix = '', - format = 'umd', - target = 'ES2017', - mode = 'production', - devtool = 'source-map' -}) { +// common config for release bundle +function buildConfig({ filename, format, target, mode, devtool }) { return { entry: path.resolve(__dirname, 'lib/index.ts'), output: { path: path.resolve(__dirname, 'dist'), - filename: `ort${suffix}.js`, + filename, library: { - name: 'ort', type: format } }, - externals: { - 'fs': 'fs', - 'path': 'path', - 'util': 'util', - }, resolve: { extensions: ['.ts', '.js'] }, plugins: [new webpack.WatchIgnorePlugin({ paths: [/\.js$/, /\.d\.ts$/] })], module: { rules: [{ - test: /\.tsx?$/, + test: /\.ts$/, use: [ { loader: 'ts-loader', options: { - compilerOptions: { target: target } + compilerOptions: { target } } } ] }] }, - mode: mode, - devtool: devtool, + mode, + devtool }; } -function buildWebConfig({ +// "ort{.min}.js" config +function buildOrtConfig({ suffix = '', - format = 'umd', - target = 'ES2017', + target = 'es5', mode = 'production', devtool = 'source-map' }) { - return { - entry: path.resolve(__dirname, 'lib/index.ts'), - output: { - path: path.resolve(__dirname, 'dist'), - filename: `ort-web${suffix}.js`, - library: { - type: format - } - }, - externals: { - 'onnxruntime-common': 'ort', - 'fs': 'fs', - 'path': 'path', - 'util': 'util', - }, - resolve: { extensions: ['.ts', '.js'] }, - plugins: [new webpack.WatchIgnorePlugin({ paths: [/\.js$/, /\.d\.ts$/] })], - module: { - rules: [{ - test: /\.tsx?$/, - use: [ - { - loader: 'ts-loader', - options: { - compilerOptions: { target: target } - } - } - ] - }] - }, - mode: mode, - devtool: devtool, + const config = buildConfig({ filename: `ort${suffix}.js`, format: 'umd', target, mode, devtool }); + // set global name 'ort' + config.output.library.name = 'ort'; + // do not use those node builtin modules in browser + config.resolve.fallback = { path: false, fs: false, util: false }; + return config; +} + +// "ort-web{.min|.node}.js" config +function buildOrtWebConfig({ + suffix = '', + format = 'umd', + target = 'es5', + mode = 'production', + devtool = 'source-map' +}) { + const config = buildConfig({ filename: `ort-web${suffix}.js`, format, target, mode, devtool }); + // exclude onnxruntime-common from bundle + config.externals = { + 'onnxruntime-common': { + commonjs: "onnxruntime-common", + commonjs2: "onnxruntime-common", + root: 'ort' + } }; + // in nodejs, treat as external dependencies + if (format === 'commonjs') { + config.externals.path = 'path'; + config.externals.fs = 'fs'; + config.externals.util = 'util'; + } + // in browser, do not use those node builtin modules + if (format === 'umd') { + config.resolve.fallback = { path: false, fs: false, util: false }; + } + return config; } function buildTestRunnerConfig({ @@ -108,14 +103,14 @@ function buildTestRunnerConfig({ 'onnxruntime-common': 'ort', 'fs': 'fs', }, - resolve: { extensions: ['.ts', '.js'] }, + resolve: { extensions: ['.ts', '.js'], aliasFields: [] }, plugins: [ new webpack.WatchIgnorePlugin({ paths: [/\.js$/, /\.d\.ts$/] }), new NodePolyfillPlugin() ], module: { rules: [{ - test: /\.tsx?$/, + test: /\.ts$/, use: [ { loader: 'ts-loader', @@ -139,10 +134,26 @@ module.exports = () => { if (bundleMode === 'prod') { builds.push( - buildAllConfig({ suffix: '.min', target: 'es5' }), - buildWebConfig({ suffix: '.min', target: 'es5' }), - buildAllConfig({ mode: 'development', devtool: 'inline-source-map', target: 'es5' }), - buildWebConfig({ mode: 'development', devtool: 'inline-source-map', target: 'es5' }), + // ort.min.js + buildOrtConfig({ suffix: '.min' }), + // ort.js + buildOrtConfig({ mode: 'development', devtool: 'inline-source-map' }), + // ort.es6.min.js + buildOrtConfig({ suffix: '.es6.min', target: 'es6' }), + // ort.es6.js + buildOrtConfig({ suffix: '.es6', mode: 'development', devtool: 'inline-source-map', target: 'es6' }), + + // ort-web.min.js + buildOrtWebConfig({ suffix: '.min' }), + // ort-web.js + buildOrtWebConfig({ mode: 'development', devtool: 'inline-source-map' }), + // ort-web.es6.min.js + buildOrtWebConfig({ suffix: '.es6.min', target: 'es6' }), + // ort-web.es6.js + buildOrtWebConfig({ suffix: '.es6', mode: 'development', devtool: 'inline-source-map', target: 'es6' }), + + // ort-web.node.js + buildOrtWebConfig({ suffix: '.node', format: 'commonjs' }), ); }