-
-
Notifications
You must be signed in to change notification settings - Fork 780
/
index.ts
40 lines (37 loc) · 1.07 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
import type { Extractor } from '@unocss/core'
const regexVueTemplate = /<template.*?lang=['"]pug['"][^>]*?>\s*([\s\S]*?\s*)<\/template>/gm
export default function extractorPug(): Extractor {
async function compile(code: string, id: string) {
const Pug = await import('pug')
try {
return Pug.compile(code, { filename: id })()
// other build processes will catch pug errors
}
catch { }
}
return {
name: 'pug',
order: -1,
async extract(ctx) {
if (!ctx.id)
return
if (ctx.id.match(/\.pug$/) || ctx.id.match(/\?vue&type=template/)) {
try {
ctx.code = await compile(ctx.code, ctx.id) || ctx.code
}
catch {}
}
else if (ctx.id.match(/\.vue$/)) {
const matches = Array.from(ctx.code.matchAll(regexVueTemplate))
let tail = ''
for (const match of matches) {
if (match && match[1])
tail += `\n${await compile(match[1], ctx.id)}`
}
if (tail)
ctx.code = `${ctx.code}\n\n${tail}`
}
return undefined
},
}
}