- Notifications
You must be signed in to change notification settings - Fork7
Babel plugin to optimize the use of clsx, classnames, and other libraries with a compatible API
License
merceyz/babel-plugin-optimize-clsx
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Babel plugin to optimize the use ofclsx,classnames, and all libraries with a compatible API
yarn add babel-plugin-optimize-clsx --devornpm install babel-plugin-optimize-clsx --save-dev
Name | Version |
---|---|
Babel | ^7.0.0 |
Node | >=8 |
clsx({[classes.disabled]:disabled,[classes.focusVisible]:focusVisible&&!disabled,});// Transforms toclsx(disabled&&classes.disabled,focusVisible&&!disabled&&classes.focusVisible);
clsx({[classes.disabled]:disabled,[classes.focusVisible]:focusVisible&&!disabled,});// Transforms toclsx(disabled ?classes.disabled :focusVisible&&classes.focusVisible);
clsx({[classes.focusVisible]:this.state.focusVisible,[focusVisibleClassName]:this.state.focusVisible,});// Transforms toclsx(this.state.focusVisible&&[classes.focusVisible,focusVisibleClassName]);
functionfoo(props){const{position:p}=props;constx=clsx({[classes.x]:p==='top',[classes.y]:p==='bottom',});}foo.propTypes={position:PropTypes.oneOf(['top','bottom']),};// Transforms tofunctionfoo(props){const{position:p}=props;constx=clsx(p==='top' ?classes.x :classes.y);}foo.propTypes={position:PropTypes.oneOf(['top','bottom']),};
constx=clsx({btn:true,'col-md-1':true,['btn-primary']:true,});// Transforms toconstx='btn col-md-1 btn-primary';
constx=clsx({btn:true,'btn-foo':isDisabled,'btn-bar':!isDisabled,});// Transforms toconstx='btn '+(isDisabled ?'btn-foo' :'btn-bar');
Benchmarks can be found in thebenchmark
directory
Name | Type | Default value |
---|---|---|
libraries | string[] | ['clsx', 'classnames'] |
By default the plugin looks forimport
andrequire
statements forclsx
andclassnames
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 haveclsx
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
importclsxfrom'clsx';constx=clsx('foo','bar');consty=clsx({classA:foo==='a',classB:foo!=='a'});constz=clsx({classA:foo==='a',classB:foo!=='a',classC:bar==='c',classD:bar!=='c',});// Transforms toconstx='foo bar';consty=foo==='a' ?'classA' :'classB';constz=(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.
About
Babel plugin to optimize the use of clsx, classnames, and other libraries with a compatible API
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.