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-extension-reloader

v1.1.6

Published

Companion package to 'Extension Development - Auto Reloader' chrome extension, to automatically reload target browser extensions during the extension development process.

Downloads

148

Readme

Extension Development - Auto Reloader

Extension auto reloader is an extension and npm package combination that automatically reloads your unpacked chrome extension when you save changes to your files during development.

It requires a websocket connection to do so, which you setup with this npm package.

Automatic extension reloading

Simple Example - Changes to a ts content script reloads and re-runs the given extension

content script example

Project Structure and running

  1. In your "src" directory make a new subfolder "extension" and put your extension files in there. Your manifest.json, serviceWorker.ts and contentScript/s.ts files should be in there and other static assets like icons. Yes we are using typescript.
src
├── extension
    ├── manifest.json
    ├── serviceWorker.ts
    ├── contentScript.ts
    ├── icons
        ├── icon16.png
        ├── icon32.png
        ├── icon48.png
        ├── icon128.png
  1. Now add a new script to your package json to run your Vite Build step in watch mode. You cannot make an extension work in dev mode, as the unpacked extension requires generated files. Hence we use build mode:
{
  "scripts": {
    ...
    "build:watch": "vite build --watch"
    ...
  }
}
  1. In your vite.config.ts file, add the following plugins (from this package) and modify the build step to include your service worker (if applicable) and content scripts (if applicable):
...
import {
  extensionReloaderBuildStep,
  extensionReloaderWatchExternal,
  extensionReloaderWebSocket,
} from "vite-plugin-extension-reloader";
import copy from "rollup-plugin-copy";  // Highly recommended for copying static assets like icons
...

export default defineConfig({
 plugins: [
    // Your existing Vite plugins
    ...
    extensionReloaderBuildStep("src/extension/manifest.json"),
    extensionReloaderWatchExternal("src/extension/**/*"),  // This is optional, but will watch for changes in your manifest
    extensionReloaderWebSocket(),
    ...
    copy({
      targets: [
        // Use glob patterns to match static files to copy
        {
          src: "src/extension/*",
          dest: "dist",
          ignore: ["**/*.js", "**/*.ts", "**/manifest.json"],
        },
      ],
      copyOnce: false,
      flatten: true,
    }),
  ],
  // Your existing config
  ...
  // Build step modifications
  build: {
    emptyOutDir: true,
    rollupOptions: {
      input: {
        main: path.resolve(__dirname, "index.html"),
        // Do this for each content script you have
        contentScript: path.resolve(
          __dirname,
          "src/extension/contentScript.ts"
        ),
        // Do this for your service worker
        serviceWorker: path.resolve(
          __dirname,
          "src/extension/serviceWorker.ts"
        ),
      },
      output: {
        entryFileNames: "[name].js",
        chunkFileNames: "[name].js",
        assetFileNames: "[name].[ext]",
      },
    },
  },
});
  1. Now once you have everything setup, you should run the included websocket server. This is so we can automatically reload your extension. Run and note the port number:
# defaults to port 3001
npx wss-ext-reloader

# Or, define your own port eg "5000":
npx wss-ext-reloader --port <XXXXX>
  1. Now you are ready to develop your extension! Run the build:watch script and start developing. When you save changes to your files, the websocket server will automatically reload your extension in the browser:
npm run build:watch
  1. To load an unpacked extension in the browser, you must point to a file location for your extension. This will be the "dist" directory which is generated by the build step:
dist
├── manifest.json
├── serviceWorker.js
├── contentScript.js
├── main.js
├── index.html
├── icons
    ├── icon16.png
    ├── icon32.png
    ├── icon48.png
    ├── icon128.png
... others
  1. Now connect the extension auto reloader browser extension to the websocket server:

https://github.com/con-dog/extension-reloader-plugin/blob/main/media/stopped.png

You can get your unpacked extensions ID from the manage extensions page in chrome: chrome://extensions/

extension ID

Then enter the required information and hit run:

extension info entered

Now whenever you make changes to your code, your extension will auto reload itself without you needing to do anything

To stop simply press stop: https://github.com/con-dog/extension-reloader-plugin/blob/main/media/3.png