add more plugins, rename some
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
node_modules
|
||||
*.log
|
||||
src
|
||||
@@ -0,0 +1,35 @@
|
||||
# babel-plugin-transform-react-constant-elements
|
||||
|
||||
Treat React JSX elements as value types and hoist them to the highest scope
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
$ npm install babel-plugin-transform-react-constant-elements
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Via `.babelrc` (Recommended)
|
||||
|
||||
**.babelrc**
|
||||
|
||||
```json
|
||||
{
|
||||
"plugins": ["transform-react-constant-elements"]
|
||||
}
|
||||
```
|
||||
|
||||
### Via CLI
|
||||
|
||||
```sh
|
||||
$ babel --plugins transform-react-constant-elements script.js
|
||||
```
|
||||
|
||||
### Via Node API
|
||||
|
||||
```javascript
|
||||
require("babel-core").transform("code", {
|
||||
plugins: ["transform-react-constant-elements"]
|
||||
});
|
||||
```
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "babel-plugin-transform-react-constant-elements",
|
||||
"version": "1.0.3",
|
||||
"description": "Treat React JSX elements as value types and hoist them to the highest scope",
|
||||
"repository": "babel/babel",
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"keywords": [
|
||||
"babel-plugin"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
export default function () {
|
||||
var immutabilityVisitor = {
|
||||
enter(path, state) {
|
||||
var stop = () => {
|
||||
state.isImmutable = false;
|
||||
path.stop();
|
||||
};
|
||||
|
||||
if (path.isJSXClosingElement()) {
|
||||
path.skip();
|
||||
return;
|
||||
}
|
||||
|
||||
if (path.isJSXIdentifier({ name: "ref" }) && path.parentPath.isJSXAttribute({ name: path.node })) {
|
||||
return stop();
|
||||
}
|
||||
|
||||
if (path.isJSXIdentifier() || path.isIdentifier() || path.isJSXMemberExpression()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!path.isImmutable()) stop();
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
visitor: {
|
||||
JSXElement(path) {
|
||||
if (path.node._hoisted) return;
|
||||
|
||||
var state = { isImmutable: true };
|
||||
path.traverse(immutabilityVisitor, state);
|
||||
|
||||
if (state.isImmutable) {
|
||||
path.hoist();
|
||||
} else {
|
||||
path.node._hoisted = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user