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

rollup-plugin-browser-sync-extension

v1.1.6

Published

Live Reload for Rollup Plugin

Downloads

16

Readme

Live Reload for Rollup Plugin

Supports web page reloading and Chrome extension reloading.

Install

npm i rollup-plugin-browser-sync-extension -D

Usage

rollup.config.js

import browserSync from 'rollup-plugin-browser-sync-extension';

export default {
  ...
  plugins: [
    browserSync({
      options: {
        open: false,
        server: 'build',
        single: true,
        ui: false,
        ghostMode: false,
        notify: false,
      },
    }),
  ],
};

Browsersync options

single: true is very suitable for single-page applications, resolving the error of not finding the page on refresh.

Browsersync sometimes interrupts reloading due to script injection failures. It's recommended to manually add the script in the HTML file for more reliability.

browserSync({
    options: {
        ...
        snippet: false,
    },
}),

<script async src="/browser-sync/browser-sync-client.js?v=2.29.3"></script>

Learn more about Browsersync configuration. https://browsersync.io/docs/options

Chrome Extension Reload

browserSync({
  options: {
    open: false,
    server: 'build',
    single: true,
    ui: false,
    ghostMode: false,
    notify: false,
  },
  extReload: true,
  extReloadOptions: { port: 5000 },
}),
  • When extReload is set to false, use browserSync for webpage reloading with the configuration items as options.
  • When extReload is set to true, use ws for extension reloading with the configuration items as extReloadOptions.

WebSocketServer Options

https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback

Chrome Extension Code

extReload.js

const ws = new WebSocket('ws://localhost:5000');
// Connection succeeded
ws.addEventListener('open', event => {
    console.log('OPEN');
});
// Receive server messages
ws.addEventListener('message', event => {
    console.log(`Message: ${event.data}`);
    // Reload background
    chrome.runtime.reload();
});
ws.addEventListener('error', error => {
    console.error(`WebSocket error: ${error}`);
});
// Reload tab
chrome.runtime.onInstalled.addListener(() => {
    chrome.tabs.query({ active: true, currentWindow: true, url: 'http://127.0.0.1/*' }, tabs => {
        tabs.forEach(tab => chrome.tabs.reload(tab.id));
    });
});

Code injection limited to development environment.

output: {
  ...
  banner: chunk => {
    // In development environment, inject reload script into specified files.
    // "watch" can be any custom name.
    if (process.env.NODE_ENV === 'watch' && chunk.fileName === 'service_worker.js') {
      const conent = fs.readFileSync('src/extReload.ts', 'utf-8');
      console.log('inject ok');
      return conent;
    }
    return '';
  },
},

Add environment variables

"scripts": {
  "watch": "rollup -c -w --environment NODE_ENV:watch",
},

Service Workers get terminated after being idle for a while.

Open the Service Worker view to keep it active.

https://developer.chrome.com/docs/extensions/mv3/service_workers/