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-dom

v1.0.4

Published

Enables DOM manipulation and comment injection during the build process

Downloads

40

Readme

Vite Plugin DOM

Vite Plugin DOM is a ViteJS plugin that enables DOM manipulation and comment injection during the build process. This plugin allows you to modify the parsed HTML and inject comments into specific DOM elements based on provided selectors. You can control the application of this plugin based on the Vite build modes and specify custom handling functions.

Install

To use Vite Plugin DOM, first, make sure you have Vite installed in your project. Then, install the plugin using npm or yarn:

# With npm
npm i -D vite-plugin-dom

# With yarn
yarn add vite-plugin-dom --dev

Usage

To use the plugin, simply import it and add it to the plugins array in your Vite config file:

// vite.config.js
import pluginDom from 'vite-plugin-dom'

export default {
  plugins: [
    pluginDom({
      applyOnMode: true,
      comments: {
        tags: ['div', /$h[1-6]$/],
        classes: ['highlight'],
        ids: ['header']
      },
      handler: element => {
        // Modify the element here
      },
      onComplete: (dom, error) => {
        if (error) {
          console.error('DOM parsing error:', error)
        } else {
          console.log('DOM parsing complete:', dom)
        }
      }
    })
  ]
}

In the code above, the plugin will be applied to all modes, and it will add comments to div and h1 elements, elements with the class highlight, and the element with the ID header. The handler function will also be called for each element, allowing you to perform custom modifications. The onComplete function will log the parsed DOM or any parsing errors.

Options

The plugin accepts the following options:

applyOnMode (default: ['static'])

Specifies the modes on which the plugin should be applied. When set to true, the plugin will be applied on all modes. When set to an array of strings, the plugin will be applied only on the specified modes.

pluginDom({
  applyOnMode: ['development', 'production']
})

comments

Selectors to match comments. The plugin will add comments to elements that match these selectors.

pluginDom({
  comments: {
    tags: ['div', 'p'], // Array of tag names to match for adding comments.
    classes: ['highlight', /theme-.*/], // Array of class names or regular expressions to match for adding comments.
    ids: ['section1', /^chapter.*/] // Array of element IDs or regular expressions to match for adding comments.
  }
})

handler

A callback function to handle elements in the DOM. This function will be called for each element in the DOM tree.

pluginDom({
  handler: element => {
    // add `[data-toggle="dropdown"]` to the dropdown toggler
    if (domHas(elem, 'class', ['dropdown', 'dropup'])) {
      // find toggler element
      const toggler = this.findOne(
        node => !!node.attribs['aria-expanded'],
        elem.childNodes,
        false
      )

      if (toggler) {
        toggler.attribs['data-toggle'] = 'dropdown'
      }
    }
  }
})

onComplete

A callback function to handle the complete DOM after parsing. This function will be called once the DOM parsing is complete.

pluginDom({
  onComplete: (dom, error) => {
    // Handle the complete DOM here
  }
})

API

The plugin exports the following utility functions for additional DOM manipulation:

domHas(elem: Element, attrName: string, pattern: string | RegExp | Array<string | RegExp>): boolean

Checks if an element has a given attribute with the provided pattern.

<div id="foo" class="bar">...</div>

Let's say the elem object is refer to the above markup:

import { domHas } from 'vite-plugin-dom'

// test by id
const hasFoo = domHas(elem, 'id', 'foo') // true
const hasFoo = domHas(elem, 'id', 'qux') // false
// test by class
const hasBar = domHas(elem, 'class', ['qux', /bar/]) // true

License

GitHub

A project by Stilearning © 2022-2023.