BrowserSync and Webpack integration plugin for live reloading and synchronization during development
npx @tessl/cli install tessl/npm-browser-sync-webpack-plugin@1.2.0BrowserSync Webpack Plugin provides seamless integration between BrowserSync and Webpack, enabling live reloading and synchronization features during development. The plugin automatically starts BrowserSync when Webpack runs in watch mode and supports both static file serving and proxying through Webpack Dev Server.
npm install --save-dev browser-sync-webpack-pluginconst BrowserSyncPlugin = require('browser-sync-webpack-plugin');For ES modules:
import BrowserSyncPlugin from 'browser-sync-webpack-plugin';const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
module.exports = {
// webpack config
plugins: [
new BrowserSyncPlugin({
// BrowserSync options - serve static files
host: 'localhost',
port: 3000,
server: { baseDir: ['public'] }
})
]
};const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
module.exports = {
// webpack config
plugins: [
new BrowserSyncPlugin(
// BrowserSync options - proxy through webpack-dev-server
{
host: 'localhost',
port: 3000,
proxy: 'http://localhost:3100/'
},
// Plugin options
{
reload: false,
name: 'my-browsersync-instance',
callback: (err, bs) => {
if (err) console.error('BrowserSync error:', err);
console.log('BrowserSync started');
}
}
)
]
};BrowserSync Webpack Plugin is built around several key integration patterns:
watch-run, compilation, done)Creates a new BrowserSync webpack plugin instance with specified options.
/**
* Creates a BrowserSync webpack plugin
* @param {Object} browserSyncOptions - Options passed to BrowserSync.init()
* @param {Object} [pluginOptions] - Plugin-specific configuration options
*/
function BrowserSyncPlugin(browserSyncOptions, pluginOptions)browserSyncOptions (Object, optional)
host, port, server, proxy, notify, open{}pluginOptions (Object, optional)
{reload: true, name: 'bs-webpack-plugin', callback: undefined}/**
* Plugin-specific configuration options
* @typedef {Object} PluginOptions
* @property {boolean} [reload=true] - Whether to trigger browser reload on webpack compilation completion
* @property {string} [name='bs-webpack-plugin'] - BrowserSync instance name for identification
* @property {function} [callback] - Callback function passed to BrowserSync.init()
*/reload (boolean)
false when using with webpack-dev-server to avoid conflicting reloadstruename (string)
'bs-webpack-plugin'callback (function)
undefinedWebpack plugin interface method that registers compilation hooks.
/**
* Webpack plugin interface method
* @param {Object} compiler - Webpack compiler instance
*/
apply(compiler)This method is called automatically by Webpack and registers the following hooks:
watch-run: Detects when Webpack enters watch modecompilation: Shows "Rebuilding..." notification during compilationdone: Initializes BrowserSync or triggers reload after compilationThe plugin handles BrowserSync initialization errors through the optional callback function. If no callback is provided, errors are handled internally by BrowserSync.
new BrowserSyncPlugin(
{ /* browserSync options */ },
{
callback: (err, bs) => {
if (err) {
console.error('BrowserSync failed to start:', err);
} else {
console.log('BrowserSync is running at:', bs.getOption('urls'));
}
}
}
);webpack --watch)reload: false, the plugin relies on webpack-dev-server's hot module replacement instead of BrowserSync reloadingbrowser-sync@^2, webpack@^1 || ^2 || ^3lodash@^4 (used internally for option merging)