-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.ts
64 lines (59 loc) · 1.53 KB
/
demo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// @ts-check
import { readFile } from 'node:fs/promises'
import MagicString from 'magic-string'
import type { Plugin, PluginContext } from '../src'
let context: PluginContext
export function demoPlugin(): Plugin {
return {
name: 'demo-plugin',
options(config) {
config.sourcemap = true
},
buildStart(_context) {
context = _context
context.log('hello world')
},
async resolveId(source, importer, options) {
if (source.startsWith('node:')) return
if (source === 'virtual-mod') {
return '/virtual-mod'
}
const result = await this.resolve(`${source}.js`, importer, options)
if (result) return result
},
async load(id) {
if (id === '/virtual-mod') {
return { code: 'export const count = 42' }
}
if (id.endsWith('trace.js')) {
const code = await readFile(id, 'utf8')
const s = new MagicString(code)
s.prepend('// header\n')
const map = s.generateMap({
file: id,
hires: 'boundary',
includeContent: true,
})
return {
code: s.toString(),
map,
}
}
},
transform(code, id) {
if (id.endsWith('trace.js') && typeof code === 'string') {
const s = new MagicString(code)
s.prepend('// header2\n')
const map = s.generateMap({
file: id,
hires: 'boundary',
includeContent: true,
})
return {
code: s.toString(),
map,
}
}
},
}
}