-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
51 lines (46 loc) · 1.21 KB
/
vite.config.js
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
import { defineConfig } from 'vite'
import posthtml from 'posthtml'
import { glob } from 'glob'
import fs from 'node:fs'
const components = {};
const allFiles = glob.sync('src/**/*.*')
allFiles.forEach(file => {
const filename = file.split('\\').pop().split('/').pop()
const start = filename.substring(0, filename.indexOf('.'));
const ending = filename.substring(filename.indexOf('.'));
if (ending === '.blade.php') {
components[start] = fs.readFileSync(file, 'utf8')
}
});
const plugin = function (tree) {
for (const [key, value] of Object.entries(components)) {
tree.match({ tag: 'x-'+key }, function (node) {
return value;
})
}
return tree;
}
function bladeTemplatesPlugin() {
return {
name: 'blade-templates-plugin',
transformIndexHtml: {
enforce: 'pre',
async transform(html) {
const { html: transformedHtml } = await posthtml(plugin).process(html);
return transformedHtml;
}
},
configureServer(server) {
server.watcher.on('change', (filePath) => {
if (filePath.endsWith('.blade.php')) {
server.restart()
}
});
}
}
}
export default defineConfig({
plugins: [
bladeTemplatesPlugin(),
]
})