Closed hdodov closed 2 years ago
Hello @hdodov. You can use https://github.com/andrefelipe/vite-php-setup. I modified the isDev function so that it checks for a manifest file.
$isDevelopment = count(getManifest()) === 0;
function getManifest(): array
{
if (!is_file(__DIR__ . '/assets/js/manifest.json')) return array();
$content = file_get_contents(__DIR__ . '/assets/js/manifest.json');
return json_decode($content, true);
}
However, the manifest file should be deleted, if you start a dev server, so the new files are being used, as you wrote. For that I use two scripts (in the scripts section) in my package.json:
"scripts": {
"del": "npx rimraf dist/site/templates/assets/js/manifest.json",
"dev": "npm run del && cross-env NODE_ENV=development vite",
"build": "npm run del && gulp && cross-env NODE_ENV=production vite build"
},
I agree with you, that a native option for this would be great and is needed.
I could not find any option in docs to specify output directory for dev server. How exactly it can be achieved?
The dev server doesn't output any files, so there's isn't anything to clear, so I'm a bit confused here. If the dist/manifest.json
is generated somewhere externally, perhaps you can write a Vite plugin to auto delete that too. Closing as this is a userland issue.
Clear and concise description of the problem
I use Vite with PHP and I need some mechanism to check whether there's a dev server currently running. I can ping
http://localhost:3000
with curl from within PHP, but I believe that's inefficient.I think it'll be better to check for the existence of
dist/manifest.json
, since it has to be used anyways. If it exists, then just load the production files listed inside. Otherwise, consider that Vite is running in development mode and request the assets fromhttp://localhost:3000
.Currently, that's not possible because the
dist
folder is removed only when you re-run thebuild
command. If you run Vite in development mode, that folder remains and thismanifest.json
check can't work.Suggested solution
Have a setting in
vite.config.js
that removes thedist
folder always, not just onbuild
. Something likebuild.clean
with possible values:"always"
: Remove the folder regardless of mode (solves this use case)."production"
: Current behavior.false
: Never clean the folder. Can be useful for cases like this https://github.com/vuejs/vue-cli/issues/1446.Alternative
A command line option like
--clear-dev
could also do the job.Additional context
No response
Validations