forked from LizardByte/Sunshine
-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathvite-plugin-ejs-v7.js
More file actions
59 lines (54 loc) · 1.69 KB
/
vite-plugin-ejs-v7.js
File metadata and controls
59 lines (54 loc) · 1.69 KB
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
import ejs from 'ejs'
/**
* Vite EJS Plugin for Vite 7
* Compatible replacement for vite-plugin-ejs that works with Vite 7's new transformIndexHtml API
* @param {Record<string, any> | ((config: any) => Record<string, any>)} data - Data to pass to EJS template
* @param {object} options - Optional EJS options
* @returns {import('vite').Plugin}
*/
export function ViteEjsPlugin(data = {}, options = {}) {
let config
return {
name: 'vite-plugin-ejs-v7',
// Get Resolved config
configResolved(resolvedConfig) {
config = resolvedConfig
},
transformIndexHtml: {
// Use 'pre' order to ensure EJS is processed before other HTML transformations
order: 'pre',
// Vite 7 uses 'handler' instead of 'transform'
handler(html) {
// Resolve data if it's a function
const resolvedData = typeof data === 'function' ? data(config) : data
// Resolve EJS options if it's a function
let ejsOptions = options && options.ejs ? options.ejs : {}
if (typeof ejsOptions === 'function') {
ejsOptions = ejsOptions(config)
}
// Render EJS template with data
const rendered = ejs.render(
html,
Object.assign(
{
NODE_ENV: config.mode,
isDev: config.mode === 'development',
},
resolvedData
),
Object.assign(
{
// Setting views enables includes support
views: [config.root],
},
ejsOptions,
{
async: false, // Force sync
}
)
)
return rendered
},
},
}
}