From 02d94bcc8ed047ec4b1fa32c5f11f7abb728b7df Mon Sep 17 00:00:00 2001 From: Yulong Wang <7679871+fs-eire@users.noreply.github.com> Date: Tue, 9 May 2023 11:11:26 -0700 Subject: [PATCH] [js/web] fix terser reserved symbols for worker (#15864) ### Description due to change from https://github.com/emscripten-core/emscripten/commit/3935cdcc57409e05830be9603e8eb0b906809f40, our minimizer need to be updated to add "startWorker" to reserved symbol. --- js/web/script/build.ts | 38 ++++++++++++++++++++++++++++++++++++-- js/web/webpack.config.js | 3 ++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/js/web/script/build.ts b/js/web/script/build.ts index ebdce462b9..d3a5be429b 100644 --- a/js/web/script/build.ts +++ b/js/web/script/build.ts @@ -34,8 +34,11 @@ const ROOT_FOLDER = path.join(__dirname, '..'); const WASM_BINDING_FOLDER = path.join(ROOT_FOLDER, 'lib', 'wasm', 'binding'); const WASM_BINDING_JS_PATH = path.join(WASM_BINDING_FOLDER, 'ort-wasm.js'); const WASM_BINDING_THREADED_JS_PATH = path.join(WASM_BINDING_FOLDER, 'ort-wasm-threaded.js'); +const WASM_BINDING_SIMD_THREADED_JSEP_JS_PATH = path.join(WASM_BINDING_FOLDER, 'ort-wasm-simd-threaded.jsep.js'); const WASM_BINDING_THREADED_WORKER_JS_PATH = path.join(WASM_BINDING_FOLDER, 'ort-wasm-threaded.worker.js'); const WASM_BINDING_THREADED_MIN_JS_PATH = path.join(WASM_BINDING_FOLDER, 'ort-wasm-threaded.min.js'); +const WASM_BINDING_SIMD_THREADED_JSEP_MIN_JS_PATH = + path.join(WASM_BINDING_FOLDER, 'ort-wasm-simd-threaded.jsep.min.js'); const WASM_BINDING_THREADED_MIN_WORKER_JS_PATH = path.join(WASM_BINDING_FOLDER, 'ort-wasm-threaded.min.worker.js'); const WASM_DIST_FOLDER = path.join(ROOT_FOLDER, 'dist'); @@ -43,8 +46,11 @@ const WASM_WASM_PATH = path.join(WASM_DIST_FOLDER, 'ort-wasm.wasm'); const WASM_THREADED_WASM_PATH = path.join(WASM_DIST_FOLDER, 'ort-wasm-threaded.wasm'); const WASM_SIMD_WASM_PATH = path.join(WASM_DIST_FOLDER, 'ort-wasm-simd.wasm'); const WASM_SIMD_THREADED_WASM_PATH = path.join(WASM_DIST_FOLDER, 'ort-wasm-simd-threaded.wasm'); +const WASM_SIMD_JSEP_WASM_PATH = path.join(WASM_DIST_FOLDER, 'ort-wasm-simd.jsep.wasm'); +const WASM_SIMD_THREADED_JSEP_WASM_PATH = path.join(WASM_DIST_FOLDER, 'ort-wasm-simd-threaded.jsep.wasm'); const WASM_THREADED_WORKER_JS_PATH = path.join(WASM_DIST_FOLDER, 'ort-wasm-threaded.worker.js'); const WASM_THREADED_JS_PATH = path.join(WASM_DIST_FOLDER, 'ort-wasm-threaded.js'); +const WASM_SIMD_THREADED_JSEP_JS_PATH = path.join(WASM_DIST_FOLDER, 'ort-wasm-simd-threaded.jsep.js'); function validateFile(path: string): void { npmlog.info('Build', `Ensure file: ${path}`); @@ -61,11 +67,14 @@ if (WASM) { try { validateFile(WASM_BINDING_JS_PATH); validateFile(WASM_BINDING_THREADED_JS_PATH); + validateFile(WASM_BINDING_SIMD_THREADED_JSEP_JS_PATH); validateFile(WASM_BINDING_THREADED_WORKER_JS_PATH); validateFile(WASM_WASM_PATH); validateFile(WASM_THREADED_WASM_PATH); validateFile(WASM_SIMD_WASM_PATH); validateFile(WASM_SIMD_THREADED_WASM_PATH); + validateFile(WASM_SIMD_JSEP_WASM_PATH); + validateFile(WASM_SIMD_THREADED_JSEP_WASM_PATH); } catch (e) { npmlog.error('Build', `WebAssembly files are not ready. build WASM first. ERR: ${e}`); throw e; @@ -86,7 +95,7 @@ if (WASM) { 'npx', [ 'terser', WASM_BINDING_THREADED_JS_PATH, '--compress', 'passes=2', '--format', 'comments=false', '--mangle', - 'reserved=[_scriptDir]', '--module' + 'reserved=[_scriptDir,startWorker]', '--module' ], {shell: true, encoding: 'utf-8', cwd: ROOT_FOLDER}); if (terser.status !== 0) { @@ -105,13 +114,38 @@ if (WASM) { } npmlog.info('Build', 'Minimizing file "ort-wasm-threaded.js"... DONE'); + npmlog.info('Build', 'Minimizing file "ort-wasm-simd-threaded.jsep.js"...'); + try { + const terser = spawnSync( + 'npx', + [ + 'terser', WASM_BINDING_SIMD_THREADED_JSEP_JS_PATH, '--compress', 'passes=2', '--format', 'comments=false', + '--mangle', 'reserved=[_scriptDir,startWorker]', '--module' + ], + {shell: true, encoding: 'utf-8', cwd: ROOT_FOLDER}); + if (terser.status !== 0) { + console.error(terser.error); + process.exit(terser.status === null ? undefined : terser.status); + } + + fs.writeFileSync(WASM_BINDING_SIMD_THREADED_JSEP_MIN_JS_PATH, terser.stdout); + fs.writeFileSync(WASM_SIMD_THREADED_JSEP_JS_PATH, `${COPYRIGHT_BANNER}${terser.stdout}`); + + validateFile(WASM_BINDING_SIMD_THREADED_JSEP_MIN_JS_PATH); + validateFile(WASM_SIMD_THREADED_JSEP_JS_PATH); + } catch (e) { + npmlog.error('Build', `Failed to run terser on ort-wasm-threaded.js. ERR: ${e}`); + throw e; + } + npmlog.info('Build', 'Minimizing file "ort-wasm-simd-threaded.jsep.js"... DONE'); + npmlog.info('Build', 'Minimizing file "ort-wasm-threaded.worker.js"...'); try { const terser = spawnSync( 'npx', [ 'terser', WASM_BINDING_THREADED_WORKER_JS_PATH, '--compress', 'passes=2', '--format', 'comments=false', - '--mangle', 'reserved=[_scriptDir]', '--toplevel' + '--mangle', 'reserved=[_scriptDir,startWorker]', '--toplevel' ], {shell: true, encoding: 'utf-8'}); if (terser.status !== 0) { diff --git a/js/web/webpack.config.js b/js/web/webpack.config.js index 85d6ac3381..84ab9bcae8 100644 --- a/js/web/webpack.config.js +++ b/js/web/webpack.config.js @@ -49,7 +49,7 @@ function defaultTerserPluginOptions(target) { passes: 2 }, mangle: { - reserved: ["_scriptDir"] + reserved: ["_scriptDir","startWorker"] } } }; @@ -123,6 +123,7 @@ function buildConfig({ filename, format, target, mode, devtool, build_defs }) { if (mode === 'production') { config.resolve.alias['./binding/ort-wasm-threaded.js'] = './binding/ort-wasm-threaded.min.js'; + config.resolve.alias['./binding/ort-wasm-threaded-simd.jsep.js'] = './binding/ort-wasm-threaded-simd.jsep.min.js'; config.resolve.alias['./binding/ort-wasm-threaded.worker.js'] = './binding/ort-wasm-threaded.min.worker.js'; const options = defaultTerserPluginOptions(target);