@@ -13,6 +13,7 @@ const cargoPackageName = /\[package\]\nname = "(.*?)"/.exec(cargoTomlContent)[1]
1313const name = cargoPackageName . replace ( / - / g, '_' )
1414
1515const content = await readFile ( path . join ( __dirname , `../../pkg/nodejs/${ name } .js` ) , "utf8" ) ;
16+ const wasmBase64 = await readFile ( path . join ( __dirname , `../../pkg/nodejs/${ name } _bg.wasm` ) , "base64" ) ;
1617
1718const patched = content
1819 // use global TextDecoder TextEncoder
@@ -21,16 +22,28 @@ const patched = content
2122 . replace ( "= module.exports" , "= imports" )
2223 // Export classes
2324 . replace ( / \n c l a s s ( .* ?) \{ / g, "\n export class $1 {" )
24- // Export functions
25+ // Export enums (Object.freeze declarations)
26+ . replace ( / \n c o n s t ( \w + ) = O b j e c t \. f r e e z e / g, "\nexport const $1 = Object.freeze" )
27+ // Export standalone functions that are assigned to exports
28+ . replace ( / \n f u n c t i o n ( \w + ) \( / g, ( match , name , offset , str ) => {
29+ // Check if there's a corresponding exports.name = name; line
30+ if ( str . includes ( `exports.${ name } = ${ name } ;` ) ) {
31+ return `\nexport function ${ name } (` ;
32+ }
33+ return match ;
34+ } )
35+ // Export functions defined inline on module.exports (for older wasm-bindgen)
2536 . replace ( / \n m o d u l e .e x p o r t s .( .* ?) = f u n c t i o n / g, "\nimports.$1 = $1;\nexport function $1" )
26- // Add exports to ' imports'
37+ // Replace module. exports.X with imports.X (for older wasm-bindgen)
2738 . replace ( / \n m o d u l e \. e x p o r t s \. ( .* ?) \s + / g, "\nimports.$1" )
39+ // Replace exports.X = X; with imports.X = X; (for newer wasm-bindgen)
40+ . replace ( / \n e x p o r t s \. ( \w + ) = ( \w + ) ; / g, "\nimports.$1 = $2;" )
2841 // Remove default export of imports
2942 . replace ( / e x p o r t d e f a u l t i m p o r t s $ / , '' )
3043 // Replace inline wasm bytes with __toBinary function and embedded base64 bytes
3144 . replace (
32- / \n c o n s t p a t h .* \n c o n s t b y t e s .* \n / ,
33- `
45+ / \n c o n s t (?: w a s m ) ? [ P p ] a t h .* \n c o n s t ( (?: w a s m ) ? [ B b ] y t e s ) .* \n / ,
46+ ( match , bytesVar ) => `
3447var __toBinary = /* @__PURE__ */ (() => {
3548 var table = new Uint8Array(128);
3649 for (var i = 0; i < 64; i++)
@@ -48,19 +61,14 @@ var __toBinary = /* @__PURE__ */ (() => {
4861 };
4962})();
5063
51- const bytes = __toBinary(${ JSON . stringify ( await readFile ( path . join ( __dirname , `../../pkg/nodejs/ ${ name } _bg.wasm` ) , "base64" ) ) } );
64+ const ${ bytesVar } = __toBinary(${ JSON . stringify ( wasmBase64 ) } );
5265`
5366 ) ;
5467
5568// Write the patched JavaScript file with additional exports
5669// This creates the final index.js that will be used by Node.js/browser consumers
57- await writeFile ( path . join ( __dirname , `../../pkg/index.js` ) , patched
58- + "\nglobalThis['pubky'] = imports\n" // Make imports available globally as 'pubky'
59- + "// Re-export enums so Next.js can statically import them\n"
60- + "export const PubkyAppPostKind = imports.PubkyAppPostKind;\n" // Export enum for named imports
61- + "export const PubkyAppFeedLayout = imports.PubkyAppFeedLayout;\n" // Export enum for named imports
62- + "export const PubkyAppFeedReach = imports.PubkyAppFeedReach;\n" // Export enum for named imports
63- + "export const PubkyAppFeedSort = imports.PubkyAppFeedSort;\n" ) ; // Export enum for named imports
70+ await writeFile ( path . join ( __dirname , `../../pkg/index.js` ) , "const imports = {};\n" + patched
71+ + "\nglobalThis['pubky'] = imports\n" ) ; // Make imports available globally as 'pubky'
6472
6573// Move outside of nodejs
6674await Promise . all ( [ ".js" , ".d.ts" , "_bg.wasm" ] . map ( suffix =>
0 commit comments