|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +const esbuild = require('esbuild'); |
| 5 | +const { polyfillNode } = require('@lit-protocol/esbuild-plugin-polyfill-node'); |
| 6 | +const Hash = require('ipfs-only-hash'); |
| 7 | + |
| 8 | +function aliasFetch() { |
| 9 | + const shim = path.resolve(__dirname, 'deno-fetch-shim.js'); |
| 10 | + |
| 11 | + return { |
| 12 | + name: 'alias-fetch', |
| 13 | + setup(build) { |
| 14 | + build.onResolve({ filter: /^node-fetch$/ }, () => ({ path: shim })); |
| 15 | + build.onResolve({ filter: /^cross-fetch(\/.*)?$/ }, () => ({ path: shim })); |
| 16 | + }, |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +const ensureDirectoryExistence = (filePath) => { |
| 21 | + const dirname = path.dirname(filePath); |
| 22 | + if (!fs.existsSync(dirname)) { |
| 23 | + fs.mkdirSync(dirname, { recursive: true }); |
| 24 | + } |
| 25 | +}; |
| 26 | + |
| 27 | +function getBundledVincentAbilityCode() { |
| 28 | + return `/** |
| 29 | + * DO NOT EDIT THIS FILE. IT IS GENERATED ON BUILD. |
| 30 | + */ |
| 31 | +
|
| 32 | +import { asBundledVincentAbility } from '@lit-protocol/vincent-ability-sdk'; |
| 33 | +import { vincentAbility } from '../lib/vincent-ability'; |
| 34 | +import metadata from './vincent-ability-metadata.json'; |
| 35 | +
|
| 36 | +if(!metadata.ipfsCid) { |
| 37 | + throw new Error('ipfsCid is not defined in metadata JSON file'); |
| 38 | +} |
| 39 | +
|
| 40 | +export const bundledVincentAbility = asBundledVincentAbility(vincentAbility, metadata.ipfsCid); |
| 41 | +`; |
| 42 | +} |
| 43 | + |
| 44 | +function litActionModuleCode({ ipfsCid, content }) { |
| 45 | + return `/** |
| 46 | + * DO NOT EDIT THIS FILE. IT IS GENERATED ON BUILD. |
| 47 | + * @type {string} |
| 48 | + */ |
| 49 | +const code = ${JSON.stringify(content)}; |
| 50 | +module.exports = { |
| 51 | + "code": code, |
| 52 | + "ipfsCid": "${ipfsCid}", |
| 53 | +}; |
| 54 | +`; |
| 55 | +} |
| 56 | + |
| 57 | +function metadataJsonFile({ ipfsCid }) { |
| 58 | + return `{ |
| 59 | + "ipfsCid": "${ipfsCid}" |
| 60 | +} |
| 61 | +`; |
| 62 | +} |
| 63 | + |
| 64 | +const createBundledAbilityFile = { |
| 65 | + name: 'create-vincent-bundled-ability-file', |
| 66 | + setup(build) { |
| 67 | + build.initialOptions.write = false; |
| 68 | + |
| 69 | + const sourceDir = path.dirname(build.initialOptions.entryPoints[0]); |
| 70 | + |
| 71 | + console.log(sourceDir); |
| 72 | + build.onEnd(async (result) => { |
| 73 | + if (result.errors.length > 0) { |
| 74 | + console.error('Build failed with errors:', result.errors); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + const outputFile = result.outputFiles[0]; |
| 79 | + const content = outputFile.text; |
| 80 | + const ipfsCid = await Hash.of(content); |
| 81 | + |
| 82 | + // Write bundledAbility wrapper |
| 83 | + const bundledSource = getBundledVincentAbilityCode(); |
| 84 | + const bundledPath = path.join(sourceDir, '../generated/vincent-bundled-ability.ts'); |
| 85 | + fs.writeFileSync(bundledPath, bundledSource); |
| 86 | + |
| 87 | + // Write metadata JSON |
| 88 | + const outputPath = path.dirname(path.resolve(outputFile.path)); |
| 89 | + const metadataPath = path.join(outputPath, 'vincent-ability-metadata.json'); |
| 90 | + const metadataContent = metadataJsonFile({ ipfsCid }); |
| 91 | + fs.writeFileSync(metadataPath, metadataContent); |
| 92 | + }); |
| 93 | + }, |
| 94 | +}; |
| 95 | + |
| 96 | +const wrapIIFEInStringPlugin = { |
| 97 | + name: 'wrap-iife-in-string', |
| 98 | + setup(build) { |
| 99 | + build.initialOptions.write = false; |
| 100 | + |
| 101 | + build.onEnd(async (result) => { |
| 102 | + if (result.errors.length > 0) { |
| 103 | + console.error('Build failed with errors:', result.errors); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + const outputFile = result.outputFiles[0]; |
| 108 | + const content = outputFile.text; |
| 109 | + const ipfsCid = await Hash.of(content); |
| 110 | + |
| 111 | + const wrapped = litActionModuleCode({ content, ipfsCid }); |
| 112 | + const outputPath = path.resolve(outputFile.path); |
| 113 | + ensureDirectoryExistence(outputPath); |
| 114 | + fs.writeFileSync(outputPath, wrapped); |
| 115 | + }); |
| 116 | + }, |
| 117 | +}; |
| 118 | + |
| 119 | +(async () => { |
| 120 | + try { |
| 121 | + await esbuild |
| 122 | + .build({ |
| 123 | + tsconfig: './tsconfig.lib.json', |
| 124 | + entryPoints: ['./src/lib/lit-action.ts'], |
| 125 | + bundle: true, |
| 126 | + minify: false, |
| 127 | + sourcemap: false, |
| 128 | + treeShaking: true, |
| 129 | + metafile: true, |
| 130 | + outdir: './src/generated/', |
| 131 | + plugins: [ |
| 132 | + aliasFetch(), |
| 133 | + polyfillNode({ |
| 134 | + globals: { |
| 135 | + Buffer: true, |
| 136 | + process: true, |
| 137 | + }, |
| 138 | + modules: { |
| 139 | + crypto: true, |
| 140 | + http: true, |
| 141 | + https: true, |
| 142 | + stream: true, |
| 143 | + zlib: true, |
| 144 | + url: true, |
| 145 | + util: true, |
| 146 | + }, |
| 147 | + }), |
| 148 | + wrapIIFEInStringPlugin, |
| 149 | + createBundledAbilityFile, |
| 150 | + ], |
| 151 | + platform: 'browser', |
| 152 | + write: false, |
| 153 | + }) |
| 154 | + .then((result) => { |
| 155 | + result.outputFiles.forEach((file) => { |
| 156 | + const bytes = file.text.length; |
| 157 | + const mbInBinary = (bytes / (1024 * 1024)).toFixed(4); |
| 158 | + const mbInDecimal = (bytes / 1_000_000).toFixed(4); |
| 159 | + |
| 160 | + console.log( |
| 161 | + `✅ ${file.path.split('/').pop()}\n- ${mbInDecimal} MB (in decimal)\n- ${mbInBinary} MB (in binary)`, |
| 162 | + ); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + console.log('✅ Vincent ability built successfully'); |
| 167 | + } catch (e) { |
| 168 | + console.error('❌ Error building Vincent ability: ', e); |
| 169 | + process.exit(1); |
| 170 | + } |
| 171 | +})(); |
0 commit comments