Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

babel-plugin-optimize-clsx

merceyz72.8kMIT2.6.2

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

babel, clsx, classnames, optimize, minimize

readme

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.

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

2.6.2 (2021-06-18)

Bug Fixes

  • clone callee when wrapping referenced objects (16eef18), closes #20

2.6.1 (2020-06-20)

Bug Fixes

  • remove peerDependency on babel/core (744391d)
  • collectCalls: write CallExpressions to the cache dir (17405f5)
  • createlookup: handle optional member expression (1af84b2)

Build System

2.6.0 (2020-02-15)

Features

  • referenced-objects: handle multiple references (14a2e97)
  • optimize referenced objects (20575e9)
  • optimize: handle unnecessary "or" statements (fcdb75e)
  • use scope to get expressions (68becac)

Bug Fixes

  • optimize: handle the operator changing (3e92ddb)
  • removecalls: remove unsafe optimization (b0deb61)

Performance Improvements

  • createlookup: modify node instead of recreating it (4800a6b)
  • createlookup: skip lookup for just one item (f8d089d)

Build System

2.5.0 (2019-10-07)

Bug Fixes

  • combinestrings: set cooked value for templateelement (5e25580), closes #15
  • extract: handle spread and method properties (51c8ced)
  • optimize: handle ConditionalExpression in LogicalExpression (fa05e72)

Build System

  • deps: upgrade dependencies (cd02789)
  • improve terser output (11ca28b)
  • remove rollup-plugin-commonjs (b994f88)

Features

  • collectcalls: include the location of the code (1ab1eab)

2.4.1 (2019-08-18)

Build System

2.4.0 (2019-08-18)

Bug Fixes

  • helper: compare literals correctly (d728a45)
  • stripliterals: handle all falsy values (721af56)
  • stripliterals: handle empty template literals (1e9295c)
  • stripliterals: remove more unnecessary truthy values (98d2a9d)

Build System

  • deps: add babel-plugin-lodash (cd37d10)
  • deps-dev: bump husky from 3.0.0 to 3.0.2 (#3) (de0ff50)
  • deps-dev: bump rollup from 1.17.0 to 1.19.4 (#12) (0cb7b7e)
  • deps-dev: bump standard-version from 6.0.1 to 7.0.0 (#6) (e699d1d)

Features

Performance Improvements

  • helper: avoid unnecessary compares (a5f1312)
  • helper: compare type of nodes directly (41ff13f)
  • helper: use switch statement over if else chain (b2874ad)
  • track removed calls to avoid crawling the AST (28188a8)
  • proptypes: skip traversing child nodes (ec883af)
  • combine visitors (#8) (d6a9a62)

2.3.0 (2019-07-18)

Bug Fixes

  • handle template and string literals correctly (f90ddaf)

Build System

  • rimraf dist before each build (d3f9f1f)

Features

  • removecalls: handle arrays (524fa51)
  • stripliterals: handle conditionalExpressions (18e35ba)
  • collect calls before optimizing (c2537e5)
  • combinestrings: support template literals (3d63596)
  • removecalls: handle template literal (7ccf15f)
  • stripliterals: remove empty strings (8672550)

Tests

  • stripliterals: improve coverage (8b5c717)

2.2.0 (2019-05-31)

Features

  • createlookup: handle multiple checks (950dd82)
  • create object key lookups (0630c91)
  • combinestrings: handle strings in arrays (62c4ce9)
  • removecalls: handle nested conditional expressions (2110589)
  • removecalls: handle single logical expression (0c453b3)
  • removecalls: handle string and logical expression as argument (2b59da8)

Tests

  • remove retainLines (07dcd92)
  • test:dev should ignore output (eea4081)

2.1.0 (2019-05-27)

Bug Fixes

  • proptypes: handle isRequired and default value (aa244e3)

Build System

  • use rollup, babel, and terser (29e730f)

Features

  • combine string literals (0bc7671)
  • remove unnecessary function calls (3ea3d85)
  • remove unused imports (8d2ae5f)
  • strip literals (f206424)
  • use proptypes to minimize expressions (300b006)

2.0.0 (2019-05-21)

Bug Fixes

  • combine: don't create an arrayExpression with just one item (699d6f5)
  • extract: transform static keys into string literals (#2) (213ff5b)
  • helper: use count instead of total length (5ffec80)

Features

  • add support for creating conditional expressions (dab4b1a)
  • get function name from imports (#1) (0c18712)

Performance Improvements

  • combine: skip if argument length is less than 2 (2bc0714)

BREAKING CHANGES

  • No longer matches on all clsx and classnames function calls, instead looks for imports(and require) and uses the names specified there. If the file doesn't contain imports nothing will be done, to get the old behaviour back you can set the functionNames option to ['clsx', 'classnames']

1.1.2 (2019-05-07)

Bug Fixes

  • combine: don't compare node against itself (9b990f6)

1.1.1 (2019-05-07)

Bug Fixes

  • combine: don't assume jagged array has dimension [x][1] (3b308a1)
  • combine: skip if no node appears more than once (f888818)
  • use lodash to compare nodes (b10733e)

1.1.0 (2019-05-05)

Bug Fixes

  • babel/types should not be a devDependency (36107ce)

Features

  • add support for classNames (f8de735)
  • add support for combining arguments (5708852)
  • handle exceptions correctly (7a5c96c)

1.0.1 (2019-05-04)

1.0.0 (2019-05-04)

Features

  • add support for optimizing objects (299d0b9)