Generate code at build-time
The applications of this plugin are wide, so it's kinda hard to sum it up, but
basically my use case was I needed to add a bunch of named exports to
glamorous (one for every DOM node type) and I didn't want to
maintain the exports in my source file. So someone created a post-build script
to concatenate them to the end of the file. I built this plugin so I could do
that without having an ad-hoc post-build script.
Read "Make maintainable workarounds with codegen 💥" for more inspiration
This plugin allows you to generate code at build-time. Any code that runs synchronously in node can be used to generate a string of code and that string will be inserted in place of where your usage appears.
It works by accepting your code string (or module when using the
comment directive) and requiring it as a module. Then it takes whatever the
export was (which should be a string) and converts that string to an AST node
and swaps your usage node with the new AST node.
Table of Contents
- Configure with Babel
- Use with
- Other Solutions
npm install --save-dev babel-plugin-codegen
This package works in a very similar way to
except in this case instead of any value being replaced in the code, you're
actually replacing it with code (giving you a little bit more power in exchange
for potentially being a little more confusing).
- All code run by
codegenis not run in a sandboxed environment
- All code must run synchronously.
You may like to watch this YouTube video to get an idea of what codegen is and how it can be used.
codegen`const fs = require('fs')module.exports = fs.readFileSync(require.resolve('./some-code.js'), 'utf8')`
some-code.js contains the text:
var x = 'Hello world!'):
var x = 'Hello world!'
codegen can also handle some simple dynamic values as well:
const three = 3const x = codegen`module.exports = ''`
const three = 3const x = 3
module.exports = 'var x = 1'):
var x = 1
You can also provide arguments! In this case, the module you import should export a function which accepts those arguments and returns a string.
module.exports = input => 'var x = ' + JSON.stringify(input) + ';'):
var x = 3
const x = codegen
export default input => 'var x = ' + JSON.stringify(input) + ';'):
const x = 3
codegen file comment (
Using the codegen file comment will update a whole file to be evaluated down to an export.
Whereas the above usages (assignment/import/require) will only codegen the scope of the assignment or file being imported.
// @codegenconst array = 'apple' 'orange' 'pear'moduleexports = array
const apple = 'apple'const orange = 'orange'const pear = 'pear'
Configure with Babel
babel --plugins codegen script.js
Via Node API
you can import/require the codegen macro at
codegen`module.exports = ['a', 'b', 'c'].map(l => 'export const ' + l + ' = ' + JSON.stringify(l)).join(';')`↓ ↓ ↓ ↓ ↓ ↓const a = "a";const b = "b";const c = "c";
APIs not supported by the macro
You could also use
codegen.macroif you'd prefer to type less 😀
One really important thing to note here is that it doesn't work by simply replacing your code with whatever string you export. Instead it replaces it at the AST level. This means that the resulting code should operate the same, but the format of the code could be entirely different. Most of the time this should not matter, but if it matters to you, please feel free to contribute back if you feel like you could make it work!
- Using babel macros with React Native: A
practical use case for solving an i18n problem using
I'm not aware of any, if you are please make a pull request and add it here!
Thanks goes to these people (emoji key):
Kent C. Dodds
💻 📖 🚇 ⚠️
💻 📖 ⚠️
Jan Willem Henckel
💻 📖 ⚠️
This project follows the all-contributors specification. Contributions of any kind welcome!