Skip to content

Commit 1b17619

Browse files
djfarlyKent C. Dodds
authored andcommitted
feat: drop direct babel dependency + allowing dynamicImport, objectRestSpread (#12)
* update babel version * update babel in getReplacement, replace and README * format as json * use babel object supplied by plugin api for everything but register * .eslintcache really wants to belong to the codebase * implement @mlrawlings poc, remove dep for babel/core, /register * fix typos * added myself to the contributers * bump babel plugin macros version again * pass parserOpts to template fn * added some test to assure correct behaviour of parserOpts * pass plugins and presets to child transforms * trust parent babel to pass the correct parserOpts, including plugins * codeToAst does not need to be a func anymore (it screws with test coverage somehow) * update readme to reflect changes * fix formatting for md files BREAKING CHANGE: We are inheriting the babel configurations of the main babel-process, instead of starting a new one. Thus we're not reading the .babelrc.
1 parent 29c7b51 commit 1b17619

14 files changed

Lines changed: 354 additions & 259 deletions

.all-contributorsrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@
2929
"doc",
3030
"test"
3131
]
32+
},
33+
{
34+
"login": "djfarly",
35+
"name": "Jan Willem Henckel",
36+
"avatar_url": "https://avatars3.githubusercontent.com/u/5230863?v=4",
37+
"profile": "https://jan.cologne",
38+
"contributions": [
39+
"code",
40+
"doc",
41+
"test"
42+
]
3243
}
3344
],
3445
"repoType": "github"

.github/ISSUE_TEMPLATE.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ never done that before, that's great! Check this free short video tutorial to
1111
learn how: http://kcd.im/pull-request
1212
-->
1313

14-
* `babel-plugin-codegen` version:
15-
* `node` version:
16-
* `npm` (or `yarn`) version:
14+
- `babel-plugin-codegen` version:
15+
- `node` version:
16+
- `npm` (or `yarn`) version:
1717

1818
Relevant code or config
1919

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ merge of your pull request!
3434

3535
<!-- to check an item, place an "x" in the box like so: "- [x] Documentation" -->
3636

37-
* [ ] Documentation
38-
* [ ] Tests
39-
* [ ] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->
40-
* [ ] Added myself to contributors table <!-- this is optional, see the contributing guidelines for instructions -->
37+
- [ ] Documentation
38+
- [ ] Tests
39+
- [ ] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->
40+
- [ ] Added myself to contributors table <!-- this is optional, see the contributing guidelines for instructions -->
4141

4242
<!-- feel free to add additional comments -->

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ dist
44
.opt-in
55
.opt-out
66
.DS_Store
7+
.eslintcache
78

89
# these cause more harm than good
910
# when working with contributors

README.md

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88

99
[![Build Status][build-badge]][build]
1010
[![Code Coverage][coverage-badge]][coverage]
11-
[![version][version-badge]][package]
12-
[![downloads][downloads-badge]][npmcharts]
11+
[![version][version-badge]][package] [![downloads][downloads-badge]][npmcharts]
1312
[![MIT License][license-badge]][license]
1413

