WIP - add extractor, generate snippet_data

This commit is contained in:
Stefan Fejes
2019-08-20 15:52:05 +02:00
parent 88084d3d30
commit cc8f1d8a7a
37396 changed files with 4588842 additions and 133 deletions

View File

@ -0,0 +1,49 @@
# babel-plugin-minify-guarded-expressions
## Example
**In**
```javascript
!x && foo();
alert(0 && new Foo());
```
**Out**
```javascript
x || foo();
alert(0);
```
## Installation
```sh
npm install babel-plugin-minify-guarded-expressions
```
## Usage
### Via `.babelrc` (Recommended)
**.babelrc**
```json
{
"plugins": ["minify-guarded-expressions"]
}
```
### Via CLI
```sh
babel --plugins minify-guarded-expressions script.js
```
### Via Node API
```javascript
require("babel-core").transform("code", {
plugins: ["minify-guarded-expressions"]
});
```

View File

@ -0,0 +1,75 @@
"use strict";
module.exports = function (_ref) {
var t = _ref.types;
var flipExpressions = require("babel-helper-flip-expressions")(t);
return {
name: "minify-guarded-expressions",
visitor: {
// Convert guarded expressions
// !a && b() --> a || b();
// This could change the return result of the expression so we only do it
// on things where the result is ignored.
LogicalExpression: {
enter: [function (path) {
var node = path.node;
var left = path.get("left");
var right = path.get("right");
// issues - 171, 174, 176
// we assume that it is returned/assigned/part of a bigger expression
// or utilized somehow
// we check if we shouldBail only when evaluating
// the rightside of the expression;
// if the left side is evaluated to be deterministic,
// we can safely replace the entire expression
var shouldBail = !path.parentPath.isExpressionStatement();
if (node.operator === "&&") {
var leftTruthy = left.evaluateTruthy();
if (leftTruthy === false) {
// Short-circuit
path.replaceWith(node.left);
} else if (leftTruthy === true && left.isPure()) {
path.replaceWith(node.right);
} else if (right.evaluateTruthy() === false && right.isPure() && !shouldBail) {
path.replaceWith(node.left);
}
} else if (node.operator === "||") {
var _leftTruthy = left.evaluateTruthy();
if (_leftTruthy === false && left.isPure()) {
path.replaceWith(node.right);
} else if (_leftTruthy === true) {
// Short-circuit
path.replaceWith(node.left);
} else if (right.evaluateTruthy() === false && right.isPure() && !shouldBail) {
path.replaceWith(node.left);
}
}
}, function (path) {
var node = path.node;
if (flipExpressions.hasSeen(node)) {
return;
}
if (!path.parentPath.isExpressionStatement() && !(path.parentPath.isSequenceExpression() && path.parentPath.parentPath.isExpressionStatement())) {
return;
}
// Start counting savings from one since we can ignore the last
// expression.
if (flipExpressions.shouldFlip(node, 1)) {
var newNode = flipExpressions.flip(node, true);
path.replaceWith(newNode);
}
}]
}
}
};
};

View File

@ -0,0 +1,49 @@
{
"_from": "babel-plugin-minify-guarded-expressions@^0.3.0",
"_id": "babel-plugin-minify-guarded-expressions@0.3.0",
"_inBundle": false,
"_integrity": "sha512-O+6CvF5/Ttsth3LMg4/BhyvVZ82GImeKMXGdVRQGK/8jFiP15EjRpdgFlxv3cnqRjqdYxLCS6r28VfLpb9C/kA==",
"_location": "/babel-plugin-minify-guarded-expressions",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "babel-plugin-minify-guarded-expressions@^0.3.0",
"name": "babel-plugin-minify-guarded-expressions",
"escapedName": "babel-plugin-minify-guarded-expressions",
"rawSpec": "^0.3.0",
"saveSpec": null,
"fetchSpec": "^0.3.0"
},
"_requiredBy": [
"/babel-preset-minify"
],
"_resolved": "https://registry.npmjs.org/babel-plugin-minify-guarded-expressions/-/babel-plugin-minify-guarded-expressions-0.3.0.tgz",
"_shasum": "2552d96189ef45d9a463f1a6b5e4fa110703ac8d",
"_spec": "babel-plugin-minify-guarded-expressions@^0.3.0",
"_where": "/Users/stefanfejes/Projects/30-seconds-of-python-code/node_modules/babel-preset-minify",
"author": {
"name": "amasad"
},
"bugs": {
"url": "https://github.com/babel/minify/issues"
},
"bundleDependencies": false,
"dependencies": {
"babel-helper-flip-expressions": "^0.3.0"
},
"deprecated": false,
"description": "## Example",
"homepage": "https://github.com/babel/minify#readme",
"keywords": [
"babel-plugin"
],
"license": "MIT",
"main": "lib/index.js",
"name": "babel-plugin-minify-guarded-expressions",
"repository": {
"type": "git",
"url": "https://github.com/babel/minify/tree/master/packages/babel-plugin-minify-guarded-expressions"
},
"version": "0.3.0"
}