|
1 | 1 | import * as esbuild from 'esbuild'; |
2 | 2 | import { nodeExternalsPlugin } from 'esbuild-node-externals'; |
3 | | -import fs from 'fs'; |
4 | 3 | import { fileURLToPath } from 'url'; |
5 | | -import { dirname, resolve } from 'path'; |
| 4 | + |
| 5 | +const ALLOW_LIST = [ |
| 6 | + 'ethers', |
| 7 | + '@lit-protocol/accs-schemas', |
| 8 | + '@lit-protocol/contracts', |
| 9 | + 'crypto', |
| 10 | + 'secp256k1', |
| 11 | +]; |
| 12 | + |
| 13 | +const getPath = (relativePath) => |
| 14 | + fileURLToPath(new URL(relativePath, import.meta.url)); |
| 15 | + |
| 16 | +/** |
| 17 | + * Common esbuild configuration options. |
| 18 | + * @param {string} entry - Entry file path. |
| 19 | + * @param {string} outfile - Output file path. |
| 20 | + * @param {string} [globalName] - Optional global name for the bundle. |
| 21 | + * @returns {esbuild.BuildOptions} Esbuild configuration object. |
| 22 | + */ |
| 23 | +const createBuildConfig = (entry, outfile, globalName) => ({ |
| 24 | + entryPoints: [getPath(entry)], |
| 25 | + outfile: getPath(outfile), |
| 26 | + bundle: true, |
| 27 | + plugins: [ |
| 28 | + nodeExternalsPlugin({ |
| 29 | + allowList: ALLOW_LIST, |
| 30 | + }), |
| 31 | + ], |
| 32 | + platform: 'node', |
| 33 | + target: 'esnext', |
| 34 | + format: 'esm', |
| 35 | + inject: [getPath('./shim.mjs')], |
| 36 | + mainFields: ['module', 'main'], |
| 37 | + ...(globalName ? { globalName } : {}), |
| 38 | +}); |
6 | 39 |
|
7 | 40 | /** |
8 | | - * Builds the project using esbuild. |
9 | | - * @returns {Promise<void>} A promise that resolves when the build is complete. |
| 41 | + * Builds the CLI-enabled version of Tinny. |
10 | 42 | */ |
11 | 43 | export const build = async () => { |
12 | | - await esbuild.build({ |
13 | | - entryPoints: [fileURLToPath(new URL('./index.ts', import.meta.url))], |
14 | | - outfile: fileURLToPath(new URL('./index.js', import.meta.url)), |
15 | | - bundle: true, |
16 | | - globalName: 'tinnySdk', |
17 | | - plugins: [ |
18 | | - nodeExternalsPlugin({ |
19 | | - allowList: [ |
20 | | - 'ethers', |
21 | | - '@lit-protocol/accs-schemas', |
22 | | - '@lit-protocol/contracts', |
23 | | - 'crypto', |
24 | | - 'secp256k1', |
25 | | - ], |
26 | | - }), |
27 | | - ], |
28 | | - platform: 'node', |
29 | | - target: 'esnext', |
30 | | - format: 'esm', |
31 | | - inject: [fileURLToPath(new URL('./shim.mjs', import.meta.url))], |
32 | | - mainFields: ['module', 'main'], |
33 | | - }); |
| 44 | + await esbuild.build(createBuildConfig('./test.ts', './build/test.mjs')); |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
| 48 | + * Bundles Tinny as a standalone package. |
| 49 | + */ |
| 50 | +export const bundle = async () => { |
| 51 | + await esbuild.build( |
| 52 | + createBuildConfig('./index.ts', './index.js', 'tinnySdk') |
| 53 | + ); |
34 | 54 | }; |
35 | 55 |
|
36 | 56 | // Go! |
37 | 57 | (async () => { |
38 | 58 | const start = Date.now(); |
39 | | - await build(); |
40 | | - console.log(`[build.mjs] 🚀 Build time: ${Date.now() - start}ms`); |
| 59 | + try { |
| 60 | + await build(); |
| 61 | + await bundle(); |
| 62 | + console.log(`[build.mjs] 🚀 Build time: ${Date.now() - start}ms`); |
| 63 | + } catch (error) { |
| 64 | + console.error(`[build.mjs] ❌ Build failed:`, error); |
| 65 | + } |
41 | 66 | })(); |
0 commit comments