Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vite Preview Watch Mode #5196

Open
4 tasks done
danwetherald opened this issue Oct 4, 2021 · 22 comments
Open
4 tasks done

Vite Preview Watch Mode #5196

danwetherald opened this issue Oct 4, 2021 · 22 comments
Labels

Comments

@danwetherald
Copy link

Clear and concise description of the problem

When using vite without a index.html file for strictly a "library" or other script where /dis/.... is the only asset in interest, the only way we can currently run this locally is with vite build --watch paired with http-server for instance.

Is there any way to get a local http server for the static /dist assets with a watcher?

Suggested solution

vite preview --watch

Alternative

No response

Additional context

No response

Validations

@mismith
Copy link

mismith commented May 4, 2023

How we're doing it, essentially: vite build --watch & vite preview

Though I agree it would be nice to have a built-in so we could chain args onto that, and avoid & cross-env issues, etc.

@silentmantra
Copy link

I think there should be a live reload also

@bluwy
Copy link
Member

bluwy commented Jul 14, 2023

We have discussed this yesterday and are open to implement vite preview --watch. It would watch the outDir and trigger a page reload via websocket. I suppose we could reuse the dev client.ts for that handling, if it makes it easier to implement.

@silentmantra
Copy link

How we're doing it, essentially: vite build --watch & vite preview

Though I agree it would be nice to have a built-in so we could chain args onto that, and avoid & cross-env issues, etc.

we need a live reload also

@bluwy
Copy link
Member

bluwy commented Jul 14, 2023

Of course, I think it was implied that --watch would trigger that reload.

@Shakeskeyboarde
Copy link
Contributor

I also want this. I think for me it would be a valid alternative to having tree shaking for the dev server (see also #8237). In fact, I already turn off HMR (which I generally am not a fan of) in favor of full page reloads (plugin).

@silentmantra
Copy link

I also want this. I think for me it would be a valid alternative to having tree shaking for the dev server (see also #8237). In fact, I already turn off HMR (which I generally am not a fan of) in favor of full page reloads (plugin).

I'm about to turn HMR off too. It doesn't keep full state of a component (props, provides, parent DOM event listeners and so on). It's ok for CSS styles though.

@SrBrahma
Copy link

SrBrahma commented Dec 7, 2023

This is what is blocking me from migrating from Parcel.

@patricknelson
Copy link

I think vite preview --watch would be fantastic when compiling to iife/umd as well, since there's no obvious way that I can find to easily develop for that output format (it's very esm centric when in dev mode). At least not with Vite on its own.

@patricknelson
Copy link

One potentially suitable alternative that I found for now: alive-server (updated fork of live-server).

If it helps anyone else, here's what I'm doing:

  1. Setup alive-server either globally or as a dev dependency

  2. Separate dev script to build/watch + serve/watch, e.g. where build:iife is my alias to build the alternate

    {
      "scripts": {
        "preview:watch": "vite build --watch & alive-server --port=4173 dist/",
      }
    }

I figured port 4173 would make it a good drop in replacement for vite preview at http://localhost:4173. 🙂

@antoniogiroz
Copy link

That is my workaround using concurrently:

{
  "scripts": {
    "preview:watch": "concurrently \"vite preview -l silent\" \"vite build --watch\"",
  }
}

@acupofspirt
Copy link

acupofspirt commented Mar 26, 2024

Vite has JavaScript API, so it is possible to run both build --watch and preview as a single command:

dev.js

import { build, preview } from 'vite';

let previewServer;

build({ build: { watch: {} } }).then((buildWatcher) => {
  buildWatcher.on('event', async ({ code }) => {
    if (code === 'END') {
      previewServer = previewServer || (await preview());

      console.log('\n');
      previewServer.printUrls();
    }
  });
});

package.json

{
  "scripts": {
    "dev": "node dev"
  }
}

@ccaspanello
Copy link

@acupofspirt - This is a great suggestion. How can this be configured to pass in a --port XXXX via the package.json script? This would be useful for micro frontends.

@acupofspirt
Copy link

acupofspirt commented May 9, 2024

@ccaspanello Smth like this

import minimist from 'minimist';
import { build, preview } from 'vite';

const { port } = minimist(process.argv.slice(2))

let previewServer;

build({ build: { watch: {} } }).then((buildWatcher) => {
  buildWatcher.on('event', async ({ code }) => {
    if (code === 'END') {
      previewServer = previewServer || (await preview({preview: { port }}));

      console.log('\n');
      previewServer.printUrls();
    }
  });
});

and

{
  "scripts": {
    "dev": "node dev --port 5555"
  }
}

@animeshk874
Copy link

Is there any update on this? It would be really useful to have the vite preview --watch command shipped out of the box. As @patricknelson mentioned, it'd make iife/umd dev experience smoother and faster

@Shakeskeyboarde
Copy link
Contributor

Shakeskeyboarde commented Jun 12, 2024

While I still think it would be useful to add --watch to preview, I realized that at the moment, the preview command has a single responsibility (starting a server) that does not include building. That does complicate adding the option to the preview command, because it should probably also include adding build specific command line options, and the --watch option would imply building, where the preview command currently does not. It might actually be better to add a --preview option to the build command, so vite build --preview instead of vite preview --watch.

So, I took the Vite API solution @acupofspirt posted, added auto page reloading, and turned it into a separate tool as a stand-in for the (hopefully) future vite preview --watch or vite build --preview command.

Hope this helps some of you. Feel free to post issues on the repo or contribute.

@nChauhan91
Copy link

nChauhan91 commented Jun 17, 2024

So, I took the Vite API solution @acupofspirt posted, added auto page reloading, and turned it into a separate tool as a stand-in for the (hopefully) future vite preview --watch or vite build --preview command.

* https://www.npmjs.com/package/vite-live-preview

* https://github.com/Shakeskeyboarde/vite-live-preview

Hope this helps some of you. Feel free to post issues on the repo or contribute.

I do get this output about the page reloading but the page doesn't reload & has to be reloaded manually

`9:31:16 PM [vite] page-reload

9:31:16 PM [vite] preview server ready`

@Shakeskeyboarde
Copy link
Contributor

So, I took the Vite API solution @acupofspirt posted, added auto page reloading, and turned it into a separate tool as a stand-in for the (hopefully) future vite preview --watch or vite build --preview command.

* https://www.npmjs.com/package/vite-live-preview

* https://github.com/Shakeskeyboarde/vite-live-preview

Hope this helps some of you. Feel free to post issues on the repo or contribute.

I do get this output about the page reloading but the page doesn't reload & has to be reloaded manually

`9:31:16 PM [vite] page-reload

9:31:16 PM [vite] preview server ready`

There was a bug. Try updating to the latest version (0.1.8).

@nChauhan91
Copy link

@Shakeskeyboarde I used (0.1.8) only, I again cross-checked by re-installing. The build is successful & the output is in the console [vite] page-reload but the page must be reloaded manually.

@Shakeskeyboarde
Copy link
Contributor

@Shakeskeyboarde I used (0.1.8) only, I again cross-checked by re-installing. The build is successful & the output is in the console [vite] page-reload but the page must be reloaded manually.

Okay. Could you report a bug on the repo please? Include the browser, os, package manager, and a minimal repro if you can.

@nChauhan91
Copy link

@ahmed-hmaidat
Copy link

Fixed it for me

export CHOKIDAR_USEPOLLING=true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests