npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-browser-sync

v4.0.0

Published

Add BrowserSync in your Vite project

Downloads

26,874

Readme

npm JSR Package node-current Coverage Status

vite-plugin-browser-sync

Add BrowserSync in your Vite project.

This plugin supports Vite 5 and 6.

🚀 Features

  • ⚡ Fully integrate in your ViteJS environment
  • 👌 Zero config available for common use cases
  • ✨ All the BrowserSync features
  • 🙌 Support for BrowserSync proxy and snippet mode
  • 🔥 Liberty to manage BrowserSync options
  • 🎛️ Can run on dev, preview or build --watch

📦 Install

npm i -D vite-plugin-browser-sync

# yarn
yarn add -D vite-plugin-browser-sync

# pnpm
pnpm add -D vite-plugin-browser-sync

# bun
bun add -D vite-plugin-browser-sync

👨‍💻 Usage

By default, BrowserSync will start alongside your Vite Server in dev. It uses the proxy mode of BrowserSync based on your Vite server options : no need to pass any options to make it works!

// vite.config.js / vite.config.ts
import VitePluginBrowserSync from 'vite-plugin-browser-sync'

export default {
  plugins: [VitePluginBrowserSync()]
}

If you want to manage BrowserSync or override default behavior of this plugin, you can pass a bs object with your BrowserSync options in it :

// vite.config.js / vite.config.ts
import VitePluginBrowserSync from 'vite-plugin-browser-sync'

export default {
  plugins: [
    VitePluginBrowserSync({
      dev: {
        bs: {
          ui: {
            port: 8080
          },
          notify: false
        }
      }
    })
  ]
}

If you need the snippet mode of BrowserSync, the plugin supports it by injecting the script automatically.

// vite.config.js / vite.config.ts
import VitePluginBrowserSync from 'vite-plugin-browser-sync'

export default {
  plugins: [
    VitePluginBrowserSync({
      dev: {
        mode: 'snippet'
      }
    })
  ]
}

You can also enable the plugin on vite build --watch mode and vite preview mode.

[!IMPORTANT]

  • In buildWatch, if you use the default proxy mode you need to set the bs object.
  • snippet mode is available in buildWatch but it is not recommanded to use since it update your index.html file.
  • In preview, only the proxy mode is supported since it will not inject the snippet.
// vite.config.js / vite.config.ts
import VitePluginBrowserSync from 'vite-plugin-browser-sync'

export default {
  plugins: [
    VitePluginBrowserSync({
      dev: {
        enable: false,
      },
      preview: {
        enable: true,
      },
      buildWatch: {
        enable: true,
        bs: {
          proxy: 'http://localhost:3000',
        }
      }
    })
  ]
}

[!NOTE] For Astro user, this plugin is not working in preview mode because of overrides made by Astro.

vite-plugin-browser-sync options for BrowserSync

This plugin overrides default options from BrowserSync to doesn't duplicate behaviors already handle by ViteJS. Futhermore, your ViteJS config are synced with BrowserSync.

If you want to change the overrided options you free to do so via the bs object.

| Option | Why | dev | buildWatch | preview | |-----------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|:---:|:----------:|:-------:| | logLevel | Set to silent, use ViteJS printUrls to display BrowserSync info | ✔️ | ✔️ | ✔️ | | open | Apply ViteJS open option | ✔️ | | ✔️ | | codeSync | Disabled because it is already handle by ViteJS | ✔️ | | | | online | Synced with the server host option | ✔️ | | ✔️ |

For proxy mode

| Option | Why | dev | buildWatch | preview | |------------------------------------------------------------------|-----------------------------------------------|:---:|------------|:-------:| | proxy.target | Inject the right url from ViteJS | ✔️ | | ✔️ | | proxy.ws | Force websocket proxy to make work ViteJS HMR | ✔️ | | ✔️ |

For snippet mode

| Option | Why | | ------------------------------------------------------------------- | ------------------------------------------------------ | | logSnippet | Handle by the plugin so no need to display the snippet | | snippet | The snippet injection is handle by the plugin |

👨‍💼 Licence

MIT