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 🙏

© 2026 – Pkg Stats / Ryan Hefner

vite-plugin-drupal-hmr

v1.1.1

Published

A Vite plugin to enhance Drupal development with HMR.

Downloads

24

Readme

vite-plugin-drupal-hmr

A Vite plugin providing seamless Hot Module Replacement (HMR) for Twig files within Drupal development workflows.

Features

  • Twig HMR: Update templates without a full page refresh.
  • JS Re-activation: Automatically re-attaches Drupal.behaviors to updated fragments.
  • SDC Support: Fully compatible with Single-Directory Components.
  • Canvas Integration: Works with Drupal Canvas pages.
  • Zero-Config Detection: Automatically detects theme names and paths relative to the Drupal root.
  • Vite Dev Server: The plugin is only active during serve mode.

Requirements

  • Twig Debug Mode: Must be enabled in Drupal's services.yml (lean how).
  • Vite Client: The /@vite/client endpoint must be loaded in your Drupal pages. The easiest way to do this is to use the Drupal Vite module.

Installation

npm install -D vite-plugin-drupal-hmr

Usage

Add the plugin to your vite.config.ts:

import { defineConfig } from "vite";
import viteDrupalHMR from "vite-plugin-drupal-hmr";

export default defineConfig({
  plugins: [
    viteDrupalHMR({
      // Optional options
    }),
  ],
});

Configuration Options

| Option | Type | Description | | --------- | ------ | ------------------------------------------------------------------------------------------------------------------ | | themePath | string | Optional: Relative path from Drupal root to the Vite project root(e.g., /themes/custom/your-theme). | | themeName | string | Optional: The machine name of your theme.Auto-detected from the themePath directory name if not provided. |

Events

The plugin dispatches a custom DOM event whenever a fragment is updated. This is useful for re-initializing third-party libraries that are not wrapped in Drupal behaviors.

document.addEventListener('drupal-hmr:updated', (event) => {
  console.log('Template updated:', event.detail.templateId);
  console.log('New fragment container:', event.detail.target);
});

Limitations

  • Existing Elements Only: Can only perform HMR on templates already rendered on the current page load.
  • Discovery: Adding a new Twig template or using a previously unused template requires a full page reload.
  • State Preservation: While CSS and HTML update instantly, local JS state (e.g., a variable inside a closure not persisted to drupalSettings) might be reset if the script is re-executed by attachBehaviors.

Technical Overview

The plugin leverages the Vite handleHotUpdate hook to detect .twig file changes.

  1. Server-side: The plugin identifies if the changed file is a standard template or an SDC component. It then broadcasts a message via Vite's WebSocket server.
  2. Client-side: An injected script intercepts the update event, performs an asynchronous fetch of the current URL, and extracts the new HTML fragment by matching the Twig debug comments.
  3. Replacement: It uses the browser's DOM Range API to replace the old template content with the newly fetched fragment without losing the global application state.
  4. JS re-activation: After replacement, it automatically triggers Drupal.attachBehaviors() on the new fragment to ensure JS interactivity (tabs, libraries, etc.) is restored.

For more technical overview, read here.