| 2 | import process from 'node:process' |
| 3 | |
| 4 | async function main() { |
| 5 | const { default: MagicString } = await import('../dist/index.mjs') |
| 6 | |
| 7 | process.chdir(import.meta.dirname) |
| 8 | |
| 9 | const result = await fs.promises.readFile('app.source.js', 'utf-8') |
| 10 | const source = result.toString() |
| 11 | const magicString = new MagicString(source) |
| 12 | |
| 13 | const pattern = /foo/g |
| 14 | while (true) { |
| 15 | const match = pattern.exec(source) |
| 16 | if (!match) |
| 17 | break |
| 18 | |
| 19 | magicString.overwrite(match.index, match.index + 3, 'answer') |
| 20 | } |
| 21 | |
| 22 | const transpiled = `${magicString.toString()}\n//# sourceMappingURL=app.js.map` |
| 23 | const map = magicString.generateMap({ |
| 24 | file: 'app.js.map', |
| 25 | source: 'app.source.js', |
| 26 | includeContent: true, |
| 27 | hires: true, |
| 28 | }) |
| 29 | |
| 30 | fs.writeFileSync('app.js', transpiled) |
| 31 | fs.writeFileSync('app.js.map', JSON.stringify(map)) |
| 32 | fs.writeFileSync('app.inlinemap.js', `${transpiled}\n//#sourceMappingURL=${map.toUrl()}`) |
| 33 | } |
| 34 | |
| 35 | main().catch((error) => { |
| 36 | console.error(error) |