[js] upgrade dependencies and enable strict mode (#14930)

### Description
This PR includes the following changes:
- upgrade js dependencies
- enable STRICT mode for web assembly build.
- corresponding fix for cmake-js upgrade
- corresponsing fix for linter upgrade
- upgrade default typescript compile option of:
    - `moduleResolution`: from `node` to `node16`
    - `target`: from `es2017` to `es2020`
- fix ESM module import in commonJS source file

## change explanation

### changes to onnxruntime_webassembly.cmake
`-s WASM=1` and `-s LLD_REPORT_UNDEFINED` in latest version is
by-default and deprecated.

### changes to onnxruntime_node.cmake
The npm package `cmake-js` updated its way to find file `node.lib`.
previously it downloads this file from Node.js public release channel,
and now it generates it from a definition file.

The node.js release channel does not contain a windows/arm64 version, so
previously cmake-js will fail to download `node.lib` for that platform.
this is why we made special handling to download the unofficial binary
to build. now this is no longer needed so we removed that from the cmake
file.

### changes to tsconfig.json
`node16` module resolution supports async import and `es2020` as target
supports top level await.
This commit is contained in:
Yulong Wang 2023-03-22 15:05:04 -07:00 committed by GitHub
parent 71b67ec1e2
commit f972d21e81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 7844 additions and 16716 deletions

View file

@ -20,6 +20,10 @@ if (onnxruntime_ENABLE_EAGER_MODE)
endif()
if (onnxruntime_BUILD_WEBASSEMBLY)
string(APPEND CMAKE_C_FLAGS " -s STRICT=1 -s DEFAULT_TO_CXX=1")
string(APPEND CMAKE_CXX_FLAGS " -s STRICT=1 -s DEFAULT_TO_CXX=1")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s ALLOW_UNIMPLEMENTED_SYSCALLS=1")
# Enable LTO for release single-thread build
if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
# NOTES:

View file

@ -72,45 +72,12 @@ add_custom_target(js_common_npm_ci ALL
WORKING_DIRECTORY ${JS_COMMON_ROOT}
COMMENT "NPM install on /js/common")
if(WIN32 AND NODEJS_BINDING_ARCH STREQUAL "arm64")
# NOTE:
# Node.js for Windows ARM64 is not available in official release list.
# We uses node.lib for Node.js ARM64 v16.2.0 unofficial build from:
# https://unofficial-builds.nodejs.org/download/release/v16.2.0/win-arm64/
# The file should be downloaded and saved to ${CMAKE_CURRENT_BINARY_DIR}
#
# once Node.js for Windows ARM64 is in official release list and cmake-js supports Windows ARM64,
# we can remove this section.
include(FetchContent)
FetchContent_Declare(
nodejs_win_arm64_node_lib
URL https://unofficial-builds.nodejs.org/download/release/v16.2.0/win-arm64/node.lib
URL_HASH SHA256=decbf5c6f7e048af34add43da5eaa75b309340acc17b2670b8289a47dd751401
DOWNLOAD_NO_EXTRACT TRUE
)
FetchContent_MakeAvailable(nodejs_win_arm64_node_lib)
FetchContent_Declare(
nodejs_win_arm64_headers
URL https://unofficial-builds.nodejs.org/download/release/v16.2.0/node-v16.2.0-headers.tar.gz
URL_HASH SHA256=4f6a1e877c65ce31217b1c67f4fd90b9f9f4d3051e5ffabf151f43dd6edf7a34
)
FetchContent_MakeAvailable(nodejs_win_arm64_headers)
add_custom_target(nodejs_binding_wrapper ALL
COMMAND ${NPM_CLI} ci
COMMAND ${NPM_CLI} run build -- --onnxruntime-build-dir=${CMAKE_CURRENT_BINARY_DIR} --config=${CMAKE_BUILD_TYPE} --arch=${NODEJS_BINDING_ARCH}
WORKING_DIRECTORY ${JS_NODE_ROOT}
COMMENT "Using cmake-js to build OnnxRuntime Node.js binding")
add_custom_target(nodejs_binding_wrapper ALL
COMMAND ${NPM_CLI} ci
COMMAND ${CMAKE_COMMAND} -E remove_directory ${JS_NODE_ROOT}/build/
COMMAND ${CMAKE_COMMAND} -E make_directory ${JS_NODE_ROOT}/build/
COMMAND cd ${JS_NODE_ROOT} && ${CMAKE_COMMAND} ${JS_NODE_ROOT} --no-warn-unused-cli -G"Visual Studio 16 2019" -A"ARM64" -DCMAKE_RUNTIME_OUTPUT_DIRECTORY="${JS_NODE_ROOT}/build/" -DCMAKE_JS_INC="${nodejs_win_arm64_headers_SOURCE_DIR}/include/node" -DCMAKE_JS_SRC="${JS_NODE_ROOT}/node_modules/cmake-js/lib/cpp/win_delay_load_hook.cc" -DNODE_RUNTIME="node" -DNODE_RUNTIMEVERSION="16.2.0" -DNODE_ARCH="arm64" -DCMAKE_JS_LIB="${nodejs_win_arm64_node_lib_SOURCE_DIR}/node.lib" -Dnapi_build_version="3" -DCMAKE_BUILD_TYPE="RelWithDebInfo" -DONNXRUNTIME_BUILD_DIR="${CMAKE_CURRENT_BINARY_DIR}" -DCMAKE_SHARED_LINKER_FLAGS="/DELAYLOAD:NODE.EXE" -B ./build
COMMAND ${CMAKE_COMMAND} --build ${JS_NODE_ROOT}/build/ --config RelWithDebInfo
WORKING_DIRECTORY ${JS_NODE_ROOT}
COMMENT "Using custom script to build OnnxRuntime Node.js binding")
else()
add_custom_target(nodejs_binding_wrapper ALL
COMMAND ${NPM_CLI} ci
COMMAND ${NPM_CLI} run build -- --onnxruntime-build-dir=${CMAKE_CURRENT_BINARY_DIR} --config=${CMAKE_BUILD_TYPE} --arch=${NODEJS_BINDING_ARCH}
WORKING_DIRECTORY ${JS_NODE_ROOT}
COMMENT "Using cmake-js to build OnnxRuntime Node.js binding")
endif()
add_dependencies(js_common_npm_ci js_npm_ci)
add_dependencies(nodejs_binding_wrapper js_common_npm_ci)
add_dependencies(nodejs_binding_wrapper onnxruntime)

View file

@ -202,12 +202,10 @@ else()
-s \"EXPORTED_RUNTIME_METHODS=${EXPORTED_RUNTIME_METHODS}\" \
-s \"EXPORTED_FUNCTIONS=_malloc,_free\" \
-s MAXIMUM_MEMORY=4294967296 \
-s WASM=1 \
-s EXIT_RUNTIME=0 \
-s ALLOW_MEMORY_GROWTH=1 \
-s MODULARIZE=1 \
-s EXPORT_ALL=0 \
-s LLD_REPORT_UNDEFINED \
-s VERBOSE=0 \
-s FILESYSTEM=0 \
${WASM_API_EXCEPTION_CATCHING} \

View file

@ -2,12 +2,15 @@
// Licensed under the MIT License.
import {EnvImpl} from './env-impl';
export declare namespace Env {
export type WasmPrefixOrFilePaths = string|{
/* eslint-disable @typescript-eslint/naming-convention */
'ort-wasm.wasm'?: string;
'ort-wasm-threaded.wasm'?: string;
'ort-wasm-simd.wasm'?: string;
'ort-wasm-simd-threaded.wasm'?: string;
/* eslint-enable @typescript-eslint/naming-convention */
};
export interface WebAssemblyFlags {
/**

File diff suppressed because it is too large Load diff

View file

@ -19,11 +19,7 @@
"ONNX Runtime"
],
"devDependencies": {
"ts-loader": "^9.4.2",
"typedoc": "^0.23.22",
"typescript": "^4.9.4",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.1"
"typedoc": "^0.23.22"
},
"main": "dist/ort-common.node.js",
"types": "dist/lib/index.d.ts",

View file

@ -31,15 +31,20 @@ include_directories(${CMAKE_SOURCE_DIR}/../../include/onnxruntime/core/session)
include_directories(${CMAKE_SOURCE_DIR}/node_modules/node-addon-api)
# source files
file(GLOB SOURCE_FILES ${CMAKE_SOURCE_DIR}/src/*.cc)
file(GLOB ORT_NODEJS_BINDING_SOURCE_FILES ${CMAKE_SOURCE_DIR}/src/*.cc)
add_library(onnxruntime_binding SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
add_library(onnxruntime_binding SHARED ${ORT_NODEJS_BINDING_SOURCE_FILES} ${CMAKE_JS_SRC})
set_target_properties(onnxruntime_binding PROPERTIES
PREFIX "" SUFFIX ".node"
BUILD_WITH_INSTALL_RPATH TRUE
INSTALL_RPATH_USE_LINK_PATH FALSE)
target_link_libraries(onnxruntime_binding PRIVATE ${CMAKE_JS_LIB})
if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET)
# Generate node.lib
execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
endif()
# add libraries
if (WIN32)
target_link_directories(onnxruntime_binding PRIVATE ${ONNXRUNTIME_BUILD_DIR}/${CMAKE_BUILD_TYPE})

5640
js/node/package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -34,18 +34,14 @@
"ONNX Runtime"
],
"devDependencies": {
"@types/fs-extra": "^9.0.6",
"@types/minimist": "^1.2.2",
"@types/mocha": "^8.2.2",
"@types/node": "^14.14.37",
"cmake-js": "^6.2.1",
"fs-extra": "^9.1.0",
"@types/mocha": "^10.0.1",
"cmake-js": "^7.2.1",
"jsonc": "^2.0.0",
"minimist": "^1.2.7",
"minimist": "^1.2.8",
"mocha": "^10.2.0",
"node-addon-api": "^3.1.0",
"onnx-proto": "^4.0.4",
"typescript": "^4.9.4"
"node-addon-api": "^6.0.0",
"onnx-proto": "^8.0.1"
},
"main": "dist/index.js",
"os": [

View file

@ -59,8 +59,15 @@ if (os.platform() === 'darwin') {
}
}
// In Windows, "npx cmake-js configure" uses a powershell script to detect the Visual Studio installation.
// The script uses the environment variable LIB. If an invalid path is specified in LIB, the script will fail.
// So we override the LIB environment variable to remove invalid paths.
const envOverride = os.platform() === 'win32' && process.env.LIB ?
{...process.env, LIB: process.env.LIB.split(';').filter(fs.existsSync).join(';')} :
process.env;
// launch cmake-js configure
const procCmakejs = spawnSync('npx', args, {shell: true, stdio: 'inherit', cwd: ROOT_FOLDER});
const procCmakejs = spawnSync('npx', args, {shell: true, stdio: 'inherit', cwd: ROOT_FOLDER, env: envOverride});
if (procCmakejs.status !== 0) {
if (procCmakejs.error) {
console.error(procCmakejs.error);

View file

@ -264,7 +264,7 @@ function loadFiltersRegex(): Array<{opset?: RegExp | undefined; name: RegExp}> {
const filters: Array<string|[string, string]> = ['(FLOAT16)'];
filters.push(...BACKEND_TEST_SERIES_FILTERS.current_failing_tests);
if (process.arch === 'x32') {
if (process.arch === 'ia32') {
filters.push(...BACKEND_TEST_SERIES_FILTERS.current_failing_tests_x86);
}

6505
js/package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,22 +1,29 @@
{
"devDependencies": {
"@types/fs-extra": "^9.0.13",
"@types/node": "^18.11.18",
"@types/fs-extra": "^11.0.1",
"@types/node": "^18.14.6",
"@types/npmlog": "^4.1.4",
"@typescript-eslint/eslint-plugin": "^4.22.1",
"@typescript-eslint/parser": "^4.22.1",
"clang-format": "^1.5.0",
"@typescript-eslint/eslint-plugin": "^5.54.1",
"@typescript-eslint/parser": "^5.54.1",
"clang-format": "^1.8.0",
"dir-compare": "^4.0.0",
"eslint": "^7.25.0",
"eslint": "^8.35.0",
"eslint-plugin-header": "^3.1.1",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsdoc": "^33.1.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jsdoc": "^40.0.1",
"eslint-plugin-prefer-arrow": "^1.2.3",
"eslint-plugin-unicorn": "^31.0.0",
"fs-extra": "^10.1.0",
"jszip": "^3.10.0",
"npmlog": "^6.0.2",
"typescript": "^4.9.4"
"eslint-plugin-unicorn": "^46.0.0",
"fs-extra": "^11.1.0",
"jszip": "^3.10.1",
"node-polyfill-webpack-plugin": "^2.0.1",
"npmlog": "^7.0.1",
"terser": "^5.16.5",
"ts-loader": "^9.4.2",
"typescript": "^4.9.5",
"webpack": "^5.75.0",
"webpack-bundle-analyzer": "^4.8.0",
"webpack-cli": "^5.0.1",
"worker-loader": "^3.0.8"
},
"scripts": {
"lint": "eslint . --ext .ts --ext .tsx",

View file

@ -1,11 +1,11 @@
{
"compilerOptions": {
"module": "ES2015",
"moduleResolution": "node",
"moduleResolution": "node16",
"declaration": true,
"esModuleInterop": true,
"target": "ES2017",
"lib": ["ES2017", "ESNext.BigInt", "dom"],
"target": "ES2020",
"lib": ["ES2020", "ESNext.BigInt", "dom"],
"sourceMap": true,
"noUnusedLocals": true,
"noImplicitAny": true,

View file

@ -275,7 +275,7 @@ class Event implements Profiler.Event {
public category: Profiler.EventCategory, public name: string, public startTime: number,
private endCallback: (e: Event) => void|Promise<void>, public timer?: WebGLQuery, public ctx?: WebGLContext) {}
end() {
async end() {
return this.endCallback(this);
}

8471
js/web/package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -33,50 +33,36 @@
"ONNX Runtime"
],
"devDependencies": {
"@chiragrupani/karma-chromium-edge-launcher": "^2.1.0",
"@types/chai": "^4.2.16",
"@types/emscripten": "^1.39.4",
"@chiragrupani/karma-chromium-edge-launcher": "^2.2.2",
"@types/chai": "^4.3.4",
"@types/emscripten": "^1.39.6",
"@types/flatbuffers": "^1.10.0",
"@types/fs-extra": "^9.0.10",
"@types/karma": "^6.1.0",
"@types/long": "^4.0.1",
"@types/minimatch": "^3.0.4",
"@types/minimatch": "^5.1.2",
"@types/minimist": "^1.2.2",
"@types/mocha": "^8.2.2",
"@types/npmlog": "^4.1.2",
"@types/platform": "^1.3.3",
"@types/mocha": "^10.0.1",
"@types/platform": "^1.3.4",
"base64-js": "^1.5.1",
"chai": "^4.3.4",
"dir-compare": "^3.3.0",
"electron": "^18.3.7",
"fs-extra": "^9.1.0",
"globby": "^11.0.3",
"jszip": "^3.7.1",
"karma": "^6.3.2",
"chai": "^4.3.7",
"electron": "^23.1.2",
"globby": "^13.1.3",
"karma": "^6.4.1",
"karma-browserstack-launcher": "^1.6.0",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^3.1.0",
"karma-chrome-launcher": "^3.1.1",
"karma-edge-launcher": "^0.4.2",
"karma-electron": "^7.0.0",
"karma-firefox-launcher": "^2.1.0",
"karma-electron": "^7.3.0",
"karma-firefox-launcher": "^2.1.2",
"karma-mocha": "^2.0.1",
"karma-mocha-reporter": "^2.2.5",
"karma-safari-applescript-launcher": "^0.1.0",
"karma-sourcemap-loader": "^0.3.8",
"minimatch": "^3.0.4",
"minimist": "^1.2.7",
"karma-safari-applescript-launcher": "^0.1.1",
"karma-sourcemap-loader": "^0.4.0",
"minimatch": "^7.4.2",
"minimist": "^1.2.8",
"mocha": "^10.2.0",
"node-polyfill-webpack-plugin": "^1.1.0",
"npmlog": "^4.1.2",
"numpy-parser": "^1.2.3",
"strip-json-comments": "^3.1.1",
"terser": "^5.16.1",
"ts-loader": "^9.4.2",
"typescript": "^4.9.4",
"webpack": "^5.75.0",
"webpack-bundle-analyzer": "^4.7.0",
"webpack-cli": "^5.0.1",
"worker-loader": "^3.0.8"
"strip-json-comments": "^5.0.0"
},
"main": "dist/ort-web.node.js",
"types": "./types/lib/index.d.ts",

File diff suppressed because it is too large Load diff

View file

@ -5,7 +5,6 @@
const path = require('path');
const fs = require('fs-extra');
const globby = require('globby');
const { spawn } = require('child_process');
const startServer = require('./simple-http-server');
@ -32,29 +31,31 @@ function getNextUserDataDir() {
return dir;
}
// find packed package
const ORT_COMMON_FOLDER = path.resolve(JS_ROOT_FOLDER, 'common');
const ORT_COMMON_PACKED_FILEPATH_CANDIDATES = globby.sync('onnxruntime-common-*.tgz', { cwd: ORT_COMMON_FOLDER });
const PACKAGES_TO_INSTALL = [];
if (ORT_COMMON_PACKED_FILEPATH_CANDIDATES.length === 1) {
PACKAGES_TO_INSTALL.push(path.resolve(ORT_COMMON_FOLDER, ORT_COMMON_PACKED_FILEPATH_CANDIDATES[0]));
} else if (ORT_COMMON_PACKED_FILEPATH_CANDIDATES.length > 1) {
throw new Error('multiple packages found for onnxruntime-common.');
}
const ORT_WEB_FOLDER = path.resolve(JS_ROOT_FOLDER, 'web');
const ORT_WEB_PACKED_FILEPATH_CANDIDATES = globby.sync('onnxruntime-web-*.tgz', { cwd: ORT_WEB_FOLDER });
if (ORT_WEB_PACKED_FILEPATH_CANDIDATES.length !== 1) {
throw new Error('cannot find exactly single package for onnxruntime-web.');
}
PACKAGES_TO_INSTALL.push(path.resolve(ORT_WEB_FOLDER, ORT_WEB_PACKED_FILEPATH_CANDIDATES[0]));
// we start here:
async function main() {
// find packed package
const {globbySync} = await import('globby');
const ORT_COMMON_FOLDER = path.resolve(JS_ROOT_FOLDER, 'common');
const ORT_COMMON_PACKED_FILEPATH_CANDIDATES = globbySync('onnxruntime-common-*.tgz', { cwd: ORT_COMMON_FOLDER });
const PACKAGES_TO_INSTALL = [];
if (ORT_COMMON_PACKED_FILEPATH_CANDIDATES.length === 1) {
PACKAGES_TO_INSTALL.push(path.resolve(ORT_COMMON_FOLDER, ORT_COMMON_PACKED_FILEPATH_CANDIDATES[0]));
} else if (ORT_COMMON_PACKED_FILEPATH_CANDIDATES.length > 1) {
throw new Error('multiple packages found for onnxruntime-common.');
}
const ORT_WEB_FOLDER = path.resolve(JS_ROOT_FOLDER, 'web');
const ORT_WEB_PACKED_FILEPATH_CANDIDATES = globbySync('onnxruntime-web-*.tgz', { cwd: ORT_WEB_FOLDER });
if (ORT_WEB_PACKED_FILEPATH_CANDIDATES.length !== 1) {
throw new Error('cannot find exactly single package for onnxruntime-web.');
}
PACKAGES_TO_INSTALL.push(path.resolve(ORT_WEB_FOLDER, ORT_WEB_PACKED_FILEPATH_CANDIDATES[0]));
// we start here:
// install dev dependencies
await runInShell(`npm install"`);