11import * as esbuild from 'esbuild' ;
22import { nodeExternalsPlugin } from 'esbuild-node-externals' ;
33import fs from 'fs' ;
4-
5- const TEST_DIR = 'local-tests ';
4+ import { fileURLToPath } from 'url' ;
5+ import { dirname , resolve } from 'path ';
66
77/**
88 * Builds the project using esbuild.
99 * @returns {Promise<void> } A promise that resolves when the build is complete.
1010 */
1111export const build = async ( ) => {
1212 await esbuild . build ( {
13- entryPoints : [ `${ TEST_DIR } /test.ts` ] ,
14- outfile : `./${ TEST_DIR } /build/test.mjs` ,
15- bundle : true ,
16- plugins : [
17- nodeExternalsPlugin ( {
18- allowList : [
19- 'ethers' ,
20- '@lit-protocol/accs-schemas' ,
21- '@lit-protocol/contracts' ,
22- 'crypto' ,
23- 'secp256k1' ,
24- ] ,
25- } ) ,
26- ] ,
27- platform : 'node' ,
28- target : 'esnext' ,
29- format : 'esm' ,
30- inject : [ `./${ TEST_DIR } /shim.mjs` ] ,
31- mainFields : [ 'module' , 'main' ] ,
32- } ) ;
33-
34- await esbuild . build ( {
35- entryPoints : [ `${ TEST_DIR } /index.ts` ] ,
36- outfile : `./${ TEST_DIR } /index.js` ,
13+ entryPoints : [ fileURLToPath ( new URL ( './index.ts' , import . meta. url ) ) ] ,
14+ outfile : fileURLToPath ( new URL ( './index.js' , import . meta. url ) ) ,
3715 bundle : true ,
3816 globalName : 'tinnySdk' ,
3917 plugins : [
@@ -50,39 +28,14 @@ export const build = async () => {
5028 platform : 'node' ,
5129 target : 'esnext' ,
5230 format : 'esm' ,
53- inject : [ `./ ${ TEST_DIR } / shim.mjs` ] ,
31+ inject : [ fileURLToPath ( new URL ( './ shim.mjs' , import . meta . url ) ) ] ,
5432 mainFields : [ 'module' , 'main' ] ,
5533 } ) ;
5634} ;
5735
58- /**
59- * Inserts a polyfill at the beginning of a file.
60- * The polyfill ensures that the global `fetch` function is available.
61- * @returns {void }
62- */
63- export const postBuildPolyfill = ( ) => {
64- try {
65- const file = fs . readFileSync ( `./${ TEST_DIR } /build/test.mjs` , 'utf8' ) ;
66- const content = `import fetch from 'node-fetch';
67- try {
68- if (!globalThis.fetch) {
69- globalThis.fetch = fetch;
70- }
71- } catch (error) {
72- console.error('❌ Error in polyfill', error);
73- }
74- ` ;
75- const newFile = content + file ;
76- fs . writeFileSync ( `./${ TEST_DIR } /build/test.mjs` , newFile ) ;
77- } catch ( e ) {
78- throw new Error ( `Error in postBuildPolyfill: ${ e } ` ) ;
79- }
80- } ;
81-
8236// Go!
8337( async ( ) => {
8438 const start = Date . now ( ) ;
8539 await build ( ) ;
86- postBuildPolyfill ( ) ;
8740 console . log ( `[build.mjs] 🚀 Build time: ${ Date . now ( ) - start } ms` ) ;
8841} ) ( ) ;
0 commit comments