Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ima/cli-plugin-scramble-css

Package Overview
Dependencies
Maintainers
9
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ima/cli-plugin-scramble-css

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

  • 1.2.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
26
increased by136.36%
Maintainers
9
Weekly downloads
 
Created
Source

@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.

Keywords

FAQs

Package last updated on 19 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc