├── .github
└── ISSUE_TEMPLATE
│ ├── bug_report.md
│ └── feature_request.md
├── .gitignore
├── LICENSE
├── README.md
├── SECURITY.md
├── composer.json
└── src
├── Commands
├── Init.php
└── Remove.php
├── Config
├── Registrar.php
├── Routes.php
└── env.default
├── Decorator.php
├── Frameworks
├── default
│ ├── package.json
│ ├── resources
│ │ └── main.js
│ └── vite.config.js
├── react
│ ├── package.json
│ ├── resources
│ │ ├── App.jsx
│ │ ├── main.jsx
│ │ ├── react.svg
│ │ └── style.css
│ └── vite.config.js
├── svelte
│ ├── package.json
│ ├── resources
│ │ ├── App.svelte
│ │ ├── app.css
│ │ ├── main.js
│ │ └── svelte.svg
│ └── vite.config.js
└── vue
│ ├── package.json
│ ├── resources
│ ├── App.vue
│ ├── main.js
│ └── vue.svg
│ └── vite.config.js
├── Helpers
└── vite_helper.php
├── Vite.php
└── logo.png
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: ''
5 | labels: bug
6 | assignees: ''
7 |
8 | ---
9 |
10 | CodeIgniter version:
11 | Package version:
12 |
13 | **Describe the bug**
14 | A clear and concise description of what the bug is.
15 |
16 | **Expected behavior**
17 | A clear and concise description of what you expected to happen.
18 |
19 | **Additional context**
20 | Add any other context about the problem here.
21 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | To suggest a new feature, please feel free to start a new [discussion](https://github.com/firtadokei/codeigniter-vitejs/discussions).
11 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /vendor
2 | composer.lock
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Mihatori Kei
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
21 |
22 | Codeigniter vite is a package that aims to integrate [vitejs](https://vitejs.dev/) with [codeigniter4](https://codeigniter.com/) in a simple way.
23 |
24 | ## Features:
25 | - ⏱️ Almost zero configuration
26 | - 🧩 Easy to install and remove
27 | - 🔨 Easy to customize
28 | - ✌️ Support most used frameworks: `react`, `vue`, and `svlete`. (check [v2](https://github.com/firtadokei/codeigniter-vitejs/tree/v2) for SvelteKit support)
29 | - 🔥 Enjoy hot module replacement (HMR)
30 |
31 | ## Installation:
32 |
33 | ```
34 | composer require mihatori/codeignitervite
35 | ```
36 |
37 | then from your project root, run:
38 |
39 | ```
40 | php spark vite:init --framework
41 | ```
42 |
43 | replace `` with `vue`, `react`, `svelte`, or `none`
44 |
45 | or you can just run:
46 |
47 | ```
48 | php spark vite:init
49 | ```
50 |
51 | our buddy `spark` will handle the rest for you 🙃
52 |
53 | ## Getting Started:
54 | - Install your node dependencies: `npm install`
55 | - Start vite server: `npm run dev`
56 | - Start CI server: `php spark serve` or access it through your virtual host.
57 | - That's all =)
58 |
59 | > **NOTE:**
60 | >
61 | > `npm run dev` is not where you should work, it main purpose is to serve assets, such as scripts or stylesheets.
62 | > once you build your files, it becomes useless
63 | > but as long as it running, the package will use it instead of the bundled files.
64 | > So make sure to **access your project** from ci server or a vitual host.
65 |
66 | ## Build your files:
67 |
68 | to bundle your files, run:
69 | ```
70 | npm run build
71 | ```
72 | this command will generate the bundled assets in your public directory.
73 | but as we said before, as long as vite server is running, the package will use it instead of bundled files, so make sure to stop it when you're done developing.
74 |
75 | ## Uninitialize:
76 |
77 | `composer remove mihatori/codeignitervite` command will remove the package, but the generated files will remain there (package.json, vite.config.js ...etc).
78 | so to avoid that, make sure to run the following command first:
79 |
80 | ```
81 | php spark vite:remove
82 | ```
83 | This command will do the following:
84 | - delete `package.json`, `packages.lock.json` and `vite.config.js`.
85 | - delete `resources` folder.
86 | - And finally restore your `.env` file.
87 |
88 | ## 🔥 Need a quick start?
89 | Check out our starter apps for [svelte](https://github.com/firtadokei/ci-svelte-appstarter) and [vue](https://github.com/firtadokei/ci-vue-appstarter).
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 | ## Contributing
99 | All contributions are welcome, it doesn't matter whether you can code, write documentation, or help find bugs.
100 | feel free to use issues or pull requests.
101 |
102 | ## Support
103 | Unfortunately, I don't drink coffee 💔, but you can star it instead 🙃
104 |
105 | ## License
106 |
107 | MIT License © 2022 [Mihatori Kei](https://github.com/firtadokei)
108 |
--------------------------------------------------------------------------------
/SECURITY.md:
--------------------------------------------------------------------------------
1 | # Security Policy
2 |
3 | ## Reporting a Vulnerability
4 |
5 | If you discover a security vulnerability please email [mihatorikei@gmail.com](mailto:mihatorikei@gmail.com) instead of using the issue tracker.
6 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "mihatori/codeignitervite",
3 | "description": "Integrate viteJs in codeiginter4 framework",
4 | "license": "MIT",
5 | "type": "library",
6 | "homepage": "https://github.com/firtadokei/codeigniter-vitejs",
7 | "keywords": [
8 | "codeigniter",
9 | "codeigniter4",
10 | "codeigniter-viteJs",
11 | "codeigniter-vueJs",
12 | "codeigniter-reactJs",
13 | "codeigniter-svelteJs",
14 | "codeigniter spa"
15 | ],
16 | "authors": [
17 | {
18 | "name": "Mihatori Kei",
19 | "email": "mihatorikei@gmail.com",
20 | "homepage": "https://github.com/firtadokei/"
21 | }
22 | ],
23 | "require": {
24 | "php": "^7.4 || ^8.0"
25 | },
26 | "autoload": {
27 | "psr-4": {
28 | "Mihatori\\CodeigniterVite\\": "src"
29 | }
30 | },
31 | "minimum-stability": "dev",
32 | "prefer-stable": true
33 | }
34 |
--------------------------------------------------------------------------------
/src/Commands/Init.php:
--------------------------------------------------------------------------------
1 | path = service('autoloader')->getNamespace('Mihatori\\CodeigniterVite')[0];
25 | }
26 |
27 | public function run(array $params)
28 | {
29 | # Module start.
30 | CLI::write('Initializing Codeigniter Vite Package 🔥⚡', 'white', 'cyan');
31 | CLI::newLine();
32 |
33 | # Set framework.
34 | $this->framework = $params['framework'] ?? CLI::prompt('Choose a framework: ', $this->supportedFrameworks);
35 | CLI::newLine();
36 |
37 | # But, what if user select a none supported framework ?!
38 | # if that's true, return an error message with available frameworks.
39 | if (!in_array($this->framework, $this->supportedFrameworks)) {
40 | CLI::error("❌ Sorry, but $this->framework is not supported!");
41 | CLI::error('Available frameworks are: ' . CLI::color(implode(', ', $this->supportedFrameworks), 'green'));
42 | CLI::newLine();
43 | return;
44 | }
45 |
46 | # Now let's generate vite necesary files (vite.config.js, package.json ...etc).
47 | $this->generateFrameworkFiles();
48 |
49 | # Update .env file.
50 | $this->updateEnvFile();
51 |
52 | # Everything is ready now.
53 | CLI::write('Codeigniter vite initialized successfully ✅', 'green');
54 | CLI::newLine();
55 | CLI::write('run: npm install && npm run dev');
56 | CLI::newLine();
57 | }
58 |
59 | /**
60 | * Generate vite files (vite.config.js, package.json & resources ...etc)
61 | *
62 | * @return void
63 | */
64 | private function generateFrameworkFiles()
65 | {
66 | helper('filesystem');
67 |
68 | CLI::write('⚡ Generating vite files...', 'yellow');
69 | CLI::newLine();
70 |
71 | # Framework files.
72 | $frameworkPath = ($this->framework === 'none') ? 'Frameworks/default' : "Frameworks/$this->framework";
73 |
74 | $frameworkFiles = directory_map($this->path . $frameworkPath, 1, true);
75 |
76 | $publisher = new Publisher($this->path . $frameworkPath, ROOTPATH);
77 |
78 | # Publish them.
79 | try {
80 | $publisher->addPaths($frameworkFiles)->merge(true);
81 | } catch (Throwable $e) {
82 | $this->showError($e);
83 | return;
84 | }
85 |
86 | CLI::write('Vite files are ready ✅', 'green');
87 | CLI::newLine();
88 | }
89 |
90 | /**
91 | * Set vite configs in .env file
92 | *
93 | * @return void
94 | */
95 | private function updateEnvFile()
96 | {
97 | CLI::write('Updating .env file...', 'yellow');
98 |
99 | # Get the env file.
100 | $envFile = ROOTPATH . '.env';
101 |
102 | # For backup.
103 | $backupFile = is_file($envFile) ? 'env-BACKUP-' . time() : null;
104 |
105 | # Does exist? if not, generate it =)
106 | if (is_file($envFile)) {
107 | # But first, let's take a backup.
108 | copy($envFile, ROOTPATH . $backupFile);
109 |
110 | # Get .env.default content
111 | $content = file_get_contents($this->path . 'Config/env.default');
112 |
113 | # Append it.
114 | file_put_contents($envFile, "\n\n$content", FILE_APPEND);
115 | } else {
116 | # As we said before, generate it.
117 | copy($this->path . 'Config/env.default', ROOTPATH . '.env');
118 | }
119 |
120 | # set the backup name in the current one.
121 | if ($backupFile) {
122 | $envContent = file_get_contents(ROOTPATH . '.env');
123 | $backupUpdate = str_replace('VITE_BACKUP_FILE=', "VITE_BACKUP_FILE='$backupFile'", $envContent);
124 | file_put_contents($envFile, $backupUpdate);
125 | }
126 |
127 | # Define framework.
128 | if ($this->framework !== 'none') {
129 | # Get .env content.
130 | $envContent = file_get_contents($envFile);
131 | # Set framework.
132 | $updates = str_replace("VITE_FRAMEWORK='none'", "VITE_FRAMEWORK='$this->framework'", $envContent);
133 |
134 | file_put_contents($envFile, $updates);
135 |
136 | # React entry file (main.jsx).
137 | if ($this->framework === 'react') {
138 | $envContent = file_get_contents($envFile);
139 | $updates = str_replace("VITE_ENTRY_FILE='main.js'", "VITE_ENTRY_FILE='main.jsx'", $envContent);
140 | file_put_contents($envFile, $updates);
141 | }
142 | }
143 |
144 | # env updated.
145 | CLI::newLine();
146 | CLI::write('.env file updated ✅', 'green');
147 | CLI::newLine();
148 | }
149 | }
150 |
--------------------------------------------------------------------------------
/src/Commands/Remove.php:
--------------------------------------------------------------------------------
1 | path = service('autoloader')->getNamespace('Mihatori\\CodeigniterVite')[0];
24 | }
25 |
26 | public function run(array $params)
27 | {
28 | # cleaning start.
29 | CLI::write('Removing Codeigniter Vite 🔥⚡', 'white', 'red');
30 | CLI::newLine();
31 |
32 | # Now let's remove vite files (vite.config.js, package.json ...etc).
33 | $this->removeFrameworkFiles();
34 |
35 | # Reset .env file.
36 | $this->resetEnvFile();
37 |
38 | # Everything is ready now.
39 | CLI::write('Codeigniter vite has removed successfuly ✅', 'green');
40 | CLI::newLine();
41 | }
42 |
43 | /**
44 | * Remove vite files (vite.config.js, package.json ...etc).
45 | *
46 | * @return void
47 | */
48 | private function removeFrameworkFiles()
49 | {
50 | helper('filesystem');
51 |
52 | CLI::write('Removing vite files...', 'yellow');
53 | CLI::newLine();
54 |
55 | $framework = env('VITE_FRAMEWORK') ?? 'default';
56 |
57 | $frameworkFiles = directory_map($this->path . "frameworks/$framework", 1, true);
58 |
59 | foreach ($frameworkFiles as $file) {
60 | # Remove resources|src dir.
61 | if (is_file(ROOTPATH . $file)) {
62 | unlink(ROOTPATH . $file);
63 | } elseif (is_dir(ROOTPATH . $file)) {
64 | (new Publisher(null, ROOTPATH . $file))->wipe();
65 | } else {
66 | CLI::error("$file does not exist");
67 | }
68 | }
69 |
70 | # Remove package-lock.json
71 | is_file(ROOTPATH . 'package-lock.json') ? unlink(ROOTPATH . 'package-lock.json') : CLI::error('package-lock.json does not exist');
72 |
73 | # Just in case user has changed the resources directory.
74 | if (env('VITE_RESOURCES_DIR') && is_dir(ROOTPATH . env('VITE_RESOURCES_DIR'))) {
75 | (new Publisher(null, ROOTPATH . env('VITE_RESOURCES_DIR')))->wipe();
76 | }
77 | }
78 |
79 | /**
80 | * Remove vite configs in .env file
81 | *
82 | * @return void
83 | */
84 | private function resetEnvFile()
85 | {
86 | CLI::write('Reseting .env file...', 'yellow');
87 | CLI::newLine();
88 |
89 | # Get the env file.
90 | $envFile = ROOTPATH . '.env';
91 | # Get last backup.
92 | $backupFile = ROOTPATH . env('VITE_BACKUP_FILE');
93 |
94 | # Does exist? if not, generate it =)
95 | if (is_file($backupFile)) {
96 | # Remove current .env
97 | unlink($envFile);
98 | # Restore backup if exists
99 | if (is_file($backupFile)) {
100 | copy($backupFile, ROOTPATH . '.env');
101 | unlink($backupFile);
102 | }
103 | }
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/src/Config/Registrar.php:
--------------------------------------------------------------------------------
1 | ['Mihatori\CodeigniterVite\Decorator'],
11 | ];
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/Config/Routes.php:
--------------------------------------------------------------------------------
1 | get('(:any)', 'Home::index');
7 |
--------------------------------------------------------------------------------
/src/Config/env.default:
--------------------------------------------------------------------------------
1 | #--------------------------------------------------------------------
2 | # Codeigniter vite
3 | #--------------------------------------------------------------------
4 |
5 | VITE_AUTO_INJECTING=true
6 | VITE_EXCLUDED_ROUTES=''
7 | VITE_ORIGIN='http://localhost:3479'
8 | VITE_PORT=3479
9 | VITE_BUILD_DIR='build'
10 | VITE_ENTRY_FILE='main.js'
11 | VITE_RESOURCES_DIR='resources'
12 | VITE_FRAMEWORK='none'
13 | VITE_BACKUP_FILE=
14 |
--------------------------------------------------------------------------------
/src/Decorator.php:
--------------------------------------------------------------------------------
1 | ', "\n\t", $html);
21 | # Close the div
22 | $html = str_replace('', "\n\t
\n