add more plugins, rename some
This commit is contained in:
3
packages/babel-plugin-transform-react-jsx/.npmignore
Normal file
3
packages/babel-plugin-transform-react-jsx/.npmignore
Normal file
@@ -0,0 +1,3 @@
|
||||
node_modules
|
||||
*.log
|
||||
src
|
||||
35
packages/babel-plugin-transform-react-jsx/README.md
Normal file
35
packages/babel-plugin-transform-react-jsx/README.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# babel-plugin-transform-react-jsx
|
||||
|
||||
Turn JSX into React function calls
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
$ npm install babel-plugin-transform-react-jsx
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Via `.babelrc` (Recommended)
|
||||
|
||||
**.babelrc**
|
||||
|
||||
```json
|
||||
{
|
||||
"plugins": ["transform-react-jsx"]
|
||||
}
|
||||
```
|
||||
|
||||
### Via CLI
|
||||
|
||||
```sh
|
||||
$ babel --plugins transform-react-jsx script.js
|
||||
```
|
||||
|
||||
### Via Node API
|
||||
|
||||
```javascript
|
||||
require("babel-core").transform("code", {
|
||||
plugins: ["transform-react-jsx"]
|
||||
});
|
||||
```
|
||||
15
packages/babel-plugin-transform-react-jsx/package.json
Normal file
15
packages/babel-plugin-transform-react-jsx/package.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "babel-plugin-transform-react-jsx",
|
||||
"version": "1.0.0",
|
||||
"description": "Turn JSX into React function calls",
|
||||
"repository": "babel/babel",
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"keywords": [
|
||||
"babel-plugin"
|
||||
],
|
||||
"dependencies": {
|
||||
"babel-runtime": "^5.0.0",
|
||||
"babel-plugin-transform-builder-react-jsx": "^1.0.0"
|
||||
}
|
||||
}
|
||||
42
packages/babel-plugin-transform-react-jsx/src/index.js
Normal file
42
packages/babel-plugin-transform-react-jsx/src/index.js
Normal file
@@ -0,0 +1,42 @@
|
||||
export default function ({ types: t }) {
|
||||
var JSX_ANNOTATION_REGEX = /^\*\s*@jsx\s+([^\s]+)/;
|
||||
|
||||
var visitor = require("babel-plugin-builder-react-jsx")(t, {
|
||||
pre(state) {
|
||||
var tagName = state.tagName;
|
||||
var args = state.args;
|
||||
if (t.react.isCompatTag(tagName)) {
|
||||
args.push(t.stringLiteral(tagName));
|
||||
} else {
|
||||
args.push(state.tagExpr);
|
||||
}
|
||||
},
|
||||
|
||||
post(state, file) {
|
||||
state.callee = file.get("jsxIdentifier");
|
||||
}
|
||||
});
|
||||
|
||||
visitor.Program = function (path, file) {
|
||||
var id = "React.createElement"; // todo: jsxPragma;
|
||||
|
||||
for (var i = 0; i < file.ast.comments.length; i++) {
|
||||
var comment = file.ast.comments[i];
|
||||
var matches = JSX_ANNOTATION_REGEX.exec(comment.value);
|
||||
if (matches) {
|
||||
id = matches[1];
|
||||
if (id === "React.DOM") {
|
||||
throw file.buildCodeFrameError(comment, "The @jsx React.DOM pragma has been deprecated as of React 0.12");
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
file.set("jsxIdentifier", id.split(".").map(t.identifier).reduce(function (object, property) {
|
||||
return t.memberExpression(object, property);
|
||||
}));
|
||||
};
|
||||
|
||||
return { visitor };
|
||||
}
|
||||
Reference in New Issue
Block a user