-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite-plugin-codeigniter.js
More file actions
72 lines (57 loc) · 2.16 KB
/
vite-plugin-codeigniter.js
File metadata and controls
72 lines (57 loc) · 2.16 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
60
61
62
63
64
65
66
67
68
69
70
71
72
// vite-pugglin.js
import fs from 'fs';
import path from 'path';
export default function vitePugglin(options = {}) {
const {
hotFile = '.vite/hot',
outputFile = 'public/vite.php',
publicPath = '/build/',
} = options;
return {
name: 'vite-pugglin',
configureServer(server) {
const hotFilePath = path.resolve(hotFile);
fs.mkdirSync(path.dirname(hotFilePath), { recursive: true });
fs.writeFileSync(hotFilePath, ''); // Create empty .vite/hot
server.httpServer.once('close', () => {
if (fs.existsSync(hotFilePath)) {
fs.unlinkSync(hotFilePath); // Remove .vite/hot on server stop
}
});
console.log(`vite-pugglin: Dev mode detected. Created ${hotFile}`);
},
closeBundle() {
const hotFilePath = path.resolve(hotFile);
if (fs.existsSync(hotFilePath)) {
fs.unlinkSync(hotFilePath); // Ensure hot file is removed in production
}
const manifestPath = path.resolve('public/build/manifest.json');
if (!fs.existsSync(manifestPath)) {
console.warn('vite-pugglin: No manifest found.');
return;
}
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
const assets = [];
for (const file in manifest) {
const entry = manifest[file];
if (entry.file) {
assets.push(publicPath + entry.file);
}
if (entry.css) {
entry.css.forEach(cssFile => {
assets.push(publicPath + cssFile);
});
}
}
const phpArray = "<?php\nreturn " + varExport(assets) + ";\n";
fs.writeFileSync(outputFile, phpArray);
console.log(`vite-pugglin: Production assets written to ${outputFile}`);
}
};
}
function varExport(arr) {
return JSON.stringify(arr, null, 4)
.replace(/"/g, "'")
.replace(//g, "[")
.replace(//g, "]");
}