generated from unplugin/unplugin-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
50 lines (45 loc) · 1.42 KB
/
index.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
import { createUnplugin } from 'unplugin'
import { getSkypackUrl } from '@tropicalraisel/skylib'
import { Options } from './types'
import { resolveOptions } from './core/options'
export default createUnplugin<Options>((options, meta) => {
const settings = resolveOptions(options)
const name = 'unplugin-skypin'
const urls = new Map<string, string>()
return {
name,
enforce: 'pre',
async buildStart() {
if (urls.size === 0) {
for (const id of settings.packages) {
const sub = settings.replace(id)
if (sub)
urls.set(typeof sub === 'string' ? sub : id, await getSkypackUrl(id, settings.minify))
}
}
},
resolveId(id: string) {
if (urls.has(id)) {
const url = urls.get(id)!
return meta.framework !== 'webpack' ? { id: url, external: true } : url
}
return id
},
/* transform(code: string, filename: string) {
if (meta.framework === 'webpack' && filename.endsWith('.html')) {
const dom = new BeautifulDom(code)
for (const id of urls.keys()) {
const attr = `src=${id}`
const src = dom.querySelector(`script[${attr}]`)
if (src) {
code.replace(new RegExp(src.outerHTML), (match) => {
return match.replace(attr, `src=${urls.get(id)}`)
})
}
}
}
return code
},
webpack(compiler: Compiler) {}, */
}
})