@@ -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 / a d d _ c o m p i l e _ d e f i n i t i o n s \( \s * B U I L D _ M L A S _ N O _ O N N X R U N T I M E \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+ / " S H E L L : - s M O D U L A R I Z E = 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 * e x p o r t \s + d e f a u l t \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 * e x p o r t \s + d e f a u l t \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