File tree Expand file tree Collapse file tree 4 files changed +57
-15
lines changed
Expand file tree Collapse file tree 4 files changed +57
-15
lines changed Original file line number Diff line number Diff line change 22# Codeigniter vite
33#--------------------------------------------------------------------
44
5+ VITE_AUTO_INJECTING='true'
56VITE_ORIGIN='http://localhost:3479'
67VITE_PORT=3479
78VITE_BUILD_DIR='build'
89VITE_ENTRY_FILE='main.js'
910VITE_RESOURCES_DIR='resources'
1011VITE_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'
1312VITE_BACKUP_FILE=
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 22
33namespace 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 ' );
You can’t perform that action at this time.
0 commit comments