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

@ima/cli-plugin-scramble-css

v1.2.7

Published

Plugin for @ima/cli that implements CSS class minimizer and uglifier that can be reverse-compiled at runtime.

Downloads

25

Readme

@ima/cli-plugin-scramble-css

Implements CSS class minimizer and uglifier that can be reverse-compiled at runtime (you can access classes using their original name).

It works by processing all CSS files using custom PostCSS plugin, that mangles (scrambles) and minimizes all classes, while also building translation table (hashtable.json) along the way.

The result is CSS file with mangled class names and companion hashtable that we use in our custom $CssClasses processor to, translate existing classes used out components to the new scrambled ones.

Requirements

For this feature to work you need to wrap all your classNames in cssClasses function. Otherwise you'll end up with scrambled classes in CSS file but original class names in your components.

import { useComponent } from '@ima/react-page-renderer';

export default function Card() {
  const { cssClasses } = useComponent();

  return (
    // highlight-next-line
    <div className={cssClasses('card')} />
  );
}

or in case of class components:

import { AbstractPureComponent } from '@ima/react-page-renderer';

export default class Card extends AbstractPureComponent {
  render() {
    return (
      // highlight-next-line
      <div className={this.cssClasses('card')} />
    );
  }
}

Installation

npm install @ima/cli-plugin-scramble-css -D

Usage

// ./ima.config.js
const { ScrambleCssPlugin } = require('@ima/cli-plugin-scramble-css');

/**
 * @type import('@ima/cli').ImaConfig
 */
module.exports = {
  plugins: [new ScrambleCssPlugin()],
};

$CssClasses override and hashtable.json

We have to provide our custom $CssClasses processor and pass it our generate hashtable.json file. To do that, we're going to load it's contents in the app environment:

// ./server/config/environment.js
const fs = require('fs');
const path = require('path');

const hashTablePath = path.resolve(
  __dirname,
  '../../build/static/css/hashTable.json'
);

module.exports = (() => {
  return {
    prod: {
      $App: {
        scrambleCss: {
          hashTable: fs.existsSync(hashTablePath)
            ? JSON.parse(fs.readFileSync(hashTablePath))
            : null,
        },
      },
      // ...
    }
  }
});

Finally, the hashtable is now available under config.$App.scrambleCss.hashTable, so we're going to provide it to the plugin's custom $CssClasses processor in the app bind.js file, and we're done:

// ./app/config/bind.js
import { scrambleCssClasses } from '@ima/cli-plugin-scramble-css/scrambleCssClasses';

export default (ns, oc, config) => {
  oc.bind(
    '$CssClasses',
    scrambleCssClasses(config?.$App?.scrambleCss?.hashTable),
    []
  );
};

CLI Arguments

--scrambleCss

boolean

The scrambling is enabled by default for production environment. However you can explicitly enable/disable it using this CLI argument. This applies for both CLI commands.

Options

new ScrambleCssPlugin(options: {
  scrambleCssMinimizerOptions?: {
    assetFilter?: (filename: string) => boolean;
    hashTableFilename?: string;
    mainAssetFilter?: (filename: string) => boolean;
  };
});

scrambleCssMinimizerOptions

object

These are passed directly into the ScrambleCssMinimizer. You can define custom:

  • assetFilter - filter files to scramble.
  • hashTableFilename - custom translation hashtable.json filename. Defaults to: ./build/static/css/hashTable.json.
  • mainAssetFilter - should resolve to the main css file. The minimizer first processes the main.css file and generates the hashtable.json translation table. If you then want to process other assets with existing hashtable, these should be filtered out in this function, since the minimizer minimizes them in second pass using existing hashtable.json.

 Note: You should be fine with the default options in almost any situation except some special use cases.


For more information, take a look at the IMA.js documentation.

Unscrambling in a browser

To unscramble a scrambled web page in a browser, you can save this code javascript: var s=document.createElement('script');s.type='module';s.src='/pro/static/public/debug-scramble-css.js';document.head.appendChild(s) as a bookmark URL.