|
| 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(__dirname, 'apps', 'browser-extension-wallet', 'dist', 'manifest.json'); |
| 28 | + const v1Manifest = JSON.parse(fs.readFileSync(v1ManifestPath, 'utf-8')); |
| 29 | + |
| 30 | + // Read LMP manifest for CSP merging |
| 31 | + const lmpManifestPath = path.join(__dirname, 'lmp-build', 'dist', 'manifest.json'); |
| 32 | + const lmpManifest = JSON.parse(fs.readFileSync(lmpManifestPath, 'utf-8')); |
| 33 | + |
| 34 | + v1Manifest.name = 'Lace'; |
| 35 | + |
| 36 | + // Update service worker to use our sw-bundle.js |
| 37 | + v1Manifest.background.service_worker = './sw-bundle.js'; |
| 38 | + |
| 39 | + // Update popup to use the bundle entrypoint, which is same as in v1 but doesn't load the script |
| 40 | + v1Manifest.action.default_popup = './popup.html'; |
| 41 | + |
| 42 | + for (const script of lmpManifest.content_scripts) { |
| 43 | + v1Manifest.content_scripts.push(script); |
| 44 | + } |
| 45 | + |
| 46 | + // Merge CSP connect-src values |
| 47 | + const v1CSP = v1Manifest.content_security_policy.extension_pages; |
| 48 | + const lmpCSP = lmpManifest.content_security_policy.extension_pages; |
| 49 | + |
| 50 | + // Extract connect-src from both CSPs |
| 51 | + const v1ConnectMatch = v1CSP.match(/connect-src ([^;]+)/); |
| 52 | + const lmpConnectMatch = lmpCSP.match(/connect-src ([^;]+)/); |
| 53 | + |
| 54 | + if (v1ConnectMatch && lmpConnectMatch) { |
| 55 | + // Parse connect-src URLs |
| 56 | + const v1ConnectUrls = new Set(v1ConnectMatch[1].trim().split(/\s+/)); |
| 57 | + const lmpConnectUrls = new Set(lmpConnectMatch[1].trim().split(/\s+/)); |
| 58 | + |
| 59 | + // Merge URLs (union of both sets) |
| 60 | + const mergedConnectUrls = new Set([...v1ConnectUrls, ...lmpConnectUrls]); |
| 61 | + |
| 62 | + // Reconstruct CSP with merged connect-src |
| 63 | + const mergedConnectSrc = Array.from(mergedConnectUrls).join(' '); |
| 64 | + v1Manifest.content_security_policy.extension_pages = v1CSP.replace( |
| 65 | + /connect-src [^;]+/, |
| 66 | + `connect-src ${mergedConnectSrc}` |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + // Merge web_accessible_resources |
| 71 | + if (lmpManifest.web_accessible_resources) { |
| 72 | + for (const lmpResource of lmpManifest.web_accessible_resources) { |
| 73 | + v1Manifest.web_accessible_resources.push(lmpResource); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Write merged manifest |
| 78 | + fs.writeFileSync(path.join(distDir, 'manifest.json'), JSON.stringify(v1Manifest, null, 2), 'utf-8'); |
| 79 | + |
| 80 | + console.log('[MergeManifestPlugin] Manifest merged successfully'); |
| 81 | + } |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + compiler.hooks.done.tap('MergeManifestPlugin', () => { |
| 86 | + console.log('\n[MergeManifestPlugin] Manifest merge finalized!\n'); |
| 87 | + }); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +module.exports = MergeManifestPlugin; |
0 commit comments