Skip to content

Commit 31fb197

Browse files
committed
fix #12
1 parent e0dedfb commit 31fb197

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

src/Decorator.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,28 @@ public static function decorate(string $html): string
1111
# Check if vite is running or manifest is ready.
1212
if (Vite::isReady() && env('VITE_AUTO_INJECTING'))
1313
{
14-
# Get generated js and css tags.
14+
# First inject app div
15+
$html = str_replace('<body>', "<body>\n\t<div id=\"app\">", $html);
16+
# Close the div
17+
$html = str_replace('</body>', "\n\t</div>\n</body>", $html);
18+
19+
# Get generated css.
1520
$tags = Vite::tags();
1621

17-
$findAndReplace = [
18-
# Generated js and css tags.
19-
'</head>' => "\n\t$tags\n</head>",
20-
# app div
21-
'<body>' => "<body>\n\t<div id=\"app\">",
22-
# Closing app div.
23-
'</body>' => "\n\t</div>\n</body>"
24-
];
25-
26-
# Insert tags just before "</head>" tag and a div with "app" id
27-
$html = str_replace(array_keys($findAndReplace), array_values($findAndReplace), $html);
22+
$jsTags = $tags['js'];
23+
24+
# now inject css
25+
if (!empty($tags['css']))
26+
{
27+
$cssTags = $tags['css'];
28+
29+
$html = str_replace('</head>', "\n\t$cssTags\n", $html);
30+
$html = str_replace('</body>', "\n\t$jsTags\n</body>", $html);
31+
}
32+
else
33+
{
34+
$html = str_replace('</head>', "\n\t$jsTags\n</head>", $html);
35+
}
2836
}
2937

3038
return $html;

src/Helpers/vite_helper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
/**
66
* Get vite entry file or bundled files.
77
*
8-
* @return string|null
8+
* @return array|null
99
*/
10-
function viteTags(): ?string
10+
function viteTags(): ?array
1111
{
1212
return Vite::tags();
1313
}

src/Vite.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,32 @@ class Vite
1313
/**
1414
* Get vite entry file on running or bundled files instead.
1515
*
16-
* @return string single script tag on developing and much more on production
16+
* @return array single script tag on developing and much more on production
1717
*/
18-
public static function tags(): ?string
18+
public static function tags(): ?array
1919
{
20+
$result = [
21+
'js' => null,
22+
'css' => null
23+
];
24+
2025
# Check if vite is running.
2126
$entryFile = env('VITE_ORIGIN') . '/' . env('VITE_RESOURCES_DIR') . '/' . env('VITE_ENTRY_FILE');
2227

23-
$result = @file_get_contents($entryFile) ? '<script type="module" src="' . $entryFile . '"></script>' : null;
28+
$result['js'] = @file_get_contents($entryFile) ? '<script type="module" src="' . $entryFile . '"></script>' : null;
2429

2530
# React HMR fix.
26-
if (!empty($result))
31+
if (!empty($result['js']))
2732
{
28-
$result = self::getReactTag() . "$result";
33+
$result['js'] = self::getReactTag() . $result['js'];
2934
}
3035

31-
# If vite isn't running, then return the compiled resources.
32-
if (empty($result) && is_file(self::$manifest))
36+
# If vite isn't running, then return the bundled resources.
37+
if (empty($result['js']) && is_file(self::$manifest))
3338
{
3439
# Get the manifest content.
3540
$manifest = file_get_contents(self::$manifest);
36-
# You look much pretty as an php object =).
41+
# You look much pretty as a php object =).
3742
$manifest = json_decode($manifest);
3843

3944
# Now, we will get all js files and css from the manifest.
@@ -45,14 +50,14 @@ public static function tags(): ?string
4550
# Generate js tag.
4651
if ($fileExtension === '.js' && isset($file->isEntry) && $file->isEntry === true)
4752
{
48-
$result .= '<script type="module" src="/' . $file->file . '"></script>';
53+
$result['js'] .= '<script type="module" src="/' . $file->file . '"></script>';
4954
}
5055

5156
if (!empty($file->css))
5257
{
5358
foreach ($file->css as $cssFile)
5459
{
55-
$result .= '<link rel="stylesheet" href="/' . $cssFile . '" />';
60+
$result['css'] .= '<link rel="stylesheet" href="/' . $cssFile . '" />';
5661
}
5762
}
5863
}

0 commit comments

Comments
 (0)