Socket
Socket
Sign inDemoInstall

babel-plugin-optimize-clsx

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-optimize-clsx

Babel plugin to optimize clsx and classNames function calls


Version published
Weekly downloads
22K
increased by1.88%
Maintainers
1
Weekly downloads
 
Created
Source

babel-plugin-optimize-clsx

Babel plugin to optimize clsx and classNames function calls

Install

yarn add babel-plugin-optimize-clsx --dev
or
npm install babel-plugin-optimize-clsx --save-dev

Example

Extract object properties

Transforms

clsx(
  'foo',
  {
    [classes.disabled]: disabled,
    [classes.focusVisible]: focusVisible && !disabled,
  },
  'bar',
);

to

clsx('foo', disabled && classes.disabled, focusVisible && !disabled && classes.focusVisible, 'bar');

Extract and create conditional expression

Transforms

clsx(
  'foo',
  {
    [classes.disabled]: disabled,
    [classes.focusVisible]: focusVisible && !disabled,
  },
  'bar',
);

to

clsx('foo', 'bar', disabled ? classes.disabled : focusVisible && classes.focusVisible);

Extract and combine

Transforms

clsx({
  [classes.disabled]: disabled,
  [classes.focusVisible]: this.state.focusVisible,
  [focusVisibleClassName]: this.state.focusVisible,
});

to

clsx(
  this.state.focusVisible && [classes.focusVisible, focusVisibleClassName],
  disabled && classes.disabled,
);

Benchmarks

Benchmarks can be found in the benchmark directory

Options

NameTypeDefault value
librariesstring[]['clsx', 'classnames']

By default the plugin looks for import and require statements for clsx and classnames and uses that to know which function calls to optimize. If you're using another library with a compatible API you can overwrite that with this option.

{
  "plugins": [
    [
      "babel-plugin-optimize-clsx",
      {
        "libraries": ["clsx", "classnames", "my-custom-library"]
      }
    ]
  ]
}

NameTypeDefault value
functionNamesstring[][]

If you want the plugin to match on all functions with a specific name, no matter where it comes from you can specify them using this option. An example for this is if you have clsx as a global function and thus don't import it.

{
  "plugins": [
    [
      "babel-plugin-optimize-clsx",
      {
        "functionNames": ["myClsxImplementation"]
      }
    ]
  ]
}

Keywords

FAQs

Package last updated on 21 May 2019

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