15-
[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors)
16-
[![PRs Welcome][prs-badge]][prs]
17-
[![Code of Conduct][coc-badge]][coc]
14+
[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors)
15+
[![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc]
1816
[![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)
1917

2018
[![Watch on GitHub][github-watch-badge]][github-watch]
@@ -30,7 +28,9 @@ maintain the exports in my source file. So someone created a post-build script
3028
to concatenate them to the end of the file. I built this plugin so I could do
3129
that without having an ad-hoc post-build script.
3230

33-
> Read ["Make maintainable workarounds with codegen 💥"](https://blog.kentcdodds.com/make-maintainable-workarounds-with-codegen-d34163a09c13) for more inspiration
31+
> Read
32+
> ["Make maintainable workarounds with codegen 💥"](https://blog.kentcdodds.com/make-maintainable-workarounds-with-codegen-d34163a09c13)
33+
> for more inspiration
3434
3535
## This solution
3636

@@ -49,23 +49,23 @@ and swaps your usage node with the new AST node.
4949

5050
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
5151

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`](#use-with-babel-plugin-macros)
63-
* [APIs not supported by the macro](#apis-not-supported-by-the-macro)
64-
* [Caveats](#caveats)
65-
* [Inspiration](#inspiration)
66-
* [Other Solutions](#other-solutions)
67-
* [Contributors](#contributors)
68-
* [LICENSE](#license)
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`](#use-with-babel-plugin-macros)
63+
- [APIs not supported by the macro](#apis-not-supported-by-the-macro)
64+
- [Caveats](#caveats)
65+
- [Inspiration](#inspiration)
66+
- [Other Solutions](#other-solutions)
67+
- [Contributors](#contributors)
68+
- [LICENSE](#license)
6969

7070
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
7171

@@ -89,13 +89,12 @@ Important notes:
8989

9090
1. All code run by `codegen` is _not_ run in a sandboxed environment
9191
2. All code _must_ run synchronously.
92-
3. All code will be transpiled via `babel-core` directly or `babel-register`
93-
and should follow all of the normal rules for `.babelrc` resolution (the
94-
closest `.babelrc` to the file being run is the one that's used). This means
95-
you can rely on any babel plugins/transforms that you're used to using
96-
elsewhere in your codebase.
97-
4. The code that's generated may or may not be transpiled (babel plugin ordering
98-
is tricky business). **You should generate the code that you wish to ship.**
92+
3. All code will be transpiled via the same instance of babel this plugin is
93+
called with, thus inheriting all presets and plugins. This means you can
94+
rely on any babel plugins/transforms that you're used to using elsewhere in
95+
your codebase.
96+
4. The code that's generated might be transpiled. Please check the output to
97+
make sure. (babel plugin ordering is tricky business 😇)
9998

10099
### Template Tag
101100

@@ -153,7 +152,8 @@ export a function which accepts those arguments and returns a string.
153152
import /* codegen(3) */ './assign-identity'
154153
```
155154

156-
**After** (`assign-identity.js` is: `module.exports = input => 'var x = ' + JSON.stringify(input) + ';'`):
155+
**After** (`assign-identity.js` is:
156+
`module.exports = input => 'var x = ' + JSON.stringify(input) + ';'`):
157157

158158
```javascript
159159
var x = 3
@@ -167,17 +167,20 @@ var x = 3
167167
const x = codegen.require('./es6-identity', 3)
168168
```
169169

170-
**After** (`es6-identity.js` is: `export default input => 'var x = ' + JSON.stringify(input) + ';'`):
170+
**After** (`es6-identity.js` is:
171+
`export default input => 'var x = ' + JSON.stringify(input) + ';'`):
171172

172173
```javascript
173174
const x = 3
174175
```
175176

176177
### codegen file comment (`// @codegen`)
177178

178-
Using the codegen file comment will update a whole file to be evaluated down to an export.
179+
Using the codegen file comment will update a whole file to be evaluated down to
180+
an export.
179181

180-
Whereas the above usages (assignment/import/require) will only codegen the scope of the assignment or file being imported.
182+
Whereas the above usages (assignment/import/require) will only codegen the scope
183+
of the assignment or file being imported.
181184

182185
**Before**:
183186

@@ -225,9 +228,10 @@ require('babel-core').transform('code', {
225228

226229
## Use with `babel-plugin-macros`
227230

228-
Once you've [configured `babel-plugin-macros`](https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/user.md)
229-
you can import/require the codegen macro at `babel-plugin-codegen/macro`.
230-
For example:
231+
Once you've
232+
[configured `babel-plugin-macros`](https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/user.md)
233+
you can import/require the codegen macro at `babel-plugin-codegen/macro`. For
234+
example:
231235

232236
```javascript
233237
import codegen from 'babel-plugin-codegen/macro'
@@ -243,10 +247,11 @@ export const c = "c";
243247

244248
### APIs not supported by the macro
245249

246-
* [file comment (`// @codegen`)](#codegen-file-comment--codegen)
247-
* [import comment](#import-comment)
250+
- [file comment (`// @codegen`)](#codegen-file-comment--codegen)
251+
- [import comment](#import-comment)
248252

249-
> You could also use [`codegen.macro`][codegen.macro] if you'd prefer to type less 😀
253+
> You could also use [`codegen.macro`][codegen.macro] if you'd prefer to type
254+
> less 😀
250255
251256
## Caveats
252257

@@ -274,8 +279,8 @@ Thanks goes to these people ([emoji key][emojis]):
274279
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
275280

276281
<!-- prettier-ignore -->
277-
| [<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") | [<img src="https://avatars1.githubusercontent.com/u/1958812?v=4" width="100px;"/><br /><sub><b>Michael Rawlings</b></sub>](https://github.com/mlrawlings)<br />[💻](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Code") [📖](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Documentation") [⚠️](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Tests") |
278-
| :---: | :---: |
282+
| [<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") | [<img src="https://avatars1.githubusercontent.com/u/1958812?v=4" width="100px;"/><br /><sub><b>Michael Rawlings</b></sub>](https://github.com/mlrawlings)<br />[💻](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Code") [📖](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Documentation") [⚠️](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=mlrawlings "Tests") | [<img src="https://avatars3.githubusercontent.com/u/5230863?v=4" width="100px;"/><br /><sub><b>Jan Willem Henckel</b></sub>](https://jan.cologne)<br />[💻](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=djfarly "Code") [📖](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=djfarly "Documentation") [⚠️](https://github.com/kentcdodds/babel-plugin-codegen/commits?author=djfarly "Tests") |
283+
| :---: | :---: | :---: |
279284

280285
<!-- ALL-CONTRIBUTORS-LIST:END -->
281286

other/CODE_OF_CONDUCT.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
**Table of Contents**
88

9-
* [Our Pledge](#our-pledge)
10-
* [Our Standards](#our-standards)
11-
* [Our Responsibilities](#our-responsibilities)
12-
* [Scope](#scope)
13-
* [Enforcement](#enforcement)
14-
* [Attribution](#attribution)
9+
- [Our Pledge](#our-pledge)
10+
- [Our Standards](#our-standards)
11+
- [Our Responsibilities](#our-responsibilities)
12+
- [Scope](#scope)
13+
- [Enforcement](#enforcement)
14+
- [Attribution](#attribution)
1515

1616
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
1717

@@ -29,21 +29,21 @@ orientation.
2929
Examples of behavior that contributes to creating a positive environment
3030
include:
3131

32-
* Using welcoming and inclusive language
33-
* Being respectful of differing viewpoints and experiences
34-
* Gracefully accepting constructive criticism
35-
* Focusing on what is best for the community
36-
* Showing empathy towards other community members
32+
- Using welcoming and inclusive language
33+
- Being respectful of differing viewpoints and experiences
34+
- Gracefully accepting constructive criticism
35+
- Focusing on what is best for the community
36+
- Showing empathy towards other community members
3737

3838
Examples of unacceptable behavior by participants include:
3939

40-
* The use of sexualized language or imagery and unwelcome sexual attention or
40+
- The use of sexualized language or imagery and unwelcome sexual attention or
4141
advances
42-
* Trolling, insulting/derogatory comments, and personal or political attacks
43-
* Public or private harassment
44-
* Publishing others' private information, such as a physical or electronic
42+
- Trolling, insulting/derogatory comments, and personal or political attacks
43+
- Public or private harassment
44+
- Publishing others' private information, such as a physical or electronic
4545
address, without explicit permission
46-
* Other conduct which could reasonably be considered inappropriate in a
46+
- Other conduct which could reasonably be considered inappropriate in a
4747
professional setting
4848

4949
## Our Responsibilities

other/MAINTAINING.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
**Table of Contents**
88

9-
* [Code of Conduct](#code-of-conduct)
10-
* [Issues](#issues)
11-
* [Pull Requests](#pull-requests)
12-
* [Release](#release)
13-
* [Thanks!](#thanks)
9+
- [Code of Conduct](#code-of-conduct)
10+
- [Issues](#issues)
11+
- [Pull Requests](#pull-requests)
12+
- [Release](#release)
13+
- [Thanks!](#thanks)
1414

1515
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
1616

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@
2929
"author": "Kent C. Dodds <kent@doddsfamily.us> (http://kentcdodds.com/)",
3030
"license": "MIT",
3131
"dependencies": {
32-
"babel-core": "^6.26.3",
3332
"babel-plugin-macros": "^2.2.1",
34-
"babel-register": "^6.26.0",
35-
"babel-template": "^6.26.0",
3633
"require-from-string": "^2.0.2"
3734
},
3835
"devDependencies": {

src/__tests__/__snapshots__/index.js.snap

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ const x = codegen.require("./fixtures/return-one", "should not be here...")
199199
200200
↓ ↓ ↓ ↓ ↓ ↓
201201
202-
Error: <PROJECT_ROOT>/src/__tests__/index.js: \`codegen.require\`-ed module (./fixtures/return-one) cannot accept arguments because it does not export a function. You passed the arguments: should not be here...
202+
Error: <PROJECT_ROOT>/src/__tests__/index.js: codegen module (src/__tests__/fixtures/return-one) cannot accept arguments because it does not export a function. You passed the arguments: should not be here...
203203
204204
`;
205205
@@ -225,6 +225,34 @@ var two = "2";
225225
226226
`;
227227
228+
exports[`codegen 27. codegen: 27. codegen 1`] = `
229+
230+
codegen\`module.exports = "var ALLCAPS = 'ALLCAPS'"\`
231+
232+
↓ ↓ ↓ ↓ ↓ ↓
233+
234+
var ALLCAPS;
235+
ALLCAPS = 'ALLCAPS', void 0;
236+
237+
`;
238+
239+
exports[`codegen accepts babels parser options for generated code: accepts babels parser options for generated code 1`] = `
240+
241+
// @codegen
242+
module.exports = "var fNum: number = do { if(true) {100} else {200} };"
243+
244+
↓ ↓ ↓ ↓ ↓ ↓
245+
246+
var fNum: number = do {
247+
if (true) {
248+
100;
249+
} else {
250+
200;
251+
}
252+
};
253+
254+
`;
255+
228256
exports[`codegen handles multipe nodes: handles multipe nodes 1`] = `
229257
230258
codegen.require('./fixtures/multiple-nodes')

src/__tests__/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pluginTester({
8989
// @codegen
9090
/* comment */`,
9191
},
92+
'codegen`module.exports = "var ALLCAPS = \'ALLCAPS\'"`',
9293
],
9394
})
9495

@@ -133,5 +134,15 @@ pluginTester({
133134
\\\`
134135
\`
135136
`,
137+
'accepts babels parser options for generated code': {
138+
babelOptions: {
139+
filename: __filename,
140+
parserOpts: {plugins: ['flow', 'doExpressions']},
141+
},
142+
code: `
143+
// @codegen
144+
module.exports = "var fNum: number = do { if(true) {100} else {200} };"
145+
`,
146+
},
136147
},
137148
})

0 commit comments

Comments
 (0)