1010[ ![ Code Coverage] [ coverage-badge ]] [ coverage ]
1111[ ![ version] [ version-badge ]] [ package ]
1212[ ![ downloads] [ downloads-badge ]] [ npmcharts ]
13- [ ![ MIT License] [ license-badge ]] [ LICENSE ]
13+ [ ![ MIT License] [ license-badge ]] [ license ]
1414
1515[ ![ All Contributors] ( https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square )] ( #contributors )
1616[ ![ PRs Welcome] [ prs-badge ]] [ prs ]
1717[ ![ Code of Conduct] [ coc-badge ]] [ coc ]
18- [ ![ Babel Macro] ( https://img.shields.io/badge/babel--macro-%F0%9F%8E%A3-f5da55.svg?style=flat-square )] ( https://github.com/kentcdodds/babel-macros )
18+ [ ![ Babel Macro] ( https://img.shields.io/badge/babel--macro-%F0%9F%8E%A3-f5da55.svg?style=flat-square )] ( https://github.com/kentcdodds/babel-plugin- macros )
1919
2020[ ![ Watch on GitHub] [ github-watch-badge ]] [ github-watch ]
2121[ ![ Star on GitHub] [ github-star-badge ]] [ github-star ]
2222[ ![ Tweet] [ twitter-badge ]] [ twitter ]
2323
24- <a href =" https://app.codesponsor.io/link/PKGFLnhDiFvsUA5P4kAXfiPs/kentcdodds/babel-plugin-codegen " rel =" nofollow " ><img src =" https://app.codesponsor.io/embed/PKGFLnhDiFvsUA5P4kAXfiPs/kentcdodds/babel-plugin-codegen.svg " style =" width : 888px ; height : 68px ;" alt =" Sponsor " /></a >
25-
2624## The problem
2725
2826The applications of this plugin are wide, so it's kinda hard to sum it up, but
@@ -45,6 +43,31 @@ comment directive) and requiring it as a module. Then it takes whatever the
4543export was (which should be a string) and converts that string to an AST node
4644and swaps your usage node with the new AST node.
4745
46+ ## Table of Contents
47+
48+ <!-- START doctoc generated TOC please keep comment here to allow auto update -->
49+
50+ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
51+
52+ * [ Installation] ( #installation )
53+ * [ Usage] ( #usage )
54+ * [ Template Tag] ( #template-tag )
55+ * [ import comment] ( #import-comment )
56+ * [ codegen.require] ( #codegenrequire )
57+ * [ codegen file comment (` // @codegen ` )] ( #codegen-file-comment--codegen )
58+ * [ Configure with Babel] ( #configure-with-babel )
59+ * [ Via ` .babelrc ` (Recommended)] ( #via-babelrc-recommended )
60+ * [ Via CLI] ( #via-cli )
61+ * [ Via Node API] ( #via-node-api )
62+ * [ Use with [ ` babel-plugin-macros ` ] [ babel-plugin-macros ]] ( #use-with-babel-plugin-macrosbabel-plugin-macros )
63+ * [ Caveats] ( #caveats )
64+ * [ Inspiration] ( #inspiration )
65+ * [ Other Solutions] ( #other-solutions )
66+ * [ Contributors] ( #contributors )
67+ * [ LICENSE] ( #license )
68+
69+ <!-- END doctoc generated TOC please keep comment here to allow auto update -->
70+
4871## Installation
4972
5073This module is distributed via [ npm] [ npm ] which is bundled with [ node] [ node ] and
@@ -62,6 +85,7 @@ actually replacing it with code (giving you a little bit more power in exchange
6285for potentially being a little more confusing).
6386
6487Important notes:
88+
65891 . All code run by ` codegen ` is _ not_ run in a sandboxed environment
66902 . All code _ must_ run synchronously.
67913 . All code will be transpiled via ` babel-core ` directly or ` babel-register `
@@ -72,8 +96,6 @@ Important notes:
72964 . The code that's generated may or may not be transpiled (babel plugin ordering
7397 is tricky business). ** You should generate the code that you wish to ship.**
7498
75- TODO...
76-
7799### Template Tag
78100
79101** Before** :
@@ -88,7 +110,7 @@ codegen`
88110** After** (assuming ` some-code.js ` contains the text: ` var x = 'Hello world!' ` ):
89111
90112``` javascript
91- var x = ' Hello world!' ;
113+ var x = ' Hello world!'
92114```
93115
94116` codegen ` can also handle _ some_ simple dynamic values as well:
@@ -118,7 +140,7 @@ import /* codegen */ './assign-one.js'
118140** After** (` assign-one.js ` is: ` module.exports = 'var x = 1' ` ):
119141
120142``` javascript
121- var x = 1 ;
143+ var x = 1
122144```
123145
124146You can also provide arguments! In this case, the module you import should
@@ -133,7 +155,7 @@ import /* codegen(3) */ './assign-identity'
133155** After** (` assign-identity.js ` is: ` module.exports = input => 'var x = ' + JSON.stringify(input) + ';' ` ):
134156
135157``` javascript
136- var x = 3 ;
158+ var x = 3
137159```
138160
139161### codegen.require
@@ -147,7 +169,7 @@ const x = codegen.require('./es6-identity', 3)
147169** After** (` es6-identity.js ` is: ` export default input => 'var x = ' + JSON.stringify(input) + ';' ` ):
148170
149171``` javascript
150- const x = 3 ;
172+ const x = 3
151173```
152174
153175### codegen file comment (` // @codegen ` )
@@ -169,9 +191,9 @@ module.exports = array
169191** After** :
170192
171193``` javascript
172- export const apple = ' apple' ;
173- export const orange = ' orange' ;
174- export const pear = ' pear' ;
194+ export const apple = ' apple'
195+ export const orange = ' orange'
196+ export const pear = ' pear'
175197```
176198
177199## Configure with Babel
@@ -200,9 +222,9 @@ require('babel-core').transform('code', {
200222})
201223```
202224
203- ## Use with [ ` babel-macros ` ] [ babel-macros ]
225+ ## Use with [ ` babel-plugin- macros ` ] [ babel-plugin -macros ]
204226
205- Once you've [ configured ` babel-macros ` ] ( https://github.com/kentcdodds/babel-macros/blob/master/other/docs/user.md )
227+ Once you've [ configured ` babel-plugin- macros ` ] ( https://github.com/kentcdodds/babel-plugin -macros/blob/master/other/docs/user.md )
206228you can import/require the codegen macro at ` babel-plugin-codegen/macro ` .
207229For example:
208230
@@ -244,8 +266,11 @@ here!
244266Thanks goes to these people ([ emoji key] [ emojis ] ):
245267
246268<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
247- | [ <img src =" https://avatars.githubusercontent.com/u/1500684?v=3 " width =" 100px; " /><br /><sub >Kent C. Dodds</sub >] ( https://kentcdodds.com ) <br />[ 💻] ( https://github.com/kentcdodds/babel-plugin-codegen/commits?author=kentcdodds ) [ 📖] ( https://github.com/kentcdodds/babel-plugin-codegen/commits?author=kentcdodds ) 🚇 [ ⚠️] ( https://github.com/kentcdodds/babel-plugin-codegen/commits?author=kentcdodds ) |
269+
270+ <!-- prettier-ignore -->
271+ | [ <img src =" https://avatars.githubusercontent.com/u/1500684?v=3 " width =" 100px; " /><br /><sub ><b >Kent C. Dodds</b ></sub >] ( https://kentcdodds.com ) <br />[ 💻] ( https://github.com/kentcdodds/babel-plugin-codegen/commits?author=kentcdodds " Code ") [ 📖] ( https://github.com/kentcdodds/babel-plugin-codegen/commits?author=kentcdodds " Documentation ") [ 🚇] ( #infra-kentcdodds " Infrastructure (Hosting, Build-Tools, etc) ") [ ⚠️] ( https://github.com/kentcdodds/babel-plugin-codegen/commits?author=kentcdodds " Tests ") |
248272| :---: |
273+
249274<!-- ALL-CONTRIBUTORS-LIST:END -->
250275
251276This project follows the [ all-contributors] [ all-contributors ] specification.
283308[ glamorous ] : https://github.com/paypal/glamorous
284309[ preval ] : https://github.com/kentcdodds/babel-plugin-preval
285310[ codegen.macro ] : https://www.npmjs.com/package/codegen.macro
286- [ babel-macros ] : https://github.com/kentcdodds/babel-macros
311+ [ babel-plugin- macros ] : https://github.com/kentcdodds/babel-plugin -macros
0 commit comments