Skip to content

Commit a9ce06f

Browse files
author
Kent C. Dodds
committed
fix: update to babel-plugin-macros
1 parent a965b83 commit a9ce06f

6 files changed

Lines changed: 64 additions & 29 deletions

File tree

‎README.md‎

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@
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

2826
The 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
4543
export was (which should be a string) and converts that string to an AST node
4644
and 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

5073
This 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
6285
for potentially being a little more confusing).
6386

6487
Important notes:
88+
6589
1. All code run by `codegen` is _not_ run in a sandboxed environment
6690
2. All code _must_ run synchronously.
6791
3. All code will be transpiled via `babel-core` directly or `babel-register`
@@ -72,8 +96,6 @@ Important notes:
7296
4. 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

124146
You 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)
206228
you can import/require the codegen macro at `babel-plugin-codegen/macro`.
207229
For example:
208230

@@ -244,8 +266,11 @@ here!
244266
Thanks 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

251276
This project follows the [all-contributors][all-contributors] specification.
@@ -283,4 +308,4 @@ MIT
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

‎package.json‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,25 @@
1717
"precommit": "kcd-scripts precommit"
1818
},
1919
"files": ["dist", "macro.js"],
20-
"keywords": ["babel-macros", "babel-plugin", "babel", "code generation"],
20+
"keywords": [
21+
"babel-plugin-macros",
22+
"babel-plugin",
23+
"babel",
24+
"code generation"
25+
],
2126
"author": "Kent C. Dodds <kent@doddsfamily.us> (http://kentcdodds.com/)",
2227
"license": "MIT",
2328
"dependencies": {
2429
"babel-core": "^6.26.0",
25-
"babel-macros": "^1.0.0",
30+
"babel-plugin-macros": "^2.0.0",
2631
"babel-register": "^6.26.0",
2732
"babel-template": "^6.26.0",
28-
"require-from-string": "^1.2.1"
33+
"require-from-string": "^2.0.1"
2934
},
3035
"devDependencies": {
31-
"ast-pretty-print": "^2.0.0",
32-
"babel-plugin-tester": "^4.0.0",
33-
"kcd-scripts": "^0.4.0"
36+
"ast-pretty-print": "^2.0.1",
37+
"babel-plugin-tester": "^5.0.0",
38+
"kcd-scripts": "^0.30.4"
3439
},
3540
"eslintConfig": {
3641
"extends": "./node_modules/kcd-scripts/eslint.js"

‎src/__tests__/.babelrc‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": [["env", {"targets": {"node": "8"}}]]
3+
}

‎src/__tests__/macro.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path'
22
import pluginTester from 'babel-plugin-tester'
3-
import plugin from 'babel-macros'
3+
import plugin from 'babel-plugin-macros'
44

55
const projectRoot = path.join(__dirname, '../../')
66

‎src/index.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ function resolveModuleToString({args, filename, source}) {
126126
if (argValues.length) {
127127
if (typeof mod !== 'function') {
128128
throw new Error(
129-
`\`codegen.require\`-ed module (${source.node
130-
.value}) cannot accept arguments because it does not export a function. You passed the arguments: ${argValues.join(
129+
`\`codegen.require\`-ed module (${
130+
source.node.value
131+
}) cannot accept arguments because it does not export a function. You passed the arguments: ${argValues.join(
131132
', ',
132133
)}`,
133134
)
@@ -170,6 +171,7 @@ function isPrimitive(val) {
170171

171172
/*
172173
eslint
173-
import/no-unassigned-import:0
174-
import/no-dynamic-require:0
174+
complexity: ["error", 8],
175+
import/no-unassigned-import: "off",
176+
import/no-dynamic-require: "off",
175177
*/

‎src/macro.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// const printAST = require('ast-pretty-print')
2-
const {createMacro} = require('babel-macros')
2+
const {createMacro} = require('babel-plugin-macros')
33
const replace = require('./replace')
44

55
module.exports = createMacro(codegenMacros)

0 commit comments

Comments
 (0)