Skip to content

Commit d07c9db

Browse files
committed
feat: support tags
1 parent 0dbcb20 commit d07c9db

File tree

21 files changed

+248
-45
lines changed

21 files changed

+248
-45
lines changed

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- name: Install pnpm
2424
uses: pnpm/[email protected]
2525
with:
26-
version: 6.28.0
26+
version: 6.31.0
2727

2828
- name: Use Node.js ${{ matrix.node-version }}
2929
uses: actions/setup-node@v2

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ export default defineConfig({
7272
title: 'index',
7373
injectScript: `<script src="./inject.js"></script>`,
7474
},
75+
tags: [
76+
{
77+
injectTo: 'body-prepend',
78+
tag: 'div',
79+
attrs: {
80+
id: 'tag',
81+
},
82+
},
83+
],
7584
},
7685
}),
7786
],
@@ -98,6 +107,15 @@ export default defineConfig({
98107
title: 'index',
99108
injectScript: `<script src="./inject.js"></script>`,
100109
},
110+
tags: [
111+
{
112+
injectTo: 'body-prepend',
113+
tag: 'div',
114+
attrs: {
115+
id: 'tag1',
116+
},
117+
},
118+
],
101119
},
102120
},
103121
{
@@ -109,6 +127,15 @@ export default defineConfig({
109127
title: 'other page',
110128
injectScript: `<script src="./inject.js"></script>`,
111129
},
130+
tags: [
131+
{
132+
injectTo: 'body-prepend',
133+
tag: 'div',
134+
attrs: {
135+
id: 'tag2',
136+
},
137+
},
138+
],
112139
},
113140
},
114141
],
@@ -137,6 +164,7 @@ export default defineConfig({
137164
| ---------- | --------------------- | ------- | ------------------------------------------------------------------------- |
138165
| data | `Record<string, any>` | - | injected data |
139166
| ejsOptions | `EJSOptions` | - | ejs configuration Options[EJSOptions](https://github.com/mde/ejs#options) |
167+
| tags | `HtmlTagDescriptor` | - | List of tags to inject |
140168

141169
`data` can be accessed in `html` using the `ejs` template syntax
142170

README.zh_CN.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ export default defineConfig({
7474
title: 'index',
7575
injectScript: `<script src="./inject.js"></script>`,
7676
},
77+
tags: [
78+
{
79+
injectTo: 'body-prepend',
80+
tag: 'div',
81+
attrs: {
82+
id: 'tag',
83+
},
84+
},
85+
],
7786
},
7887
}),
7988
],
@@ -100,6 +109,15 @@ export default defineConfig({
100109
title: 'index',
101110
injectScript: `<script src="./inject.js"></script>`,
102111
},
112+
tags: [
113+
{
114+
injectTo: 'body-prepend',
115+
tag: 'div',
116+
attrs: {
117+
id: 'tag1',
118+
},
119+
},
120+
],
103121
},
104122
},
105123
{
@@ -111,6 +129,15 @@ export default defineConfig({
111129
title: 'other page',
112130
injectScript: `<script src="./inject.js"></script>`,
113131
},
132+
tags: [
133+
{
134+
injectTo: 'body-prepend',
135+
tag: 'div',
136+
attrs: {
137+
id: 'tag2',
138+
},
139+
},
140+
],
114141
},
115142
},
116143
],
@@ -139,6 +166,7 @@ export default defineConfig({
139166
| ---------- | --------------------- | ------ | ---------------------------------------------------------- |
140167
| data | `Record<string, any>` | - | 注入的数据 |
141168
| ejsOptions | `EJSOptions` | - | ejs 配置项[EJSOptions](https://github.com/mde/ejs#options) |
169+
| tags | `HtmlTagDescriptor` | - | 需要注入的标签列表 |
142170

143171
`data` 可以在 `html` 中使用 `ejs` 模版语法获取
144172

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@commitlint/config-conventional": "^16.2.1",
2828
"@types/html-minifier-terser": "^6.1.0",
2929
"@types/jsdom": "^16.2.14",
30-
"@types/node": "^17.0.19",
30+
"@types/node": "^17.0.21",
3131
"@typescript-eslint/eslint-plugin": "^5.12.1",
3232
"@typescript-eslint/parser": "^5.12.1",
3333
"commitizen": "^4.2.4",
@@ -44,7 +44,7 @@
4444
"typescript": "^4.5.5",
4545
"unbuild": "^0.6.9",
4646
"vite": "^2.8.4",
47-
"vitest": "^0.5.1"
47+
"vitest": "^0.5.5"
4848
},
4949
"lint-staged": {
5050
"*": [

packages/core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"dotenv-expand": "^8.0.1",
5151
"ejs": "^3.1.6",
5252
"fast-glob": "^3.2.11",
53-
"fs-extra": "^10.0.0",
53+
"fs-extra": "^10.0.1",
5454
"html-minifier-terser": "^6.1.0",
5555
"node-html-parser": "^5.2.0",
5656
"pathe": "^0.2.0"
@@ -62,7 +62,7 @@
6262
"@types/ejs": "^3.1.0",
6363
"@types/fs-extra": "^9.0.13",
6464
"@types/html-minifier-terser": "^6.1.0",
65-
"@types/node": "^17.0.19",
65+
"@types/node": "^17.0.21",
6666
"typescript": "^4.5.5",
6767
"vite": "^2.8.4"
6868
}

packages/core/src/htmlPlugin.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,17 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption {
6060
const url = cleanUrl(req.url || '')
6161
const base = viteConfig.base
6262
const excludeBaseUrl = url.replace(base, '/')
63-
6463
if (!htmlFilter(url) && excludeBaseUrl !== '/') {
6564
return next()
6665
}
67-
6866
try {
6967
const htmlName =
7068
excludeBaseUrl === '/' ? DEFAULT_TEMPLATE : url.replace('/', '')
7169

7270
const page = getPage(userOptions, htmlName, viteConfig)
7371

74-
const { injectOptions = {} } = page
7572
let html = await getHtmlInPages(page, viteConfig.root)
7673

77-
html = await renderHtml(html, {
78-
injectOptions,
79-
viteConfig,
80-
env,
81-
entry: page.entry || entry,
82-
verbose,
83-
})
8474
if (server.transformIndexHtml) {
8575
html = await server.transformIndexHtml(url, html, req.originalUrl)
8676
}
@@ -90,6 +80,30 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption {
9080
}
9181
})
9282
},
83+
84+
transformIndexHtml: {
85+
enforce: 'pre',
86+
async transform(html, ctx) {
87+
const url = ctx.filename
88+
const base = viteConfig.base
89+
const excludeBaseUrl = url.replace(base, '/')
90+
const htmlName = path.relative(process.cwd(), excludeBaseUrl)
91+
const page = getPage(userOptions, htmlName, viteConfig)
92+
const { injectOptions = {} } = page
93+
const _html = await renderHtml(html, {
94+
injectOptions,
95+
viteConfig,
96+
env,
97+
entry: page.entry || entry,
98+
verbose,
99+
})
100+
const { tags = [] } = injectOptions
101+
return {
102+
html: _html,
103+
tags: tags,
104+
}
105+
},
106+
},
93107
transform(code, id): Promise<TransformResult> | TransformResult {
94108
if (viteConfig.command === 'build' && htmlFilter(id)) {
95109
const htmlName = id

packages/core/src/typing.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Options as EJSOptions } from 'ejs'
22
import type { Options as MinifyOptions } from 'html-minifier-terser'
3+
import type { HtmlTagDescriptor } from 'vite'
34

45
export type Entry = string | Record<string, string>
56

@@ -9,6 +10,8 @@ export interface InjectOptions {
910
*/
1011
data?: Record<string, any>
1112

13+
tags?: HtmlTagDescriptor[]
14+
1215
/**
1316
* @description esj options configuration
1417
*/

packages/core/src/utils/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ export function loadEnv(
2626
for (const file of envFiles) {
2727
const path = lookupFile(envDir, [file], true)
2828
if (path) {
29-
const parsed = dotenv.parse(fse.readFileSync(path), {
30-
debug: !!process.env.DEBUG || undefined,
31-
})
29+
const parsed = dotenv.parse(fse.readFileSync(path))
3230

3331
// let environment variables use each other
3432
expand({
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" href="/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>test-<%- title %></title>
8+
</head>
9+
10+
<body>
11+
<div><%- ENV %></div>
12+
<div><%- NODE_ENV %></div>
13+
<div id="app"></div>
14+
<script type="module" src="/src/main.ts"></script>
15+
<%- injectScript %>
16+
</body>
17+
</html>

packages/playground/basic/vite.config.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ export default defineConfig({
77
vue(),
88
createHtmlPlugin({
99
minify: true,
10-
entry: 'src/main.ts',
11-
/**
12-
* @default index.html
13-
*/
14-
template: 'public/index.html',
1510
inject: {
1611
data: {
1712
title: 'index',
1813
injectScript: `<script src="./inject.js"></script>`,
1914
},
15+
tags: [
16+
{
17+
tag: 'div',
18+
attrs: { id: 'ddd' },
19+
injectTo: 'body-prepend',
20+
},
21+
],
2022
},
2123
}),
2224
],

0 commit comments

Comments
 (0)