Skip to content

Commit 71ebe53

Browse files
authored
v2 initial commit
2 parents 52fb941 + 2f29976 commit 71ebe53

38 files changed

Lines changed: 352 additions & 35 deletions

β€ŽREADME.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Codeigniter vite is a package that aims to integrate [vitejs](https://vitejs.dev
2222
- ⏱️ Almost zero configuration
2323
- 🧩 Easy to install and remove
2424
- πŸ”¨ Easy to customize
25-
- ✌️ Support most used frameworks: `react`, `vue`, and `svlete`
25+
- ✌️ Support most used frameworks: `react`, `vue`, `svlete` and **`SvelteKit`** πŸŽ‰
2626
- πŸ”₯ Enjoy hot module replacement (HMR)
2727

2828
## Installation:

β€Žsrc/Commands/Init.phpβ€Ž

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class Init extends BaseCommand
1111
{
12-
protected $group = 'Codeigniter Vite';
12+
protected $group = 'CodeIgniter Vite';
1313
protected $name = 'vite:init';
1414
protected $description = 'Initialize codeigniter vite package';
1515

@@ -18,6 +18,7 @@ class Init extends BaseCommand
1818
*/
1919
private string $framework;
2020

21+
private array $supportedFrameworks = ['none', 'react', 'vue', 'svelte', 'sveltekit'];
2122

2223
/**
2324
* Module path
@@ -34,24 +35,24 @@ public function __construct()
3435
public function run(array $params)
3536
{
3637
# Module start.
37-
CLI::write('Initializing Codeigniter Vite πŸ”₯⚑', 'white', 'cyan');
38+
CLI::write('Initializing Codeigniter Vite Package πŸ”₯⚑', 'white', 'cyan');
3839
CLI::newLine();
3940

4041
# Set framework.
41-
$this->framework = $params['framework'] ?? CLI::prompt('Choose a framework: ', ['none', 'vue', 'react', 'svelte']);
42+
$this->framework = $params['framework'] ?? CLI::prompt('Choose a framework: ', $this->supportedFrameworks);
4243
CLI::newLine();
4344

44-
# First, what if user select a none supported framework ?!
45+
# But, what if user select a none supported framework ?!
4546
# if that's true, return an error message with available frameworks.
46-
if (!in_array($this->framework, ['none', 'vue', 'react', 'svelte']))
47+
if (!in_array($this->framework, $this->supportedFrameworks))
4748
{
4849
CLI::error("❌ Sorry, but $this->framework is not supported!");
49-
CLI::error('Available frameworks are: ' . CLI::color('vue, react and svelte', 'green'));
50+
CLI::error('Available frameworks are: ' . CLI::color(implode(', ', $this->supportedFrameworks), 'green'));
5051
CLI::newLine();
5152
return;
5253
}
5354

54-
# Now let's generate vite necesary files (vite.config.js, package.json & resources direction).
55+
# Now let's generate vite necesary files (vite.config.js, package.json ...etc).
5556
$this->generateFrameworkFiles();
5657

5758
# Update .env file.
@@ -65,31 +66,28 @@ public function run(array $params)
6566
}
6667

6768
/**
68-
* Generate vite files (vite.config.js, package.json & resources)
69+
* Generate vite files (vite.config.js, package.json & resources ...etc)
6970
*
7071
* @return void
7172
*/
7273
private function generateFrameworkFiles()
7374
{
75+
helper('filesystem');
76+
7477
CLI::write('⚑ Generating vite files...', 'yellow');
7578
CLI::newLine();
7679

7780
# Framework files.
78-
$paths = ['vite.config.js', 'package.json', 'resources'];
81+
$frameworkPath = ($this->framework === 'none') ? 'frameworks/default' : "frameworks/$this->framework";
7982

80-
if ($this->framework === 'none')
81-
{
82-
$publisher = new Publisher($this->path . 'Config/default', ROOTPATH);
83-
}
84-
else
85-
{
86-
$publisher = new Publisher($this->path . "Config/$this->framework", ROOTPATH);
87-
}
83+
$frameworkFiles = directory_map($this->path . $frameworkPath, 1, true);
84+
85+
$publisher = new Publisher($this->path . "$frameworkPath", ROOTPATH);
8886

8987
# Publish them.
9088
try
9189
{
92-
$publisher->addPaths($paths)->merge(true);
90+
$publisher->addPaths($frameworkFiles)->merge(true);
9391
}
9492
catch (Throwable $e)
9593
{
@@ -159,6 +157,14 @@ private function updateEnvFile()
159157
$updates = str_replace("VITE_ENTRY_FILE='main.js'", "VITE_ENTRY_FILE='main.jsx'", $envContent);
160158
file_put_contents($envFile, $updates);
161159
}
160+
161+
# SvelteKit src directory.
162+
if ($this->framework === 'sveltekit')
163+
{
164+
$envContent = file_get_contents($envFile);
165+
$updates = str_replace("VITE_RESOURCES_DIR='resources'", "VITE_RESOURCES_DIR='src'", $envContent);
166+
file_put_contents($envFile, $updates);
167+
}
162168
}
163169

164170
# env updated.

β€Žsrc/Commands/Remove.phpβ€Ž

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,27 @@
99

1010
class Remove extends BaseCommand
1111
{
12-
protected $group = 'Codeigniter Vite';
12+
protected $group = 'CodeIgniter Vite';
1313
protected $name = 'vite:remove';
1414
protected $description = 'Remove codeigniter vite generated files and settings';
1515

16+
/**
17+
* Module path
18+
*/
19+
private string $path;
20+
21+
public function __construct()
22+
{
23+
$this->path = service('autoloader')->getNamespace('Mihatori\\CodeigniterVite')[0];
24+
}
25+
1626
public function run(array $params)
1727
{
1828
# cleaning start.
1929
CLI::write('Removing Codeigniter Vite πŸ”₯⚑', 'white', 'red');
2030
CLI::newLine();
2131

22-
# Now let's remove vite files (vite.config.js, package.json & resources direction).
32+
# Now let's remove vite files (vite.config.js, package.json ...etc).
2333
$this->removeFrameworkFiles();
2434

2535
# Reset .env file.
@@ -31,29 +41,45 @@ public function run(array $params)
3141
}
3242

3343
/**
34-
* Remove vite files (vite.config.js, package.json & resources)
44+
* Remove vite files (vite.config.js, package.json ...etc).
3545
*
3646
* @return void
3747
*/
3848
private function removeFrameworkFiles()
3949
{
50+
helper('filesystem');
51+
4052
CLI::write('Removing vite files...', 'yellow');
4153
CLI::newLine();
4254

43-
# First vite.config.js
44-
is_file(ROOTPATH . 'vite.config.js') ? unlink(ROOTPATH . 'vite.config.js') : CLI::error('vite.config.js does not exist');
55+
$framework = env('VITE_FRAMEWORK') ?? 'default';
56+
57+
$frameworkFiles = directory_map($this->path . "frameworks/$framework", 1, true);
4558

46-
# package.json
47-
is_file(ROOTPATH . 'package.json') ? unlink(ROOTPATH . 'package.json') : CLI::error('package.json does not exist');
59+
foreach ($frameworkFiles as $file)
60+
{
61+
# Remove resources|src dir.
62+
if (is_file(ROOTPATH . $file))
63+
{
64+
unlink(ROOTPATH . $file);
65+
}
66+
elseif (is_dir(ROOTPATH . $file))
67+
{
68+
(new Publisher(null, ROOTPATH . $file))->wipe();
69+
}
70+
else
71+
{
72+
CLI::error("$file does not exist");
73+
}
74+
}
4875

4976
# Remove package-lock.json
5077
is_file(ROOTPATH . 'package-lock.json') ? unlink(ROOTPATH . 'package-lock.json') : CLI::error('package-lock.json does not exist');
5178

52-
# Empty resources dir.
53-
if (is_dir(ROOTPATH . 'resources'))
79+
# Just in case user has changed the resources directory.
80+
if (env('VITE_RESOURCES_DIR') && is_dir(ROOTPATH . env('VITE_RESOURCES_DIR')))
5481
{
55-
$publisher = new Publisher(null, ROOTPATH . 'resources');
56-
$publisher->wipe();
82+
(new Publisher(null, ROOTPATH . env('VITE_RESOURCES_DIR')))->wipe();
5783
}
5884
}
5985

β€Žsrc/Decorator.phpβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ public static function decorate(string $html): string
2121
# Close the div
2222
$html = str_replace('</body>', "\n\t</div>\n</body>", $html);
2323

24-
# Get generated css.
24+
# Get bundled assets.
2525
$tags = Vite::tags();
2626

2727
$jsTags = $tags['js'];
2828

2929
# now inject css
30-
if (!empty($tags['css']))
30+
if (!empty($tags['css']) && env('VITE_FRAMEWORK') !== 'sveltekit')
3131
{
3232
$cssTags = $tags['css'];
3333

β€Žsrc/Vite.phpβ€Ž

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,29 @@ public static function tags(): ?array
4848
$fileExtension = substr($file->file, -3, 3);
4949

5050
# Generate js tag.
51-
if ($fileExtension === '.js' && isset($file->isEntry) && $file->isEntry === true)
51+
if ($fileExtension === '.js' && isset($file->isEntry) && $file->isEntry === true && (!isset($file->isDynamicEntry) || $file->isDynamicEntry !== true))
5252
{
53-
$result['js'] .= '<script type="module" src="/' . $file->file . '"></script>';
53+
# SvelteKit requires this small module.
54+
if (env('VITE_FRAMEWORK') == 'sveltekit')
55+
{
56+
$result['js'] = "
57+
<script type='module'>
58+
import { start } from '/" . $file->file . "';
59+
start({
60+
target: document.querySelector('#app'),
61+
paths: {
62+
base: '',
63+
assets: ''
64+
},
65+
route: true,
66+
spa: true,
67+
})
68+
</script>";
69+
}
70+
else
71+
{
72+
$result['js'] .= '<script type="module" src="/' . $file->file . '"></script>';
73+
}
5474
}
5575

5676
if (!empty($file->css))
@@ -69,7 +89,7 @@ public static function tags(): ?array
6989
/**
7090
* Enable HMR for react.
7191
*
72-
* @see https://v2.vitejs.dev/guide/backend-integration.html
92+
* @see https://vitejs.dev/guide/backend-integration.html
7393
*
7494
* @return string|null a simple module script
7595
*/

0 commit comments

Comments
Β (0)