1
0
mirror of https://github.com/S2-/minifyfromhtml.git synced 2025-08-03 04:10:04 +02:00

add some packages

This commit is contained in:
s2
2018-05-05 13:54:07 +02:00
parent 48c1138518
commit ff6e20677d
3738 changed files with 215920 additions and 0 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,73 @@
"use strict";
module.exports = function ({
types: t
}) {
const 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) {
const node = path.node;
const left = path.get("left");
const 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
const shouldBail = !path.parentPath.isExpressionStatement();
if (node.operator === "&&") {
const 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 === "||") {
const 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) {
const 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)) {
const newNode = flipExpressions.flip(node, true);
path.replaceWith(newNode);
}
}]
}
}
};
};

View File

@@ -0,0 +1,49 @@
{
"_from": "babel-plugin-minify-guarded-expressions@^0.4.1",
"_id": "babel-plugin-minify-guarded-expressions@0.4.1",
"_inBundle": false,
"_integrity": "sha1-ylpZoGvBwi3Vz9mWpnUWOm9hm30=",
"_location": "/babel-plugin-minify-guarded-expressions",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "babel-plugin-minify-guarded-expressions@^0.4.1",
"name": "babel-plugin-minify-guarded-expressions",
"escapedName": "babel-plugin-minify-guarded-expressions",
"rawSpec": "^0.4.1",
"saveSpec": null,
"fetchSpec": "^0.4.1"
},
"_requiredBy": [
"/babel-preset-minify"
],
"_resolved": "https://registry.npmjs.org/babel-plugin-minify-guarded-expressions/-/babel-plugin-minify-guarded-expressions-0.4.1.tgz",
"_shasum": "ca5a59a06bc1c22dd5cfd996a675163a6f619b7d",
"_spec": "babel-plugin-minify-guarded-expressions@^0.4.1",
"_where": "/home/s2/Documents/Code/minifyfromhtml/node_modules/babel-preset-minify",
"author": {
"name": "amasad"
},
"bugs": {
"url": "https://github.com/babel/minify/issues"
},
"bundleDependencies": false,
"dependencies": {
"babel-helper-flip-expressions": "^0.4.1"
},
"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.4.1"
}