-
-
Save asbjornh/439b440632484cd67b2d95698ac22335 to your computer and use it in GitHub Desktop.
Babel examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { generate, parse, t, traverse } = require('./babel'); | |
| const source = ` | |
| const babel = "cool"; | |
| function test(babel) { | |
| return babel; | |
| } | |
| `; | |
| // var babel = "cool"; | |
| const ast = parse(source); | |
| traverse(ast, { | |
| VariableDeclaration: path => { | |
| path.node.kind = 'var'; | |
| }, | |
| Function: path => { | |
| path.scope.rename('babel', 'newname'); | |
| } | |
| }); | |
| const { code } = generate(ast); | |
| console.log(code); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { generate, parse, t, traverse } = require('./babel'); | |
| const source = `[1, 2].map(n => n + 1);`; | |
| // [1, 2].map(function(n) { return n + 1 }); | |
| const ast = parse(source); | |
| traverse(ast, { | |
| ArrowFunctionExpression: path => { | |
| path.replaceWith( | |
| t.functionExpression( | |
| null, | |
| path.node.params, | |
| t.blockStatement([t.returnStatement(path.node.body)]) | |
| ) | |
| ); | |
| } | |
| }); | |
| const { code } = generate(ast); | |
| console.log(code); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { generate, parse, t, traverse } = require('./babel'); | |
| const source = `<div className="blah" id="id">Hello</div>`; | |
| // React.createElement("div", { className: "blah" }, "Hello"); | |
| const ast = parse(source, { plugins: ['jsx'] }); | |
| const pragma = t.memberExpression( | |
| t.identifier('React'), | |
| t.identifier('createElement') | |
| ); | |
| traverse(ast, { | |
| JSXElement: path => { | |
| path.replaceWith( | |
| t.callExpression(pragma, [ | |
| t.stringLiteral(path.node.openingElement.name.name), | |
| t.objectExpression( | |
| path.node.openingElement.attributes.map(attr => | |
| t.objectProperty(t.identifier(attr.name.name), attr.value) | |
| ) | |
| ), | |
| ...path.node.children.map(child => t.stringLiteral(child.value)) | |
| ]) | |
| ); | |
| } | |
| }); | |
| const { code } = generate(ast); | |
| console.log(code); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const generate = require('@babel/generator').default; | |
| const { parse } = require('@babel/parser'); | |
| const t = require('@babel/types'); | |
| const traverse = require('@babel/traverse').default; | |
| module.exports = { generate, parse, t, traverse }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "babel-demo", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "test": "echo \"Error: no test specified\" && exit 1" | |
| }, | |
| "author": "Asbjørn Hegdahl", | |
| "license": "MIT", | |
| "dependencies": { | |
| "@babel/generator": "^7.4.0", | |
| "@babel/parser": "^7.4.2", | |
| "@babel/traverse": "^7.4.0", | |
| "@babel/types": "^7.4.0" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment