|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | +const esbuild = require("esbuild"); |
| 4 | +const glob = require("glob"); |
| 5 | + |
| 6 | +// automatically find all files in the src/lit-actions/src directory |
| 7 | +const ENTRY_POINTS = glob.sync("./src/lit-actions/**/*.ts"); |
| 8 | + |
| 9 | +const configs = ENTRY_POINTS.map((entryPoint) => { |
| 10 | + // Read the content and extract the comment block |
| 11 | + const content = fs.readFileSync(entryPoint, "utf8"); |
| 12 | + const commentBlock = content.match(/\/\*\*([\s\S]*?)\*\//)?.[1]; |
| 13 | + |
| 14 | + if (!commentBlock) return { entryPoint }; |
| 15 | + |
| 16 | + // Find all lines containing 'inject' or 'inject:' |
| 17 | + const injectLines = commentBlock |
| 18 | + .split("\n") |
| 19 | + .filter((line) => line.includes("inject")); |
| 20 | + |
| 21 | + // Extract the injected values |
| 22 | + const injectedValues = injectLines.map((line) => { |
| 23 | + const match = line.match(/inject:?\s*([^\s]+)/); |
| 24 | + return match ? match[1] : null; |
| 25 | + }); |
| 26 | + |
| 27 | + // for each injected value, check if the file exist |
| 28 | + injectedValues.forEach((injectedValue) => { |
| 29 | + if (injectedValue && !fs.existsSync(injectedValue)) { |
| 30 | + throw new Error(`❌ File ${injectedValue} does not exist`); |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + return { |
| 35 | + entryPoint, |
| 36 | + ...(injectedValues.length > 0 && { injectedValues }), |
| 37 | + }; |
| 38 | +}); |
| 39 | + |
| 40 | +const ensureDirectoryExistence = (filePath) => { |
| 41 | + const dirname = path.dirname(filePath); |
| 42 | + if (!fs.existsSync(dirname)) { |
| 43 | + fs.mkdirSync(dirname, { recursive: true }); |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +const wrapIIFEInStringPlugin = { |
| 48 | + name: "wrap-iife-in-string", |
| 49 | + setup(build) { |
| 50 | + // Ensure write is set to false so our plugin will always receive outputFiles |
| 51 | + build.initialOptions.write = false; |
| 52 | + |
| 53 | + build.onEnd((result) => { |
| 54 | + if (result.errors.length > 0) { |
| 55 | + console.error("Build failed with errors:", result.errors); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + result.outputFiles.forEach((outputFile) => { |
| 60 | + let content = outputFile.text; |
| 61 | + |
| 62 | + // IMPORTANT: if minify is disabled, we need to: |
| 63 | + // 1. remove var import_ethers = __require("ethers"); |
| 64 | + // 2. remove import_ethers. |
| 65 | + content = content |
| 66 | + .replace(/var import_ethers = __require\("ethers"\);/g, "") |
| 67 | + .replace(/import_ethers\./g, ""); |
| 68 | + |
| 69 | + // IMPORTANT: if minify is enabled, we need to: |
| 70 | + // 1. remove var t=o(\"ethers\"); |
| 71 | + // 2. replace t.ethers to ethers |
| 72 | + content = content |
| 73 | + .replace(/var t=o\("ethers"\);/g, "") |
| 74 | + .replace(/t\.ethers/g, "ethers"); |
| 75 | + |
| 76 | + // Use JSON.stringify to safely encode the content |
| 77 | + const wrappedContent = `/** |
| 78 | + * DO NOT EDIT THIS FILE. IT IS GENERATED ON BUILD. RUN \`yarn generate-lit-actions\` IN THE ROOT DIRECTORY TO UPDATE THIS FILE. |
| 79 | + * |
| 80 | + * @type { string } code - Lit Action code. You want to use the content in the "code" constant and NOT the content of this file. |
| 81 | + * |
| 82 | + */ |
| 83 | +export const code = ${JSON.stringify(content, null, 2)}; |
| 84 | +`; |
| 85 | + |
| 86 | + // Ensure the output directory exists |
| 87 | + const outputPath = path.resolve(outputFile.path); |
| 88 | + ensureDirectoryExistence(outputPath); |
| 89 | + |
| 90 | + // Write the modified content back to the output file |
| 91 | + fs.writeFileSync(outputPath, wrappedContent); |
| 92 | + }); |
| 93 | + }); |
| 94 | + }, |
| 95 | +}; |
| 96 | + |
| 97 | +const promises = configs.map((config) => { |
| 98 | + return esbuild.build({ |
| 99 | + entryPoints: [config.entryPoint], |
| 100 | + bundle: true, |
| 101 | + minify: true, |
| 102 | + treeShaking: true, |
| 103 | + outdir: "./src/lit-actions/dist", |
| 104 | + external: ["ethers"], |
| 105 | + plugins: [wrapIIFEInStringPlugin], |
| 106 | + ...(config?.injectedValues && { inject: config?.injectedValues }), |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +// resolve all promises |
| 111 | +const startTime = Date.now(); |
| 112 | + |
| 113 | +Promise.all(promises) |
| 114 | + .then((results) => { |
| 115 | + results.forEach((result) => { |
| 116 | + // Check if outputFiles is defined and is an array |
| 117 | + if (result.outputFiles && Array.isArray(result.outputFiles)) { |
| 118 | + result.outputFiles.forEach((file) => { |
| 119 | + const bytes = file.contents.length; |
| 120 | + const mbInBinary = (bytes / (1024 * 1024)).toFixed(4); |
| 121 | + const mbInDecimal = (bytes / 1_000_000).toFixed(4); |
| 122 | + const fileName = path.relative(process.cwd(), file.path); |
| 123 | + console.log(`🗂️ File: ${fileName}`); |
| 124 | + console.log( |
| 125 | + ` Size: ${mbInDecimal} MB (decimal) | ${mbInBinary} MB (binary)` |
| 126 | + ); |
| 127 | + }); |
| 128 | + } |
| 129 | + }); |
| 130 | + |
| 131 | + const endTime = Date.now(); |
| 132 | + const buildTime = (endTime - startTime) / 1000; // Convert to seconds |
| 133 | + const msg = `✅ Lit actions built successfully in ${buildTime.toFixed( |
| 134 | + 2 |
| 135 | + )} seconds`; |
| 136 | + console.log( |
| 137 | + msg |
| 138 | + .split("") |
| 139 | + .map((char) => "=") |
| 140 | + .join("") |
| 141 | + ); |
| 142 | + console.log(msg); |
| 143 | + }) |
| 144 | + .catch((error) => { |
| 145 | + console.error("❌ Error building lit actions: ", error); |
| 146 | + process.exit(1); |
| 147 | + }); |
0 commit comments