如何在传统的 PHP 站点运行 Vite 示例
This is an example code, simulating how to run Vite on traditional PHP sites.
A bare-minimum setup to serve as example to adapt to other scenarios (WordPress, Laravel, etc).
For everything else beyond the PHP side please refer to the Vite documentation, which is great.
A characteristic of this setup is that you'll run your project from your own local server, for exemple http://vite-php-setup.test. Vite will be running at http://localhost:{your_port_number} where our script and styles will be served from, but accesing http://localhost:{your_port_number} directly will be empty, which is fine.
Of course, HMR and styles will work just fine! And fast!
A limitation is Vite's port during development, PHP helpers must match the one that was created during "npm run dev" (default 5173). For example, if the port 5173 is in use, Vite will try the next one (5174 and so on), so our helper PHP wouldn't know about that.
The solution is to stricly specify which port to use, and match the PHP side to the same port. Check vite.config.js for example.
Image urls within CSS works fine BUT you need to create a symlink on dev server to map to your assets folder. This is an expected limitation as noted on Vite docs
The solution is here, adjust the paths and run in terminal:
ln -s {path_to_project_source}/src/assets {path_to_public_html}/assets
Note: this happens because our Vite code is outside the server public access, if it where, we could use server.origin config.
You may find the need to handle multiple entries, for example, one js/css for the backend and another js/css for frontend. For that, it depends directly on how you want to organize your code.
So you can have:
To get this running on Docker quickly, check out this fork. Thanks @mrothauer
暂无开放 Issues,或尚未同步最近议题。