|
1 | 1 | import path from "path"; |
| 2 | +import fs from "fs"; |
| 3 | +import { createHash } from "crypto"; |
2 | 4 | import { defineConfig } from "vite"; |
| 5 | +import type { Plugin } from "vite"; |
3 | 6 | import dtsPlugin from "vite-plugin-dts"; |
4 | | -import { nodePolyfills } from "vite-plugin-node-polyfills"; |
| 7 | +// Polyfills are usually not needed in the snap runtime. Keep it only if you know you need specific shims. |
| 8 | +// import { nodePolyfills } from "vite-plugin-node-polyfills"; |
| 9 | + |
| 10 | +function updateManifestShasum(): Plugin { |
| 11 | + return { |
| 12 | + name: "update-manifest-shasum", |
| 13 | + closeBundle() { |
| 14 | + const bundlePath = path.resolve(__dirname, "dist", "bundle.js"); |
| 15 | + if (!fs.existsSync(bundlePath)) return; |
| 16 | + |
| 17 | + const source = fs.readFileSync(bundlePath); |
| 18 | + const shasum = createHash("sha256").update(source).digest("base64"); // MetaMask expects base64 sha256 |
| 19 | + |
| 20 | + const manifestPath = path.resolve(__dirname, "snap.manifest.json"); |
| 21 | + const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); |
| 22 | + |
| 23 | + // IMPORTANT: this URL must match how the file will be fetched by MetaMask |
| 24 | + // Your playground serves ../snap at http://localhost:5173/ |
| 25 | + manifest.source = manifest.source ?? {}; |
| 26 | + manifest.source.location = manifest.source.location ?? {}; |
| 27 | + manifest.source.location.http = { |
| 28 | + url: "http://localhost:5173/dist/bundle.js", |
| 29 | + }; |
| 30 | + manifest.source.shasum = shasum; |
| 31 | + |
| 32 | + fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2)); |
| 33 | + console.log("[snap] Updated manifest shasum"); |
| 34 | + }, |
| 35 | + }; |
| 36 | +} |
5 | 37 |
|
6 | 38 | export default defineConfig({ |
7 | 39 | build: { |
| 40 | + target: "es2020", |
| 41 | + sourcemap: false, |
| 42 | + minify: "esbuild", |
| 43 | + emptyOutDir: true, |
8 | 44 | lib: { |
9 | 45 | entry: path.resolve(__dirname, "src/index.ts"), |
10 | | - name: "Ecency Snap", |
11 | | - formats: ["es"], |
| 46 | + name: "EcencySnap", |
| 47 | + formats: ["iife"], // self-executing, no imports |
12 | 48 | fileName: () => "bundle.js", |
13 | 49 | }, |
14 | 50 | rollupOptions: { |
15 | | - external: [ |
16 | | - "crypto", |
17 | | - "@ecency/wallets", |
18 | | - "@okxweb3/coin-aptos", |
19 | | - "@okxweb3/coin-base", |
20 | | - "@okxweb3/coin-bitcoin", |
21 | | - "@okxweb3/coin-cosmos", |
22 | | - "@okxweb3/coin-ethereum", |
23 | | - "@okxweb3/coin-solana", |
24 | | - "@okxweb3/coin-ton", |
25 | | - "@okxweb3/coin-tron", |
26 | | - "@okxweb3/crypto-lib", |
27 | | - "bip39", |
28 | | - ], |
| 51 | + output: { |
| 52 | + inlineDynamicImports: true, // single file |
| 53 | + }, |
| 54 | + treeshake: true, |
29 | 55 | }, |
30 | 56 | }, |
31 | 57 | resolve: { |
32 | 58 | alias: { |
33 | 59 | "@": path.resolve(__dirname, "./src"), |
34 | 60 | }, |
35 | 61 | }, |
36 | | - plugins: [dtsPlugin(), nodePolyfills()], |
| 62 | + plugins: [ |
| 63 | + dtsPlugin({ insertTypesEntry: false }), |
| 64 | + // nodePolyfills(), |
| 65 | + updateManifestShasum(), |
| 66 | + ], |
37 | 67 | }); |
0 commit comments