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 - npm Package Compare versions

Comparing version 1.1.2 to 2.0.0

src/options.js

21

CHANGELOG.md

@@ -5,2 +5,23 @@ # Change Log

# [2.0.0](https://github.com/merceyz/babel-plugin-optimize-clsx/compare/v1.1.2...v2.0.0) (2019-05-21)
### Bug Fixes
- **combine:** don't create an arrayExpression with just one item ([699d6f5](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/699d6f5))
- **extract:** transform static keys into string literals ([#2](https://github.com/merceyz/babel-plugin-optimize-clsx/issues/2)) ([213ff5b](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/213ff5b))
- **helper:** use count instead of total length ([5ffec80](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/5ffec80))
### Features
- add support for creating conditional expressions ([dab4b1a](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/dab4b1a))
- get function name from imports ([#1](https://github.com/merceyz/babel-plugin-optimize-clsx/issues/1)) ([0c18712](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/0c18712))
### Performance Improvements
- **combine:** skip if argument length is less than 2 ([2bc0714](https://github.com/merceyz/babel-plugin-optimize-clsx/commit/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](https://github.com/merceyz/babel-plugin-optimize-clsx/compare/v1.1.1...v1.1.2) (2019-05-07)

@@ -7,0 +28,0 @@

5

package.json
{
"name": "babel-plugin-optimize-clsx",
"version": "1.1.2",
"version": "2.0.0",
"main": "src/index.js",

@@ -20,2 +20,3 @@ "files": [

"test": "jest",
"test:dev": "cross-env DEV_MODE=true jest",
"release": "standard-version",

@@ -31,4 +32,6 @@ "benchmark": "node benchmark/index.js"

"babel-jest": "^24.7.1",
"babel-plugin-tester": "^6.2.1",
"benchmark": "^2.1.4",
"clsx": "^1.0.4",
"cross-env": "^5.2.0",
"husky": "^2.2.0",

@@ -35,0 +38,0 @@ "jest": "^24.7.1",

65

README.md

@@ -15,3 +15,3 @@ # babel-plugin-optimize-clsx

### Extract objects
### Extract object properties

@@ -37,2 +37,23 @@ Transforms

### Extract and create conditional expression
Transforms
```javascript
clsx(
'foo',
{
[classes.disabled]: disabled,
[classes.focusVisible]: focusVisible && !disabled,
},
'bar',
);
```
to
```javascript
clsx('foo', 'bar', disabled ? classes.disabled : focusVisible && classes.focusVisible);
```
### Extract and combine

@@ -62,1 +83,43 @@

Benchmarks can be found in the [`benchmark`](/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.
```json
{
"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.
```json
{
"plugins": [
[
"babel-plugin-optimize-clsx",
{
"functionNames": ["myClsxImplementation"]
}
]
]
}
```

@@ -1,24 +0,28 @@

const t = require('@babel/types');
const extractArguments = require('./extractArguments');
const combineArguments = require('./combineArguments');
const getOptions = require('./options');
module.exports = () => {
return {
visitor: {
CallExpression: path => {
const { node } = path;
const { callee: c } = node;
if (t.isIdentifier(c) && (c.name === 'clsx' || c.name === 'classNames')) {
try {
let args = node.arguments;
args = extractArguments(args);
args = combineArguments(args);
node.arguments = args;
} catch (err) {
throw path.buildCodeFrameError(err);
const visitors = [
require('./visitors/findFunctionNames'),
require('./visitors/extractObjectProperties'),
require('./visitors/combineArguments'),
require('./visitors/createConditionalExpression'),
];
module.exports = () => ({
visitor: {
Program(path, state) {
const options = getOptions(state.opts);
try {
for (const visitor of visitors) {
visitor(path, options);
if (options.functionNames.length === 0) {
return;
}
}
},
} catch (err) {
throw path.buildCodeFrameError(err);
}
},
};
};
},
});
const t = require('@babel/types');
const fs = require('fs');
const path = require('path');
const compareNodes = require('./compareNodes');
const generate = require('@babel/generator');

@@ -29,3 +28,3 @@

let maxNode = null;
let maxLength = 0;
let maxCount = 0;

@@ -35,3 +34,3 @@ operators.forEach((row, row_index) => {

if (col_index === row.length - 1) return;
let length = 0;
let count = 0;

@@ -49,3 +48,3 @@ operators.forEach((row2, row2_index) => {

if (compareNodes(col, col2)) {
length += col.end - col.start;
count += 1;
}

@@ -55,5 +54,5 @@ });

if (length > maxLength) {
if (count > maxCount) {
maxNode = col;
maxLength = length;
maxCount = count;
}

@@ -96,3 +95,11 @@ });

const isEqualWith = require('lodash/isEqualWith');
function compareNodes(obj1, obj2) {
return isEqualWith(obj1, obj2, (v1, v2, key) => {
return key === 'start' || key === 'end' || key === 'loc' ? true : undefined;
});
}
module.exports = {
compareNodes,
dumpData,

@@ -99,0 +106,0 @@ flattenLogicalOperator,

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