MCPcopy Create free account
hub / github.com/Rich-Harris/magic-string / main

Function main

example/build.js:4–33  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2import process from 'node:process'
3
4async 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
35main().catch((error) => {
36 console.error(error)

Callers 1

build.jsFile · 0.85

Calls 5

overwriteMethod · 0.95
toStringMethod · 0.95
generateMapMethod · 0.95
toStringMethod · 0.45
toUrlMethod · 0.45

Tested by

no test coverage detected