
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
vite-live-preview
Advanced tools
Vite build with preview.
First, please understand that live preview does not support HMR. If you want to use HMR, you'll need to stick to the Vite dev server. This plugin starts a preview server when building the project with file watching. The preview server is augmented to reload the browser on rebuild.
Install this package (and its Vite peer dependency) in your project.
npm install --save-dev vite vite-live-preview
Once installed, you can either use the plugin or the CLI.
Add the plugin to your Vite configuration.
import { defineConfig } from 'vite'
import livePreview from 'vite-live-preview'
export default defineConfig({
plugins: [livePreview({ /* (optional) plugin config */ })],
preview: { /* (optional) preview server config */ },
})
Start a build with file watching.
npx vite build --watch
Any build with file watching enabled will start a preview server.
To only start the preview server when building for development, use the following configuration.
export default defineConfig((env) => {
return {
plugins: [
env.mode === 'development' && livePreview(),
],
},
});
Now, you would need to add the --mode=development
option to the build command for the preview server to start.
npx vite build --watch --mode=development
(Deprecated) You can also use the
--mode=preview
option instead of--watch
. Any mode that starts withpreview
will work. This is legacy behavior, and it will stop working in a future release. Building with--watch
is the recommended way to use this plugin.
reload: boolean
Allow or disable automatic browser reloading on rebuild. Set it to false to require manual browser reloading. The default is true.
config: LivePreviewConfig
Provide configuration overrides that will be deeply merged when live preview is enabled. This can be used to set options like build.mode
, build.sourcemap
, or outDir
for the preview build.
livePreview({
config: {
build: {
mode: 'development',
sourcemap: true,
outDir: 'dist-preview',
},
},
})
plugins: PluginOption[]
Provide plugins that will only be added to the preview server configuration. Only plugins which affect the Vite preview server make sense here.
Plugins in your main (build) configuration are not inherited by the preview server. See also the JavaScript API Limitations section.
Start the live preview.
npx vite-live-preview
--reload [boolean]
Allow or disable automatic browser reloading on rebuild. The default is true.
Use --reload false
to disable automatic reloading. Manually reloading the page after a rebuild will still show the updated content.
All Vite preview command options are also supported.
If you use the JavaScript API build()
command, the preview server will not inherit any plugins that are added "inline".
await build({
plugins: [
// Plugins included in this array are "inline" (not loaded from a
// config file), and will not be inherited by the preview server.
livePreview({
plugins: [
// You can re-add the plugins here if needed, as well as any
// additional preview server specific plugins.
],
}),
],
})
The common convention for plugins is to provide a "factory" function which returns the plugin instance. This allows plugins to accept options, but also provides a closure where a plugin instance can share state between individual hook methods. This is commonly used to save parts of the resolved configuration for use in later hooks, but it can be used for any plugin state.
Unfortunately, the factory return value is added to the plugins array, not the plugin factory functions themselves. So, there is no way to create new instances of the plugins with independent state before passing them to the preview command. Passing the build command's plugin instances to the preview command would result in plugin hooks being called out of order or more times than expected, and any shared state would be shared between the two commands.
The vite -d, --debug [feat]
option is supported, and the following feature names are available for this tool.
live-preview
: General debugging information.live-preview-request
: Log preview server requests.There are cases where the Vite dev server is not adequate. It does not bundle the project, and it relies on the browser to load ES modules directly. This can be slow (especially with "barrel" files), and does not accurately represent the final build.
The Vite preview
command is a good alternative, but it does not do an initial build or automatically rebuild on file changes.
This tool is roughly equivalent to running vite build --watch & vite preview
to start a build (with file watching), and a preview server in parallel. Additionally, browser page reloads will also be triggered after each rebuild.
FAQs
Vite build with preview.
The npm package vite-live-preview receives a total of 3,736 weekly downloads. As such, vite-live-preview popularity was classified as popular.
We found that vite-live-preview demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.