* 从普通 JS bundle 的注释中读取 metadata
(buffer)
| 163 | * 从普通 JS bundle 的注释中读取 metadata |
| 164 | */ |
| 165 | function readMetadataFromJSBundle(buffer) { |
| 166 | const content = buffer.toString('utf8'); |
| 167 | const metadata = {}; |
| 168 | |
| 169 | // 查找 //# BUNDLE_METADATA {...} 注释 |
| 170 | const metaMatch = content.match(/\/\/# BUNDLE_METADATA\s+(\{[^}]+\})/); |
| 171 | if (metaMatch) { |
| 172 | try { |
| 173 | const metaObj = JSON.parse(metaMatch[1]); |
| 174 | metadata.contentHash = metaObj.contentHash; |
| 175 | console.log(`✅ Found contentHash in JS bundle comment: ${metadata.contentHash.slice(0, 16)}...`); |
| 176 | } catch (e) { |
| 177 | console.error('Failed to parse BUNDLE_METADATA comment:', e); |
| 178 | } |
| 179 | } else { |
| 180 | console.warn('⚠️ BUNDLE_METADATA comment not found in JS bundle'); |
| 181 | } |
| 182 | |
| 183 | return metadata; |
| 184 | } |
| 185 | |
| 186 | // 测试读取 |
| 187 | const metadata = readBundleMetadata('./index.bundlejs'); |