|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | + |
| 4 | +class MergeManifestPlugin { |
| 5 | + constructor(options = {}) { |
| 6 | + this.options = options; |
| 7 | + } |
| 8 | + |
| 9 | + apply(compiler) { |
| 10 | + compiler.hooks.initialize.tap("MergeManifestPlugin", () => { |
| 11 | + console.log("[MergeManifestPlugin] Manifest merge initialized\n"); |
| 12 | + }); |
| 13 | + |
| 14 | + compiler.hooks.thisCompilation.tap("MergeManifestPlugin", (compilation) => { |
| 15 | + compilation.hooks.processAssets.tap( |
| 16 | + { |
| 17 | + name: "MergeManifestPlugin", |
| 18 | + stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE, |
| 19 | + }, |
| 20 | + () => { |
| 21 | + const distDir = path.resolve(__dirname, "dist"); |
| 22 | + if (!fs.existsSync(distDir)) { |
| 23 | + fs.mkdirSync(distDir); |
| 24 | + } |
| 25 | + |
| 26 | + // Read v1 manifest as base |
| 27 | + const v1ManifestPath = path.join( |
| 28 | + __dirname, |
| 29 | + "v1", |
| 30 | + "apps", |
| 31 | + "browser-extension-wallet", |
| 32 | + "dist", |
| 33 | + "manifest.json" |
| 34 | + ); |
| 35 | + const v1Manifest = JSON.parse( |
| 36 | + fs.readFileSync(v1ManifestPath, "utf-8") |
| 37 | + ); |
| 38 | + |
| 39 | + // Read LMP manifest for CSP merging |
| 40 | + const lmpManifestPath = path.join( |
| 41 | + __dirname, |
| 42 | + "v2", |
| 43 | + "apps", |
| 44 | + "midnight-extension", |
| 45 | + "dist", |
| 46 | + "manifest.json" |
| 47 | + ); |
| 48 | + const lmpManifest = JSON.parse( |
| 49 | + fs.readFileSync(lmpManifestPath, "utf-8") |
| 50 | + ); |
| 51 | + |
| 52 | + v1Manifest.name = "Lace"; |
| 53 | + |
| 54 | + // Update service worker to use our sw-bundle.js |
| 55 | + v1Manifest.background.service_worker = "./sw-bundle.js"; |
| 56 | + |
| 57 | + // Update popup to use the bundle entrypoint, which is same as in v1 but doesn't load the script |
| 58 | + v1Manifest.action.default_popup = "./popup.html"; |
| 59 | + |
| 60 | + for (const script of lmpManifest.content_scripts) { |
| 61 | + v1Manifest.content_scripts.push(script); |
| 62 | + } |
| 63 | + |
| 64 | + // Merge CSP connect-src values |
| 65 | + const v1CSP = v1Manifest.content_security_policy.extension_pages; |
| 66 | + const lmpCSP = lmpManifest.content_security_policy.extension_pages; |
| 67 | + |
| 68 | + // Extract connect-src from both CSPs |
| 69 | + const v1ConnectMatch = v1CSP.match(/connect-src ([^;]+)/); |
| 70 | + const lmpConnectMatch = lmpCSP.match(/connect-src ([^;]+)/); |
| 71 | + |
| 72 | + if (v1ConnectMatch && lmpConnectMatch) { |
| 73 | + // Parse connect-src URLs |
| 74 | + const v1ConnectUrls = new Set( |
| 75 | + v1ConnectMatch[1].trim().split(/\s+/) |
| 76 | + ); |
| 77 | + const lmpConnectUrls = new Set( |
| 78 | + lmpConnectMatch[1].trim().split(/\s+/) |
| 79 | + ); |
| 80 | + |
| 81 | + // Merge URLs (union of both sets) |
| 82 | + const mergedConnectUrls = new Set([ |
| 83 | + ...v1ConnectUrls, |
| 84 | + ...lmpConnectUrls, |
| 85 | + ]); |
| 86 | + |
| 87 | + // Reconstruct CSP with merged connect-src |
| 88 | + const mergedConnectSrc = Array.from(mergedConnectUrls).join(" "); |
| 89 | + v1Manifest.content_security_policy.extension_pages = v1CSP.replace( |
| 90 | + /connect-src [^;]+/, |
| 91 | + `connect-src ${mergedConnectSrc}` |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + // Merge web_accessible_resources |
| 96 | + if (lmpManifest.web_accessible_resources) { |
| 97 | + for (const lmpResource of lmpManifest.web_accessible_resources) { |
| 98 | + v1Manifest.web_accessible_resources.push(lmpResource); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // Write merged manifest |
| 103 | + fs.writeFileSync( |
| 104 | + path.join(distDir, "manifest.json"), |
| 105 | + JSON.stringify(v1Manifest, null, 2), |
| 106 | + "utf-8" |
| 107 | + ); |
| 108 | + |
| 109 | + console.log("[MergeManifestPlugin] Manifest merged successfully"); |
| 110 | + } |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + compiler.hooks.done.tap("MergeManifestPlugin", () => { |
| 115 | + console.log("\n[MergeManifestPlugin] Manifest merge finalized!\n"); |
| 116 | + }); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +module.exports = MergeManifestPlugin; |
0 commit comments