Skip to content

Commit a79c017

Browse files
committed
fix(onnxruntime): add EXPORT_ES6=0 patch and require shim for WASM build
- Patch cmake to add EXPORT_ES6=0 to prevent ES module format issues - Add require shim using createRequire in exported .mjs file - Update export to handle non-threaded WASM files (ort-wasm.wasm/mjs) - Fix variable name typo in cmake patching
1 parent f62b287 commit a79c017

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

packages/onnxruntime/scripts/build.mjs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,23 @@ async function cloneOnnxSource() {
122122
// When threading is disabled, BUILD_MLAS_NO_ONNXRUNTIME causes MLFloat16 errors.
123123
printStep('Patching onnxruntime_webassembly.cmake to fix MLFloat16 build...')
124124
const cmakePath = path.join(ONNX_SOURCE_DIR, 'cmake', 'onnxruntime_webassembly.cmake')
125-
const cmakeContent = await fs.readFile(cmakePath, 'utf-8')
126-
const updatedCmake = cmakeContent.replace(
125+
let cmakeContent = await fs.readFile(cmakePath, 'utf-8')
126+
127+
// Patch 1: Comment out BUILD_MLAS_NO_ONNXRUNTIME.
128+
cmakeContent = cmakeContent.replace(
127129
/add_compile_definitions\(\s*BUILD_MLAS_NO_ONNXRUNTIME\s*\)/,
128130
'# add_compile_definitions(\n # BUILD_MLAS_NO_ONNXRUNTIME\n # )'
129131
)
130-
await fs.writeFile(cmakePath, updatedCmake, 'utf-8')
131-
printSuccess('BUILD_MLAS_NO_ONNXRUNTIME commented out in cmake')
132+
133+
// Patch 2: Add EXPORT_ES6=0 to prevent ES module format.
134+
// This forces UMD/CommonJS output which we'll patch later.
135+
cmakeContent = cmakeContent.replace(
136+
/"SHELL:-s MODULARIZE=1"/,
137+
'"SHELL:-s MODULARIZE=1"\n "SHELL:-s EXPORT_ES6=0"'
138+
)
139+
140+
await fs.writeFile(cmakePath, cmakeContent, 'utf-8')
141+
printSuccess('BUILD_MLAS_NO_ONNXRUNTIME commented out and EXPORT_ES6=0 added')
132142

133143
// Clear CMake cache to ensure patch is picked up.
134144
printStep('Clearing CMake cache to force reconfiguration...')
@@ -179,7 +189,6 @@ async function build() {
179189
'--skip_tests',
180190
'--parallel',
181191
// '--enable_wasm_threads', // Commented out as fallback to get build working.
182-
'--cmake_extra_defines', 'onnxruntime_EMSCRIPTEN_SETTINGS=WASM_ASYNC_COMPILATION=0;EXPORT_ES6=1',
183192
], {
184193
cwd: ONNX_SOURCE_DIR,
185194
shell: WIN32,
@@ -204,28 +213,36 @@ async function exportWasm() {
204213
const platform = process.platform === 'darwin' ? 'Darwin' : 'Linux'
205214
const buildOutputDir = path.join(ONNX_SOURCE_DIR, 'build', platform, 'Release')
206215

207-
const wasmFile = path.join(buildOutputDir, 'ort-wasm-simd-threaded.wasm')
208-
const jsFile = path.join(buildOutputDir, 'ort-wasm-simd-threaded.js')
216+
// Look for non-threaded WASM files since we disabled threading.
217+
const wasmFile = path.join(buildOutputDir, 'ort-wasm.wasm')
218+
const mjsFile = path.join(buildOutputDir, 'ort-wasm.mjs')
209219

210220
if (!existsSync(wasmFile)) {
211221
printError('WASM file not found - build failed')
212222
printError(`Expected: ${wasmFile}`)
213223
throw new Error(`Required WASM file not found: ${wasmFile}`)
214224
}
215225

216-
const outputWasm = path.join(OUTPUT_DIR, 'ort-wasm-simd-threaded.wasm')
217-
const outputJs = path.join(OUTPUT_DIR, 'ort-wasm-simd-threaded.js')
226+
const outputWasm = path.join(OUTPUT_DIR, 'ort-wasm.wasm')
227+
const outputMjs = path.join(OUTPUT_DIR, 'ort-wasm.mjs')
218228

219229
// Copy WASM file.
220230
await fs.copyFile(wasmFile, outputWasm)
221231

222-
// Copy JS glue code and strip export statement if present.
223-
if (existsSync(jsFile)) {
224-
const jsContent = await fs.readFile(jsFile, 'utf-8')
225-
// Strip the export statement at the end of the file.
226-
const withoutExport = jsContent.replace(/;?\s*export\s+default\s+\w+\s*;\s*$/, '')
227-
await fs.writeFile(outputJs, withoutExport, 'utf-8')
228-
printStep(`JS: ${outputJs}`)
232+
// Copy and patch MJS glue code.
233+
if (existsSync(mjsFile)) {
234+
let mjsContent = await fs.readFile(mjsFile, 'utf-8')
235+
236+
// Add require shim at the top for Node.js CommonJS compatibility.
237+
// The generated code uses require() which isn't available in ES modules.
238+
const requireShim = `import { createRequire } from 'node:module';\nconst require = createRequire(import.meta.url);\n\n`
239+
mjsContent = requireShim + mjsContent
240+
241+
// Strip the export statement at the end of the file if present.
242+
mjsContent = mjsContent.replace(/;?\s*export\s+default\s+\w+\s*;\s*$/, '')
243+
244+
await fs.writeFile(outputMjs, mjsContent, 'utf-8')
245+
printStep(`MJS: ${outputMjs}`)
229246
}
230247

231248
const wasmSize = await getFileSize(outputWasm)

0 commit comments

Comments
 (0)