3.0.2 • Published 1 month ago

vite-plugin-browser-sync v3.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

npm JSR Package node-current Coverage Status

vite-plugin-browser-sync

Add BrowserSync in your Vite project.

This plugin supports Vite 4 and 5.

🚀 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',
        }
      }
    })
  ]
}

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.

OptionWhydevbuildWatchpreview
logLevelSet to silent, use ViteJS printUrls to display BrowserSync info✔️✔️✔️
openApply ViteJS open option✔️✔️
codeSyncDisabled because it is already handle by ViteJS✔️
onlineSynced with the server host option✔️✔️

For proxy mode

OptionWhydevbuildWatchpreview
proxy.targetInject the right url from ViteJS✔️✔️
proxy.wsForce websocket proxy to make work ViteJS HMR✔️✔️

For snippet mode

OptionWhy
logSnippetHandle by the plugin so no need to display the snippet
snippetThe snippet injection is handle by the plugin

👨‍💼 Licence

MIT

3.0.2

1 month ago

3.0.1

2 months ago

3.0.0

3 months ago

2.0.1

5 months ago

1.0.7

6 months ago

2.0.0

6 months ago

1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

2 years ago