-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite-plugin-insertBody.ts
50 lines (44 loc) · 1.26 KB
/
vite-plugin-insertBody.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
// 结合 useBodyClick hook 进行使用,模拟 document.body.addEventListener('click', () => {})
import type { PluginOption } from 'vite'
const insertedTemplate = `
<template${'$1'}>
<view
id="body"
@click="handleBodyClick"
>
${'$2'}
</view>
</template>
`
const insertedScript = `
<script${'$1'}>
import { handleBodyClick } from '@/hooks/useBodyClick'
${'$2'}
</script>
`
const insertedStyle = `
<style${'$1'}>
#body {
height: 100%;
overflow: auto;
}
${'$2'}
</style>
`
// 编译前对 src/pages/**/*.vue 的代码进行修改(插入 body 标签相关代码)
const viteInsertBody = (): PluginOption => ({
name: 'vite-plugin-insertBody',
enforce: 'pre',
transform(code, id) {
if (!/src\/pages\/.*\.vue$/.test(id)) return
let result = code.replace(/<template([^<]*?)>([\S\s]*\S+[\S\s]*)<\/template>/, insertedTemplate)
const replaced = result !== code // 是否已插入 body 标签
replaced && (result = result.replace(/<script([^<]*?)>([\S\s]*)<\/script>/, insertedScript))
replaced && (result = result.replace(/<style([^<]*?)>([\S\s]*)<\/style>/, insertedStyle))
return {
code: result,
map: null,
}
},
})
export default viteInsertBody