Skip to content

Commit

Permalink
fix E2E
Browse files Browse the repository at this point in the history
  • Loading branch information
fs-eire committed Jan 10, 2025
1 parent 60d2f44 commit d924833
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
55 changes: 55 additions & 0 deletions cmake/onnxruntime_webassembly.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,59 @@ jsepDownload:_pp_")
endif()

set_target_properties(onnxruntime_webassembly PROPERTIES OUTPUT_NAME ${target_name} SUFFIX ".mjs")

#
# The following POST_BUILD script is a workaround for enabling:
# - using onnxruntime-web with Multi-threading enabled when import from CDN
# - using onnxruntime-web when consumed in some frameworks like Vite
#
# In the use case mentioned above, the file name of the script may be changed. So we need to replace the line:
# `new Worker(new URL("ort-wasm-*.mjs", import.meta.url),`
# with
# `new Worker(new URL(import.meta.url),`
#
# This behavior is introduced in https://github.com/emscripten-core/emscripten/pull/22165. Since it's unlikely to be
# reverted, and there is no config to disable this behavior, we have to use a post-build script to workaround it.
#

# Generate a script to do the post-build work
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/wasm_post_build.js "
const fs = require('fs');
const path = require('path');
// node wasm_post_build.js <mjsFilePath>
const mjsFilePath = process.argv[2];
let contents = fs.readFileSync(mjsFilePath).toString();
const regex = 'new Worker\\\\(new URL\\\\(\".+?\", ?import\\\\.meta\\\\.url\\\\),';
const matches = [...contents.matchAll(new RegExp(regex, 'g'))];
if (matches.length !== 1) {
throw new Error(
`Unexpected number of matches for \"${regex}\" in \"${filepath}\": ${matches.length}.`,
);
}
// Replace the only occurrence.
contents = contents.replace(
new RegExp(regex),
`new Worker(new URL(import.meta.url),`,
);
fs.writeFileSync(mjsFilePath, contents);
"
)

find_program(NODE_EXECUTABLE node required)
if (NOT NODE_EXECUTABLE)
message(FATAL_ERROR "Node is required to run the post-build script")
endif()

add_custom_command(
TARGET onnxruntime_webassembly
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "Backup file at $<TARGET_FILE_NAME:onnxruntime_webassembly>.bak"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<TARGET_FILE_NAME:onnxruntime_webassembly>" "$<TARGET_FILE_NAME:onnxruntime_webassembly>.bak"
COMMAND ${CMAKE_COMMAND} -E echo "Performing workaround for $<TARGET_FILE_NAME:onnxruntime_webassembly>"
COMMAND ${NODE_EXECUTABLE} "${CMAKE_CURRENT_BINARY_DIR}/wasm_post_build.js" "$<TARGET_FILE_NAME:onnxruntime_webassembly>"
)
endif()
22 changes: 7 additions & 15 deletions js/web/script/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,9 @@ async function minifyWasmModuleJsForBrowser(filepath: string): Promise<string> {

let contents = await fs.readFile(filepath, { encoding: 'utf-8' });

const fileBaseName = path.basename(filepath);
// Replace the following line to create worker:
// ```
// new Worker(new URL({{{fileBaseName}}}, import.meta.url), ...
// new Worker(new URL(import.meta.url), ...
// ```
// with:
// ```
Expand All @@ -129,27 +128,20 @@ async function minifyWasmModuleJsForBrowser(filepath: string): Promise<string> {
// : new URL(import.meta.url), ...
// ```
//
// This change is required because of the following reasons:
// 1. When bundling the JS code, the original file is no longer available. We should use `import.meta.url` to get
// the path of the bundle file.
// 2. some bundlers that does not support runtime `import.meta.url`. In this case, we should use
// `new URL(BUILD_DEFS.BUNDLE_FILENAME, import.meta.url)`.
// NOTE: this is a workaround for some bundlers that does not support runtime import.meta.url.
// TODO: in emscripten 3.1.61+, need to update this code.

// First, check if there is exactly one occurrence of "new Worker(new URL({{{fileBaseName}}}, import.meta.url)".
//
// /new Worker\(new URL\("ort-wasm-simd-threaded\.jsep\.mjs", ?import\.meta\.url\),/
const regex = `new Worker\\(new URL\\("${fileBaseName.replace(/\./g, '\\.')}", ?import\\.meta\\.url\\),`;

const matches = [...contents.matchAll(new RegExp(regex, 'g'))];
// First, check if there is exactly one occurrence of "new Worker(new URL(import.meta.url)".
const matches = [...contents.matchAll(/new Worker\(new URL\(import\.meta\.url\),/g)];
if (matches.length !== 1) {
throw new Error(
`Unexpected number of matches for "new Worker(new URL("${fileBaseName}", import.meta.url)" in "${filepath}": ${matches.length}.`,
`Unexpected number of matches for "new Worker(new URL(import.meta.url)" in "${filepath}": ${matches.length}.`,
);
}

// Replace the only occurrence.
contents = contents.replace(
new RegExp(regex),
/new Worker\(new URL\(import\.meta\.url\),/,
`new Worker(import.meta.url.startsWith('file:')?new URL(BUILD_DEFS.BUNDLE_FILENAME, import.meta.url):new URL(import.meta.url),`,
);

Expand Down

0 comments on commit d924833

Please sign in to comment.