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

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 the use of clsx, classnames, and all libraries with a compatible API

  • 2.6.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
27K
increased by15.01%
Maintainers
1
Weekly downloads
 
Created
Source

babel-plugin-optimize-clsx

Babel plugin to optimize the use of clsx, classnames, and all libraries with a compatible API

Install

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

Requirements

NameVersion
Babel^7.0.0
Node>=8

Examples

Object properties

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

// Transforms to

clsx(disabled && classes.disabled, focusVisible && !disabled && classes.focusVisible);

Conditional expressions

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

// Transforms to

clsx(disabled ? classes.disabled : focusVisible && classes.focusVisible);

Combine arguments

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

// Transforms to

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

PropTypes

function foo(props) {
	const { position: p } = props;
	const x = clsx({
		[classes.x]: p === 'top',
		[classes.y]: p === 'bottom',
	});
}

foo.propTypes = {
	position: PropTypes.oneOf(['top', 'bottom']),
};

// Transforms to

function foo(props) {
	const { position: p } = props;
	const x = clsx(p === 'top' ? classes.x : classes.y);
}

foo.propTypes = {
	position: PropTypes.oneOf(['top', 'bottom']),
};

String literals

const x = clsx({
	btn: true,
	'col-md-1': true,
	['btn-primary']: true,
});

// Transforms to

const x = 'btn col-md-1 btn-primary';

Unnecessary function calls

const x = clsx({
	btn: true,
	'btn-foo': isDisabled,
	'btn-bar': !isDisabled,
});

// Transforms to

const x = 'btn ' + (isDisabled ? 'btn-foo' : 'btn-bar');

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"]
			}
		]
	]
}

NameTypeDefault value
removeUnnecessaryCallsbooleantrue

By default the plugin will remove unnecessary function calls and if all calls are removed, imports. If you need to keep them, you can set this option to false.

Example of some unnecessary calls

import clsx from 'clsx';
const x = clsx('foo', 'bar');
const y = clsx({ classA: foo === 'a', classB: foo !== 'a' });
const z = clsx({
	classA: foo === 'a',
	classB: foo !== 'a',
	classC: bar === 'c',
	classD: bar !== 'c',
});

// Transforms to

const x = 'foo bar';
const y = foo === 'a' ? 'classA' : 'classB';
const z = (foo === 'a' ? 'classA ' : 'classB ') + (bar === 'c' ? 'classC' : 'classD');

NameTypeDefault value
collectCallsbooleanfalse

Writes all function calls, before they are optimized, to a file. Used to help test the plugin on repositories.

Keywords

FAQs

Package last updated on 18 Jun 2021

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