| 3 | const path = require('path'); |
| 4 | |
| 5 | async function extractSources() { |
| 6 | const mapFile = path.join(__dirname, 'package/cli.js.map'); |
| 7 | console.log('Reading source map...'); |
| 8 | const rawMap = JSON.parse(fs.readFileSync(mapFile, 'utf8')); |
| 9 | |
| 10 | console.log(`Sources count: ${rawMap.sources.length}`); |
| 11 | console.log('Sample sources:', rawMap.sources.slice(0, 10)); |
| 12 | |
| 13 | const outDir = path.join(__dirname, 'restored-src'); |
| 14 | |
| 15 | let written = 0, skipped = 0; |
| 16 | |
| 17 | for (let i = 0; i < rawMap.sources.length; i++) { |
| 18 | const sourcePath = rawMap.sources[i]; |
| 19 | const content = rawMap.sourcesContent && rawMap.sourcesContent[i]; |
| 20 | |
| 21 | if (!content) { skipped++; continue; } |
| 22 | |
| 23 | // Sanitize path |
| 24 | let relPath = sourcePath |
| 25 | .replace(/^.*node_modules\//, 'node_modules/') |
| 26 | .replace(/^webpack:\/\/\//, '') |
| 27 | .replace(/^webpack:\/\//, '') |
| 28 | .replace(/^\/?\.\.\//, '') |
| 29 | .replace(/\?.*$/, ''); |
| 30 | |
| 31 | if (!relPath || relPath === 'webpack/bootstrap') { |
| 32 | relPath = `__webpack__/source_${i}.js`; |
| 33 | } |
| 34 | |
| 35 | // Remove leading slashes and dangerous path components |
| 36 | relPath = relPath.replace(/^\/+/, '').replace(/\.\.\//g, '_dotdot_/'); |
| 37 | |
| 38 | const fullPath = path.join(outDir, relPath); |
| 39 | const dir = path.dirname(fullPath); |
| 40 | |
| 41 | fs.mkdirSync(dir, { recursive: true }); |
| 42 | fs.writeFileSync(fullPath, content, 'utf8'); |
| 43 | written++; |
| 44 | |
| 45 | if (written % 500 === 0) console.log(`Written ${written} files...`); |
| 46 | } |
| 47 | |
| 48 | console.log(`Done. Written: ${written}, Skipped (no content): ${skipped}`); |
| 49 | } |
| 50 | |
| 51 | extractSources().catch(console.error); |