-
-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathindex.ts
71 lines (67 loc) · 2.08 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import type { Options } from './types'
import { createUnplugin } from 'unplugin'
import { generateComponentFromPath, isIconPath, normalizeIconPath, resolveIconsPath } from './core/loader'
import { resolveOptions } from './core/options'
const unplugin = createUnplugin<Options | undefined>((options = {}) => {
const resolved = resolveOptions(options)
return {
name: 'unplugin-icons',
enforce: 'pre',
resolveId(id) {
if (isIconPath(id)) {
const normalizedId = normalizeIconPath(id)
// fix issue 322
const queryIndex = normalizedId.indexOf('?')
const res = `${(queryIndex > -1 ? normalizedId.slice(0, queryIndex) : normalizedId)
.replace(/\.\w+$/, '')
.replace(/^\//, '')}${queryIndex > -1 ? `?${normalizedId.slice(queryIndex + 1)}` : ''}`
const resolved = resolveIconsPath(res)
// accept raw compiler from query params
const compiler = resolved?.query?.raw === 'true' ? 'raw' : options.compiler
if (compiler && typeof compiler !== 'string') {
const ext = compiler.extension
if (ext)
return `${res}.${ext.startsWith('.') ? ext.slice(1) : ext}`
}
else {
switch (compiler) {
case 'astro':
return `${res}.astro`
case 'jsx':
return `${res}.jsx`
case 'qwik':
return `${res}.jsx`
case 'marko':
return `${res}.marko`
case 'svelte':
return `${res}.svelte`
case 'solid':
return `${res}.tsx`
}
}
return res
}
return null
},
loadInclude(id) {
return isIconPath(id)
},
async load(id) {
const config = await resolved
const code = await generateComponentFromPath(id, config) || null
if (code) {
return {
code,
map: { version: 3, mappings: '', sources: [] } as any,
}
}
},
rollup: {
api: {
config: options,
},
},
}
})
export * from './types'
export default unplugin