-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathVite.php
More file actions
129 lines (107 loc) · 3.88 KB
/
Vite.php
File metadata and controls
129 lines (107 loc) · 3.88 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace Mihatori\CodeigniterVite;
class Vite
{
/**
* @var string manifest path.
*/
private static $manifest = FCPATH . 'manifest.json';
/**
* Get vite entry file on running or bundled files instead.
*
* @return array single script tag on developing and much more on production
*/
public static function tags(): ?array
{
$result = [
'js' => null,
'css' => null
];
# Check if vite is running.
$entryFile = env('VITE_ORIGIN') . '/' . env('VITE_RESOURCES_DIR') . '/' . env('VITE_ENTRY_FILE');
$result['js'] = @file_get_contents($entryFile) ? '<script type="module" src="' . $entryFile . '"></script>' : null;
# React HMR fix.
if (!empty($result['js']))
{
$result['js'] = self::getReactTag() . $result['js'];
}
# If vite isn't running, then return the bundled resources.
if (empty($result['js']) && is_file(self::$manifest))
{
# Get the manifest content.
$manifest = file_get_contents(self::$manifest);
# You look much pretty as a php object =).
$manifest = json_decode($manifest);
# Now, we will get all js files and css from the manifest.
foreach ($manifest as $file)
{
# Check extension
$fileExtension = substr($file->file, -3, 3);
# Generate js tag.
if ($fileExtension === '.js' && isset($file->isEntry) && $file->isEntry === true && (!isset($file->isDynamicEntry) || $file->isDynamicEntry !== true))
{
$result['js'] .= '<script type="module" src="/' . $file->file . '"></script>';
}
if (!empty($file->css))
{
foreach ($file->css as $cssFile)
{
$result['css'] .= '<link rel="stylesheet" href="/' . $cssFile . '" />';
}
}
}
}
return $result;
}
/**
* Enable HMR for react.
*
* @see https://vitejs.dev/guide/backend-integration.html
*
* @return string|null a simple module script
*/
public static function getReactTag(): ?string
{
if (env('VITE_FRAMEWORK') === 'react')
{
$origin = env('VITE_ORIGIN');
$result = "<script type=\"module\">import RefreshRuntime from '$origin/@react-refresh';RefreshRuntime.injectIntoGlobalHook(window);window.\$RefreshReg\$ = () => {};window.\$RefreshSig\$ = () => (type) => type;window.__vite_plugin_react_preamble_installed__ = true;</script>";
return "$result\n\t";
}
return null;
}
/**
* Check whether vite is running or manifest does exist.
*
* @return bool true if vite is runnig or if manifest does exist, otherwise false;
*/
public static function isReady(): bool
{
$entryFile = env('VITE_ORIGIN') . '/' . env('VITE_RESOURCES_DIR') . '/' . env('VITE_ENTRY_FILE');
switch (true)
{
case @file_get_contents($entryFile):
$result = true;
break;
case is_file(self::$manifest):
$result = true;
break;
default:
$result = false;
}
return $result;
}
/**
* Check whether the current route is exluded or not.
*
* @return bool
*/
public static function routeIsNotExluded(): bool
{
$routes = explode(',', env('VITE_EXCLUDED_ROUTES'));
# remove spaces before and after the route.
// foreach($routes as $i => $route) $routes[$i] = ltrim( rtrim($route) );
$routes = array_filter($routes, function ($route) { return $route !== ""; });
return !in_array(uri_string(), $routes);
}
}