All Projects → merceyz → Babel Plugin Optimize Clsx

merceyz / Babel Plugin Optimize Clsx

Licence: mit
Babel plugin to optimize the use of clsx, classnames, and other libraries with a compatible API

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Babel Plugin Optimize Clsx

Babel Plugin Sitrep
Log all assignments and the return value of a function with a simple comment
Stars: ✭ 442 (+452.5%)
Mutual labels:  babel, babel-plugin
Postjss
Use the power of PostCSS in compiling with JSS
Stars: ✭ 40 (-50%)
Mutual labels:  babel, babel-plugin
Htm
Hyperscript Tagged Markup: JSX alternative using standard tagged templates, with compiler support.
Stars: ✭ 7,299 (+9023.75%)
Mutual labels:  babel, babel-plugin
I18nize React
Internationalize react apps within a lunch break
Stars: ✭ 389 (+386.25%)
Mutual labels:  babel, babel-plugin
Tinker.macro
Evaluate Laravel code at build-time, via Laravel Tinker
Stars: ✭ 56 (-30%)
Mutual labels:  babel, babel-plugin
Faster.js
faster.js is a Babel plugin that compiles idiomatic Javascript to faster, micro-optimized Javascript.
Stars: ✭ 429 (+436.25%)
Mutual labels:  babel, babel-plugin
Babel Plugin Styled Components
Improve the debugging experience and add server-side rendering support to styled-components
Stars: ✭ 878 (+997.5%)
Mutual labels:  babel, babel-plugin
Effectfuljs
JavaScript embedded effects compiler
Stars: ✭ 287 (+258.75%)
Mutual labels:  babel, babel-plugin
Babel Plugin Css Prop
Babel plugin to transpile `css` prop to a styled component. (Experimental)
Stars: ✭ 56 (-30%)
Mutual labels:  babel, babel-plugin
Babel Plugin Root Import
Add the opportunity to import modules by the root path
Stars: ✭ 1,084 (+1255%)
Mutual labels:  babel, babel-plugin
Babel Plugin React Remove Properties
Babel plugin for removing React properties. 💨
Stars: ✭ 327 (+308.75%)
Mutual labels:  babel, babel-plugin
Astexplorer.app
https://astexplorer.net with ES Modules support and Hot Reloading
Stars: ✭ 65 (-18.75%)
Mutual labels:  babel, babel-plugin
Babel Plugin Tailwind Components
Use Tailwind with any CSS-in-JS library
Stars: ✭ 320 (+300%)
Mutual labels:  babel, babel-plugin
Babel Plugin Partial Application
[DEPRECATED] Please use https://github.com/citycide/param.macro
Stars: ✭ 60 (-25%)
Mutual labels:  babel, babel-plugin
Babel Plugin Css Modules Transform
Extract css class names from required css module files, so we can render it on server.
Stars: ✭ 318 (+297.5%)
Mutual labels:  babel, babel-plugin
Babel Plugin Transform React Remove Prop Types
Remove unnecessary React propTypes from the production build. 🎈
Stars: ✭ 890 (+1012.5%)
Mutual labels:  babel, babel-plugin
Babel Plugin Console
Babel Plugin that adds useful build time console functions 🎮
Stars: ✭ 278 (+247.5%)
Mutual labels:  babel, babel-plugin
Babel Plugin Module Resolver
Custom module resolver plugin for Babel
Stars: ✭ 3,134 (+3817.5%)
Mutual labels:  babel, babel-plugin
Xwasm
[Work In Progress] WebAssembly Packager and WASM tooling for modern frontend
Stars: ✭ 45 (-43.75%)
Mutual labels:  babel, babel-plugin
Babel Plugin Captains Log
Babel plugin that injects helpful details into console statements
Stars: ✭ 80 (+0%)
Mutual labels:  babel, babel-plugin

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

Name Version
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

Name Type Default value
libraries string[] ['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"]
			}
		]
	]
}

Name Type Default value
functionNames string[] []

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

Name Type Default value
removeUnnecessaryCalls boolean true

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');

Name Type Default value
collectCalls boolean false

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].