[js/web] skip default locateFile() when dynamic import is disabled (#21073)

### Description
skip default `locateFile()` when dynamic import is disabled. This allows
the file to work with bundlers to load WebAssembly file correctly if
`env.wasm.wasmPaths` is not set.
This commit is contained in:
Yulong Wang 2024-06-18 12:21:45 -07:00 committed by GitHub
parent b75b2fcdcb
commit 631a2c16be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 5 deletions

View file

@ -54,9 +54,11 @@ export const initializeFlags = (): void => {
}
}
// overwrite wasm paths override if not set
if (env.wasm.wasmPaths === undefined && scriptSrc && scriptSrc.indexOf('blob:') !== 0) {
env.wasm.wasmPaths = scriptSrc.substring(0, scriptSrc.lastIndexOf('/') + 1);
if (!BUILD_DEFS.DISABLE_DYNAMIC_IMPORT) {
// overwrite wasm paths override if not set
if (env.wasm.wasmPaths === undefined && scriptSrc && scriptSrc.indexOf('blob:') !== 0) {
env.wasm.wasmPaths = scriptSrc.substring(0, scriptSrc.lastIndexOf('/') + 1);
}
}
};

View file

@ -133,13 +133,17 @@ export const initializeWebAssembly = async(flags: Env.WebAssemblyFlags): Promise
* created.
*/
numThreads,
};
if (wasmPathOverride || wasmPrefixOverride) {
/**
* A callback function to locate the WebAssembly file. The function should return the full path of the file.
*
* Since Emscripten 3.1.58, this function is only called for the .wasm file.
*/
locateFile: (fileName, scriptDirectory) => wasmPathOverride ?? (wasmPrefixOverride ?? scriptDirectory) + fileName
};
config.locateFile = (fileName, scriptDirectory) =>
wasmPathOverride ?? (wasmPrefixOverride ?? scriptDirectory) + fileName;
}
ortWasmFactory(config).then(
// wasm module initialized successfully