vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
68.14k stars 6.15k forks source link

Option to remove build folder when in development mode #6055

Closed hdodov closed 2 years ago

hdodov commented 2 years ago

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 from http://localhost:3000.

Currently, that's not possible because the dist folder is removed only when you re-run the build command. If you run Vite in development mode, that folder remains and this manifest.json check can't work.

Suggested solution

Have a setting in vite.config.js that removes the dist folder always, not just on build. Something like build.clean with possible values:

Alternative

A command line option like --clear-dev could also do the job.

Additional context

No response

Validations

jmartsch commented 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.

koyadume commented 2 years ago

I could not find any option in docs to specify output directory for dev server. How exactly it can be achieved?

bluwy commented 2 years ago

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.