@@ -31,12 +31,25 @@ const __dirname = path.dirname(__filename);
3131const fileHeaderPath = path . resolve ( __dirname , '../../../.idea/fileTemplates/includes/PAG File Header.h' ) ;
3232const banner = readFileSync ( fileHeaderPath , 'utf-8' ) ;
3333
34- // Copy WASM files to lib
34+ // ARCH selects the wasm output flavor: 'wasm-mt' (multi-threaded) or 'wasm' (single-threaded).
35+ // Both flavors publish to the same lib/ directory; the multi-threaded build keeps the
36+ // canonical filenames (pagx-viewer.umd.js, pagx-viewer.wasm), while the single-threaded
37+ // build adds a `.st` infix (pagx-viewer.st.umd.js, pagx-viewer.st.wasm) to disambiguate.
38+ const SUPPORTED_ARCHS = [ 'wasm-mt' , 'wasm' ] ;
39+ const arch = process . env . ARCH || 'wasm-mt' ;
40+ if ( ! SUPPORTED_ARCHS . includes ( arch ) ) {
41+ throw new Error ( `Unsupported ARCH: ${ arch } . Expected one of ${ SUPPORTED_ARCHS . join ( ', ' ) } .` ) ;
42+ }
43+ const nameInfix = arch === 'wasm-mt' ? '' : '.st' ;
44+ const libDir = path . resolve ( __dirname , '../lib' ) ;
45+
46+ // Copy the wasm next to the bundle. For the single-threaded build, the wasm is renamed
47+ // with the `.st` infix; the emcc glue's hard-coded `new URL("pagx-viewer.wasm", ...)` is
48+ // rewritten to the matching name later by script/fix-wasm-imports.js.
3549const copyWasmPlugin = {
3650 name : 'copy-wasm' ,
3751 writeBundle ( ) {
38- const wasmDir = path . resolve ( __dirname , '../wasm-mt' ) ;
39- const libDir = path . resolve ( __dirname , '../lib' ) ;
52+ const wasmDir = path . resolve ( __dirname , `../${ arch } ` ) ;
4053
4154 if ( ! existsSync ( libDir ) ) {
4255 mkdirSync ( libDir , { recursive : true } ) ;
@@ -45,20 +58,28 @@ const copyWasmPlugin = {
4558 // Copy wasm file
4659 const wasmFile = path . join ( wasmDir , 'pagx-viewer.wasm' ) ;
4760 if ( existsSync ( wasmFile ) ) {
48- copyFileSync ( wasmFile , path . join ( libDir , ' pagx-viewer.wasm' ) ) ;
61+ copyFileSync ( wasmFile , path . join ( libDir , ` pagx-viewer${ nameInfix } .wasm` ) ) ;
4962 }
5063 } ,
5164} ;
5265
5366const plugins = [
54- esbuild ( { tsconfig : path . resolve ( __dirname , '../tsconfig.json' ) , minify : false } ) ,
55- resolve ( { extensions : [ '.ts' , '.js' ] } ) ,
56- commonJs ( ) ,
67+ // MUST stay before resolve()/esbuild() — see PR #3436. Otherwise node-resolve will
68+ // resolve `../../wasm-mt/pagx-viewer` to a concrete file before alias rewrites it ,
69+ // and the single-threaded bundle will silently inline the multi-threaded glue.
5770 alias ( {
5871 entries : [
5972 { find : '@tgfx' , replacement : path . resolve ( __dirname , '../../../third_party/tgfx/web/src' ) } ,
73+ // Redirect the wasm glue import in pagx.ts to the requested arch's output directory.
74+ {
75+ find : '../../wasm-mt/pagx-viewer' ,
76+ replacement : path . resolve ( __dirname , `../${ arch } /pagx-viewer` ) ,
77+ } ,
6078 ] ,
6179 } ) ,
80+ esbuild ( { tsconfig : path . resolve ( __dirname , '../tsconfig.json' ) , minify : false } ) ,
81+ resolve ( { extensions : [ '.ts' , '.js' ] } ) ,
82+ commonJs ( ) ,
6283 {
6384 name : 'preserve-import-meta-url' ,
6485 resolveImportMeta ( property ) {
@@ -71,7 +92,6 @@ const plugins = [
7192] ;
7293
7394const input = path . resolve ( __dirname , '../src/ts/pagx.ts' ) ;
74- const libDir = path . resolve ( __dirname , '../lib' ) ;
7595
7696// UMD format (for script tag usage, mounts to window.pagxViewer)
7797const umdConfig = {
@@ -82,7 +102,7 @@ const umdConfig = {
82102 format : 'umd' ,
83103 exports : 'named' ,
84104 sourcemap : true ,
85- file : path . join ( libDir , ' pagx-viewer.umd.js' ) ,
105+ file : path . join ( libDir , ` pagx-viewer${ nameInfix } .umd.js` ) ,
86106 } ,
87107 plugins : [ ...plugins ] ,
88108} ;
@@ -96,7 +116,7 @@ const umdMinConfig = {
96116 format : 'umd' ,
97117 exports : 'named' ,
98118 sourcemap : true ,
99- file : path . join ( libDir , ' pagx-viewer.min.js' ) ,
119+ file : path . join ( libDir , ` pagx-viewer${ nameInfix } .min.js` ) ,
100120 } ,
101121 plugins : [ ...plugins , terser ( ) ] ,
102122} ;
@@ -105,8 +125,8 @@ const umdMinConfig = {
105125const moduleConfig = {
106126 input,
107127 output : [
108- { banner, file : path . join ( libDir , ' pagx-viewer.esm.js' ) , format : 'esm' , sourcemap : true } ,
109- { banner, file : path . join ( libDir , ' pagx-viewer.cjs.js' ) , format : 'cjs' , exports : 'named' , sourcemap : true } ,
128+ { banner, file : path . join ( libDir , ` pagx-viewer${ nameInfix } .esm.js` ) , format : 'esm' , sourcemap : true } ,
129+ { banner, file : path . join ( libDir , ` pagx-viewer${ nameInfix } .cjs.js` ) , format : 'cjs' , exports : 'named' , sourcemap : true } ,
110130 ] ,
111131 plugins : [ ...plugins , copyWasmPlugin ] ,
112132} ;
0 commit comments