Last active
December 11, 2018 11:17
-
-
Save chardos/4d065b9e4f32a7f38a4314c683b8891a to your computer and use it in GitHub Desktop.
This file contains 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
// wrap-transform.js | |
const fs = require('fs'); | |
const parser = require('@babel/parser').parse; | |
const traverse = require('@babel/traverse').default; | |
const generate = require('@babel/generator').default; | |
const prettier = require('prettier'); | |
const file = fs.readFileSync('wrap-before.js').toString(); | |
const ast = parser(file, {sourceType: 'module', plugins: ['jsx']}); | |
let exportDefaultPath; | |
traverse(ast, { | |
ExportDefaultDeclaration(path) { | |
exportDefaultPath = path; | |
}, | |
}) | |
const declarationName = exportDefaultPath.node.declaration.name; | |
const reducerNames = ['volleyball', 'soccer'].join(','); | |
exportDefaultPath.replaceWith( | |
parser( | |
` | |
const mapStateToProps = ({${reducerNames}}) => ({${reducerNames}}) | |
export default connect(mapStateToProps)(${declarationName}) | |
`, | |
{sourceType: 'module'} | |
) | |
) | |
const newCode = generate(ast).code; | |
const prettifiedCode = prettier.format(newCode, { parser: 'babylon' }) | |
fs.writeFile('wrap-after.js', prettifiedCode, (err) => { | |
if (err) throw new Error(err) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment