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-replace-mangle-css-class

v0.0.7

Published

Vite plugin to replace minified and obfuscated CSS classes at runtime.

Downloads

11

Readme

Usage

The plugin will generate optimized class name in HTML and JavaScript. configure as follows:

vite.config.js

import { defineConfig } from "vite";
import replaceMangleCSSClass from "vite-plugin-replace-mangle-css-class";

export default defineConfig({
  plugins: [
    replaceMangleCSSClass({
      extensions: [".html", ".js", ".ts", ".vue", ".tsx", ".jsx"],
    }),
  ],
});

This will replace class name matched regex in HTML and JavaScript files.

How it works

This plugin in html type files it looks for and obtains the html class attribute and in javascript (others) files it also replaces strings like this: .{classname}. For example:

In html

<!-- replace correctly -->
  <!-- input -->
  <div :class="{card: true}" class="card">
    <div class="card-body"></div>
  </div>
  <!-- output -->
  <div :class="{pkJ414a: true}" class="pkJ414a">
    <div class="mki4Aak"></div>
  </div>

But... in javascript:

// replace correctly
const el = document.querySelector(".card");
const el = document.querySelector(".pkJ414a");
// not replace correctly
const el = document.getElementsByClassName("card");
const el = document.getElementsByClassName("card");

Options

 const options = {
    cwd: process.cwd(), // the current working directory
    enabled: true, // enabled/disabled
    extensions: [".html", ".js"], // files to find and replace obfuscated classes
    JSONFile: "./.mangle-css-class/classes.json", // the json file of renamed CSS classes
    exclude: [], // lists of all CSS classes to exclude
    generateBundle(classNames) {}, // callback after completion of replacements
  }

cwd String

the current working directory

enabled boolean

Enable/Disable the plugin

extensions String[]

Files accepted for find and replace renamed css classes. e.g.: [".html",".js"]

JSONFile String

The JSON file containing all the renamed css classes. From this file you will get the CSS classes to find and replace. It must have the following structure:

{
  "card":"pkJ414a",
  "card-body":"mki4Aak"
}

exclude String, Regex, String[], Regex[]

List of all CSS classes that should not be replaced. Accepts values ​​of type string or regex type. e.g.: ["card", /card/gi]

generateBundle(classNames) Function

A callback that is called after generating the bundle.

Example

Source code

// css
.card {
}
.card-body {
}
<!-- html -->
<div class="card">
  <div class="card-body"></div>
</div>

Output code

// css
.pkJ414a {
}
.mki4Aak {
}
<!-- html -->
<div class="pkJ414a">
  <div class="mki4Aak"></div>
</div>

Notes

Dynamically generated css classes will not be replaced

We recommend using this vite plugin with postcss-mangle-css-class

License

This project is under license from MIT. For more details, see the LICENSE file.

Made with ❤️