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