Skip to content

Commit 453ca1a

Browse files
authored
Merge pull request #8 from firtadokei/dev
- Add some helpers - auto injecting is now optional
2 parents 433ce65 + b0ad3bb commit 453ca1a

File tree

4 files changed

+57
-15
lines changed

4 files changed

+57
-15
lines changed

src/Config/env.default

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
# Codeigniter vite
33
#--------------------------------------------------------------------
44

5+
VITE_AUTO_INJECTING='true'
56
VITE_ORIGIN='http://localhost:3479'
67
VITE_PORT=3479
78
VITE_BUILD_DIR='build'
89
VITE_ENTRY_FILE='main.js'
910
VITE_RESOURCES_DIR='resources'
1011
VITE_FRAMEWORK='none'
11-
# Set this to false, or otherwise the plugin will try to add a div with an app id in your view file
12-
VITE_ADD_APP_ID='true'
1312
VITE_BACKUP_FILE=

src/Decorator.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@ class Decorator implements ViewDecoratorInterface
99
public static function decorate(string $html): string
1010
{
1111
# Check if vite is running or manifest is ready.
12-
if (CodeigniterVite::check())
12+
if (Vite::isReady() && env('VITE_AUTO_INJECTING'))
1313
{
1414
# Get generated js and css tags.
15-
$tags = CodeigniterVite::tags();
15+
$tags = Vite::tags();
1616

17-
# Insert tags just before "</head>" tag.
18-
$html = empty($tags) ? $html : str_replace('</head>', "\n\t$tags\n</head>", $html);
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+
];
1925

20-
if (env('VITE_ADD_APP_ID') == "true")
21-
{
22-
# Insert app id just after body tag
23-
$html = empty($tags) ? $html : str_replace('<body>', "<body>\n\t<div id=\"app\">", $html);
24-
# Close it.
25-
$html = empty($tags) ? $html : str_replace('</body>', "\n\t</div>\n</body>", $html);
26-
}
26+
# Insert tags just before "</head>" tag and a div with "app" id
27+
$html = str_replace(array_keys($findAndReplace), array_values($findAndReplace), $html);
2728
}
2829

2930
return $html;

src/Helpers/vite_helper.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use Mihatori\CodeigniterVite\Vite;
4+
5+
/**
6+
* Get vite entry file or bundled files.
7+
*
8+
* @return string|null
9+
*/
10+
function viteTags(): ?string
11+
{
12+
return Vite::tags();
13+
}
14+
15+
/**
16+
* Get react module
17+
*
18+
* @return string|null
19+
*/
20+
function getReactModule(): ?string
21+
{
22+
return vite::getReactTag();
23+
}
24+
25+
/**
26+
* @return bool true if vite is running
27+
*/
28+
function viteIsRunning(): bool
29+
{
30+
$entryFile = env('VITE_ORIGIN') . '/' . env('VITE_RESOURCES_DIR') . '/' . env('VITE_ENTRY_FILE');
31+
return (bool) @file_get_contents($entryFile);
32+
}
33+
34+
/**
35+
* Get vite framework
36+
*
37+
* @return string react, vue, svelte or none
38+
*/
39+
function viteFramework()
40+
{
41+
return env('VITE_FRAMEWORK');
42+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Mihatori\CodeigniterVite;
44

5-
class CodeigniterVite
5+
class Vite
66
{
77

88
/**
@@ -79,7 +79,7 @@ public static function getReactTag(): ?string
7979
*
8080
* @return bool true if vite is runnig or if manifest does exist, otherwise false;
8181
*/
82-
public static function check(): bool
82+
public static function isReady(): bool
8383
{
8484
# Check if vite is running.
8585
$entryFile = env('VITE_ORIGIN') . '/' . env('VITE_RESOURCES_DIR') . '/' . env('VITE_ENTRY_FILE');

0 commit comments

Comments
 (0)