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,51 @@
# babel-plugin-transform-regexp-constructors
This changes RegExp constructors into literals if the RegExp arguments are strings.
## Example
**In**
```javascript
const foo = 'ab+';
var a = new RegExp(foo+'c', 'i');
```
**Out**
```javascript
const foo = 'ab+';
var a = /ab+c/i;
```
## Installation
```sh
npm install babel-plugin-transform-regexp-constructors
```
## Usage
### Via `.babelrc` (Recommended)
**.babelrc**
```json
{
"plugins": ["transform-regexp-constructors"]
}
```
### Via CLI
```sh
babel --plugins transform-regexp-constructors script.js
```
### Via Node API
```javascript
require("@babel/core").transform("code", {
plugins: ["transform-regexp-constructors"]
});
```

View File

@@ -0,0 +1,53 @@
"use strict";
function createRegExpLiteral(args, prettify, t) {
const evaluatedArgs = args.map(a => a.evaluate());
if (!evaluatedArgs.every(a => a.confident && typeof a.value === "string")) {
return;
}
let pattern = evaluatedArgs.length >= 1 && evaluatedArgs[0].value !== "" ? evaluatedArgs[0].value : "(?:)";
const flags = evaluatedArgs.length >= 2 ? evaluatedArgs[1].value : "";
pattern = new RegExp(pattern).source;
if (prettify) {
pattern = pattern.replace(/\n/g, "\\n").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029").replace(/[\b]/g, "[\\b]").replace(/\v/g, "\\v").replace(/\f/g, "\\f").replace(/\r/g, "\\r");
}
pattern = pattern.replace(/\0/g, "\\0");
return t.regExpLiteral(pattern, flags);
}
function maybeReplaceWithRegExpLiteral(path, t) {
if (!t.isIdentifier(path.node.callee, {
name: "RegExp"
})) {
return;
}
const regExpLiteral = createRegExpLiteral(path.get("arguments"), true, t);
if (regExpLiteral) {
path.replaceWith(regExpLiteral);
}
}
module.exports = function ({
types: t
}) {
return {
name: "transform-regexp-constructors",
visitor: {
NewExpression(path) {
maybeReplaceWithRegExpLiteral(path, t);
},
CallExpression(path) {
// equivalent to `new RegExp()` according to §21.2.3
maybeReplaceWithRegExpLiteral(path, t);
}
}
};
};

View File

@@ -0,0 +1,46 @@
{
"_from": "babel-plugin-transform-regexp-constructors@^0.4.1",
"_id": "babel-plugin-transform-regexp-constructors@0.4.1",
"_inBundle": false,
"_integrity": "sha1-T/fx2g4MMZENDhRhq+0MZ5yzHu4=",
"_location": "/babel-plugin-transform-regexp-constructors",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "babel-plugin-transform-regexp-constructors@^0.4.1",
"name": "babel-plugin-transform-regexp-constructors",
"escapedName": "babel-plugin-transform-regexp-constructors",
"rawSpec": "^0.4.1",
"saveSpec": null,
"fetchSpec": "^0.4.1"
},
"_requiredBy": [
"/babel-preset-minify"
],
"_resolved": "https://registry.npmjs.org/babel-plugin-transform-regexp-constructors/-/babel-plugin-transform-regexp-constructors-0.4.1.tgz",
"_shasum": "4ff7f1da0e0c31910d0e1461abed0c679cb31eee",
"_spec": "babel-plugin-transform-regexp-constructors@^0.4.1",
"_where": "/home/s2/Documents/Code/minifyfromhtml/node_modules/babel-preset-minify",
"author": {
"name": "shinew"
},
"bugs": {
"url": "https://github.com/babel/minify/issues"
},
"bundleDependencies": false,
"deprecated": false,
"description": "This changes RegExp constructors into literals if the RegExp arguments are strings.",
"homepage": "https://github.com/babel/minify#readme",
"keywords": [
"babel-plugin"
],
"license": "MIT",
"main": "lib/index.js",
"name": "babel-plugin-transform-regexp-constructors",
"repository": {
"type": "git",
"url": "https://github.com/babel/minify/tree/master/packages/babel-plugin-transform-regexp-constructors"
},
"version": "0.4.1"
}