Socket
Socket
Sign inDemoInstall

stylehacks

Package Overview
Dependencies
13
Maintainers
7
Versions
45
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    stylehacks

Detect/remove browser hacks from CSS files.


Version published
Weekly downloads
8.5M
decreased by-18.8%
Maintainers
7
Install size
2.45 MB
Created
Weekly downloads
 

Package description

What is stylehacks?

The stylehacks npm package is designed to detect and remove hacks from CSS files. These hacks are often used to target specific browsers with CSS rules that wouldn't be valid or necessary in a standards-compliant browser. By identifying and removing these hacks, stylehacks helps in cleaning up CSS files, making them more maintainable and potentially reducing file size.

What are stylehacks's main functionalities?

Detecting CSS hacks

This feature allows the detection and automatic removal of CSS hacks. The code sample demonstrates how to use stylehacks with PostCSS to process a CSS string that contains a hack (`_color: red`), which is targeted at old versions of IE, and remove it.

"const stylehacks = require('stylehacks');
const postcss = require('postcss');

postcss([ stylehacks() ])
.process('h1 { _color: red }', { from: undefined })
.then(result => {
  console.log(result.css);
  // Output will be 'h1 { }' as the hack is removed
});"

Whitelisting specific hacks

This feature allows users to lint for hacks without removing them, useful for cases where a hack is intentionally being used. The code sample shows how to use stylehacks in lint mode, which will not remove the hack but can be used to report or log its presence.

"const stylehacks = require('stylehacks');
const postcss = require('postcss');

postcss([ stylehacks({ lint: true }) ])
.process('h1 { _color: red }', { from: undefined })
.then(result => {
  console.log(result.css);
  // Output will be the same input 'h1 { _color: red }' as it only lints
});"

Other packages similar to stylehacks

Readme

Source

stylehacks

Detect/remove browser hacks from CSS files.

Install

With npm do:

npm install stylehacks --save

Example

In its default mode, stylehacks will remove hacks from your CSS file, based on the browsers that you wish to support.

Input

h1 {
    _color: white;
    color: rgba(255, 255, 255, 0.5);
}

Output

h1 {
    color: rgba(255, 255, 255, 0.5);
}

API

stylehacks.detect(node)

Type: function
Returns: boolean

This method will take any PostCSS node, run applicable plugins depending on its type, then will return a boolean depending on whether it found any of the supported hacks. For example, if the decl node found below is passed to the detect function, it will return true. But if the rule node is passed, it will return false instead.

h1 { _color: red }

postcss([ stylehacks(opts) ])

stylehacks can also be consumed as a PostCSS plugin. See the documentation for examples for your environment.

options
lint

Type: boolean
Default: false

If lint mode is enabled, stylehacks will not remove hacks from the CSS; instead, it will add warnings to Result#messages.

stylehacks works well with your existing PostCSS setup:

  • stylelint - Comprehensive & modern CSS linter, to ensure that your code style rules are respected.

Contributing

Pull requests are welcome. If you add functionality, then please add unit tests to cover it.

License

MIT © Ben Briggs

Keywords

FAQs

Last updated on 28 Oct 2022

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc